为了实现 创建N多个按钮, 并且让按钮根据所给的title长度自适应, 并且高度自适应,封装了这个类
效果如图:
ButtonView.h
//
// ButtonView.h
// AiComic
//
// Created by Damon on 15/2/26.
// Copyright (c) 2015年 Damon. All rights reserved.
//
#import "BaseView.h"
@protocol searchButtonViewDelegate <NSObject>
// 协议方法:
//点击每个按钮时的协议方法
- (void)searchButtonViewClicked:(NSString *)str;
// 传出此时布局后View的高度
- (void)searchButtonViewFrame:(CGFloat)y;
@end
@interface ButtonView : BaseView
// 代理
@property (nonatomic, retain) id<searchButtonViewDelegate>delegate;
// 数组, 存储title
@property (nonatomic, retain) NSMutableArray *arr;
// View的高度
@property (nonatomic, assign) CGFloat y;
// 重写初始化方法
- (id)initWithFrame:(CGRect)frame buttonNumber:(NSInteger)number;
// 给数组赋值
- (void)setMyArr:(NSMutableArray *)arr;
@end
ButtonView.m文件
//
// ButtonView.m
// AiComic
//
// Created by Damon on 15/2/26.
// Copyright (c) 2015年 Damon. All rights reserved.
//
#import "ButtonView.h"
@interface ButtonView ()
// 按钮的数量
@property (nonatomic, assign) NSInteger number;
// 字体大小
@property (nonatomic,retain)UIFont *font;
@end
@implementation ButtonView
// 初始化
- (id)initWithFrame:(CGRect)frame buttonNumber:(NSInteger)number
{
self = [super initWithFrame:frame];
if (self) {
self.arr = [NSMutableArray array];
self.number = number;
for (int i = 1; i <= number; i++) {
[self.arr addObject:[NSString stringWithFormat:@"%d",i]];
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
[button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
button.tag = i;
// self.button0.backgroundColor = [UIColor redColor];
[self addSubview:button];
self.font = button.titleLabel.font;
}
}
return self;
}
- (void)layoutSubviews
{
[super layoutSubviews];
CGFloat width = self.frame.size.width;
CGFloat frontWidth = 10;
CGFloat widthOfButton = 20;
CGFloat buttonHeight = 20;
self.y = 20;
// 布局
for (int i = 1; i <=self.arr.count; i++) {
if (i == 1) {
UIButton *button = (UIButton *)[self viewWithTag:i];
button.frame = CGRectMake(frontWidth, self.y, [self getLabelSize:[self.arr objectAtIndex:i - 1]].width, buttonHeight);
} else {
UIButton *upButton = (UIButton *)[self viewWithTag:i - 1];
UIButton *button = (UIButton *)[self viewWithTag:i];
if ((upButton.frame.size.width + upButton.frame.origin.x + widthOfButton + frontWidth + [self getLabelSize:[self.arr objectAtIndex:i - 1]].width) > width) {
self.y = self.y + 40;
button.frame = CGRectMake(frontWidth, self.y, [self getLabelSize:[self.arr objectAtIndex:i - 1]].width, buttonHeight);
} else {
button.frame = CGRectMake(upButton.frame.origin.x + upButton.frame.size.width + widthOfButton, self.y, [self getLabelSize:[self.arr objectAtIndex:i - 1]].width, buttonHeight);
}
}
}
// 布局后 把当前高度传给代理
[self.delegate searchButtonViewFrame:self.y];
}
// 获取字符串的宽度
- (CGSize)getLabelSize:(NSString *)str
{
CGSize size = CGSizeMake(150, 20);
NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:self.font, NSFontAttributeName, nil];
CGSize Labelsize = [str boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil].size;
return Labelsize;
}
// //点击每个按钮时的协议方法的实现
- (void)buttonClicked:(UIButton *)button
{
// 让代理执行方法
[self.delegate searchButtonViewClicked:button.titleLabel.text];
}
// 给标题数组赋值
- (void)setMyArr:(NSMutableArray *)arr
{
//self.number = arr.count;
[self.arr removeAllObjects];
[self.arr addObjectsFromArray:arr];
[self setTitleOfButton];
}
// 给每个button赋值
- (void)setTitleOfButton
{
NSInteger i = 1;
for (NSString *str in self.arr) {
UIButton *button = (UIButton *)[self viewWithTag:i];
[button setTitle:str forState:UIControlStateNormal];
i++;
}
[self layoutSubviews];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
@end<span style="color:#ff0000;">
</span>