iOS根据标签不同长度内容按钮自动换行
2016-06-14 18:24
441 查看
//
// ViewController.m
// tableview
//
// Created by 方超 on 16/6/14.
// Copyright © 2016年方超. All rights reserved.
//
#import "ViewController.h"
@interface
ViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView
* tableview;
@property(nonatomic,strong)NSArray
* array;
@property(nonatomic,assign)float cellheight;
@end
@implementation ViewController
- (void)viewDidLoad {
[superviewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.tableview=[[UITableViewalloc]initWithFrame:self.view.bounds];
self.tableview.delegate=self;
self.tableview.dataSource=self;
[self.viewaddSubview:self.tableview];
self.array=@[@"11122",@"safaf",@"事实上事实上",@"爽肤水",@"啊啊",@"是的撒飞洒发舒服舒服",@"爽肤水",@"啊啊",@"是的撒飞洒发舒服舒服",@"爽",@"121",@"121"];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
returnself.array.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath
*)indexPath{
returnself.cellheight;
}
- (CGSize)getSizeByString:(NSString*)string AndFontSize:(CGFloat)font
{
CGSize size = [stringboundingRectWithSize:CGSizeMake(999,25)
options:NSStringDrawingTruncatesLastVisibleLine |NSStringDrawingUsesLineFragmentOrigin
|NSStringDrawingUsesFontLeadingattributes:@{NSFontAttributeName:[UIFontsystemFontOfSize:font]}context:nil].size;
size.width +=5;
return size;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath
*)indexPath{
staticNSString * strcell=@"cellstr";
UITableViewCell * cell=[tableViewdequeueReusableCellWithIdentifier:strcell];
if (!cell){
cell=[[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:strcell];
}
int width =0;
int height =0;
int number =0;
int han =0;
for (int i =0; i <
self.array.count; i++){
UILabel *button = [[UILabelalloc]initWithFrame:CGRectMake(0,15,
50,20)];
button.tag =300 + i;
button.textAlignment=NSTextAlignmentCenter;
CGSize titleSize = [selfgetSizeByString:self.array[i]AndFontSize:13];
han = han +titleSize.width;
if (han >= [[UIScreenmainScreen]bounds].size.width-75)
{
han = 0;
han = han + titleSize.width;
height++;
width = 0;
width = width+titleSize.width;
number = 0;
button.frame =CGRectMake(11,30
+25*height, titleSize.width,20);
self.cellheight=button.frame.origin.y;
}else{
button.frame =CGRectMake(width+11+(number*10),30
+25*height, titleSize.width,20);
width = width+titleSize.width;
self.cellheight=button.frame.origin.y;
}
number++;
button.enabled=NO;
button.font = [UIFontsystemFontOfSize:11];
button.layer.masksToBounds =YES;
button.layer.cornerRadius =5;
button.textColor=[UIColorwhiteColor];
button.text=[self.arrayobjectAtIndex:i];
[button.layersetBorderWidth:1];
//设置按钮的边界颜色
CGColorSpaceRef colorSpaceRef =CGColorSpaceCreateDeviceRGB();
CGColorRef color =CGColorCreate(colorSpaceRef, (CGFloat[]){0.5,0.5,0.5,0.5});
[button.layersetBorderColor:color];
[cell addSubview:button];
}
return cell;
}
@end
// ViewController.m
// tableview
//
// Created by 方超 on 16/6/14.
// Copyright © 2016年方超. All rights reserved.
//
#import "ViewController.h"
@interface
ViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView
* tableview;
@property(nonatomic,strong)NSArray
* array;
@property(nonatomic,assign)float cellheight;
@end
@implementation ViewController
- (void)viewDidLoad {
[superviewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.tableview=[[UITableViewalloc]initWithFrame:self.view.bounds];
self.tableview.delegate=self;
self.tableview.dataSource=self;
[self.viewaddSubview:self.tableview];
self.array=@[@"11122",@"safaf",@"事实上事实上",@"爽肤水",@"啊啊",@"是的撒飞洒发舒服舒服",@"爽肤水",@"啊啊",@"是的撒飞洒发舒服舒服",@"爽",@"121",@"121"];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
returnself.array.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath
*)indexPath{
returnself.cellheight;
}
- (CGSize)getSizeByString:(NSString*)string AndFontSize:(CGFloat)font
{
CGSize size = [stringboundingRectWithSize:CGSizeMake(999,25)
options:NSStringDrawingTruncatesLastVisibleLine |NSStringDrawingUsesLineFragmentOrigin
|NSStringDrawingUsesFontLeadingattributes:@{NSFontAttributeName:[UIFontsystemFontOfSize:font]}context:nil].size;
size.width +=5;
return size;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath
*)indexPath{
staticNSString * strcell=@"cellstr";
UITableViewCell * cell=[tableViewdequeueReusableCellWithIdentifier:strcell];
if (!cell){
cell=[[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:strcell];
}
int width =0;
int height =0;
int number =0;
int han =0;
for (int i =0; i <
self.array.count; i++){
UILabel *button = [[UILabelalloc]initWithFrame:CGRectMake(0,15,
50,20)];
button.tag =300 + i;
button.textAlignment=NSTextAlignmentCenter;
CGSize titleSize = [selfgetSizeByString:self.array[i]AndFontSize:13];
han = han +titleSize.width;
if (han >= [[UIScreenmainScreen]bounds].size.width-75)
{
han = 0;
han = han + titleSize.width;
height++;
width = 0;
width = width+titleSize.width;
number = 0;
button.frame =CGRectMake(11,30
+25*height, titleSize.width,20);
self.cellheight=button.frame.origin.y;
}else{
button.frame =CGRectMake(width+11+(number*10),30
+25*height, titleSize.width,20);
width = width+titleSize.width;
self.cellheight=button.frame.origin.y;
}
number++;
button.enabled=NO;
button.font = [UIFontsystemFontOfSize:11];
button.layer.masksToBounds =YES;
button.layer.cornerRadius =5;
button.textColor=[UIColorwhiteColor];
button.text=[self.arrayobjectAtIndex:i];
[button.layersetBorderWidth:1];
//设置按钮的边界颜色
CGColorSpaceRef colorSpaceRef =CGColorSpaceCreateDeviceRGB();
CGColorRef color =CGColorCreate(colorSpaceRef, (CGFloat[]){0.5,0.5,0.5,0.5});
[button.layersetBorderColor:color];
[cell addSubview:button];
}
return cell;
}
@end
相关文章推荐
- firefox css自动换行的实现方法
- ASP.NET GridView中文本内容无法换行(自动换行/正常换行)
- css 不换行 自动换行 强制换行的实现方法
- ASP.NET Datagridview自动换行的小例子
- 效控制C#中label输出文字的长度,自动换行
- Discuz 公告效果(自动换行,无间隙滚动)
- 浅析响应式框架中,table表头自动换行的快速解决方法
- 绘制长度不定可自动换行的标签
- UILabel自动换行,自适应高度
- 字符串固定字节换行
- iOS-UILabel 自动换行
- 文字自动换行,保留空格和换行符
- RadioButton实现自动换行的效果
- CSS控制文本自动换行
- css3文本属性
- Android 自定义View实现多行RadioGroup (MultiLineRadioGroup)
- Android Canvas.drawText(...)自动换行
- Autolayout下UILabel的自动换行实现
- Android自定义ViewGroup之子控件的自动换行和添加删除
- iOS小技巧--Label自动换行