封装自定义View,创建N个按钮,按钮根据title长度Size-fit显示
2015-02-27 15:37
489 查看
为了实现 创建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>
效果如图:
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>
相关文章推荐
- IOS让返回按钮显示自定义标题而不是上个ViewController的title
- iOS 在一个屏幕中根据不同的按钮创建不同的 view 默认显示第一个
- 创建静态控件——静态控件是用来显示的。只要需要根据size移动其大小就好了,内部显示不用处理
- yii CGridView 如何根据字段值来显示不同的自定义按钮或者文字?
- 安卓Toast显示提示消息(自定义view,根据子线程消息显示提示)
- [Phonegap+Sencha Touch] 移动开发47 NavigationView如何根据不同的View显示不同的顶栏按钮
- 自定义view封装-用Button显示商品图片和名称
- ios中tableview的创建和自定义cell的封装
- iOS创建不带nib文件的工程之四--在自定义view上添加按钮
- 60-给自定义的tableView的cell创建分割线(代码封装)
- 自定义view--带进度显示的下载按钮
- 自定义View之MultiStateView根据不同状态显示不同布局的View(雷惊风)
- [Phonegap+Sencha Touch] 移动开发48 扩展的NavigationView控件,支持根据不同的View显示不同的顶栏按钮、不同的屏幕方向,增加视图缓存机制
- iOS 在一个屏幕中根据不同的按钮创建不同的 view
- 自定义View中textSize显示异常
- 自定义了一个titleVIew放在navigationBar上面 位置显示不正常
- 自定义View(通过ViewGroup实现,Textview长度根据字数的多少而定的)
- 自定义View中图片不能根据屏幕大小自动创建
- yii CGridView 如何根据字段值来显示不同的自定义按钮或者文字或不同样式(转)
- android 自定义View对Title的封装