label在cell中 自适应
来源:互联网 发布:麻辣it网 编辑:程序博客网 时间:2024/06/06 18:48
先在初始化方法中将之创建,并设置numberOfLines = 0
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{ self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self ) { UILabel *tempContent = [[UILabel alloc]initWithFrame:CGRectMake(self.titleLabel.frame.origin.x, CGRectGetMaxY(self.titleLabel.frame),BOUNDS.size.width-self.titleLabel.frame.origin.x-15, 30)]; tempContent.font = [UIFont systemFontOfSize:14.0f]; tempContent.textAlignment = NSTextAlignmentLeft; tempContent.textColor = CLColor(102, 102, 102); tempContent.numberOfLines = 0; [self.contentView addSubview:tempContent]; self.contentLabel = tempContent; } return self;}
在添加请求文字的方法中,根据请求到的内容,让其根据内容自适应
- (void)setCellValueWith:(NSDictionary *)param { self.contentLabel.frame = CGRectMake(self.titleLabel.frame.origin.x, CGRectGetMaxY(self.titleLabel.frame),BOUNDS.size.width-self.titleLabel.frame.origin.x-15, 0); [self.contentLabel sizeToFit];//让内容自适应 }
控制器界面
//得到内容的自适应高度-(CGFloat)contentHeightWithSize:(CGFloat)size width:(CGFloat)width string:(NSString *)string{ //第一个参数:是进行自适应的尺寸 第二个参数:布局格式 第三个参数:字符串的属性列表 第四个忽略 CGRect rect = [string boundingRectWithSize:CGSizeMake(width, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:size]} context:nil]; return rect.size.height;}- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ NSDictionary *dic = [_dataSource objectAtIndex:indexPath.row]; CGFloat contentH = [self contentHeightWithSize:13.0 width:200 string:[dic objectForKey:@"topic"]]; return 60+contentH;}
好了,已经可以实现label的自适应啦,是不是很简单
0 0
- label在cell中 自适应
- swift 中cell 自适应label高度
- swift 中cell 自适应label高度
- iOS cell 的自适应高度 中 返回label的高度
- iOS 中 cell和 label 的自适应高度
- iOS中cell自适应文本Label高度篇
- StoryBoard设置Cell中的Label自适应高度
- label中文字的自适应
- UItableView Cell 高度自适应--随着多个label内容变化自适应
- iOS 中Label在自适应宽高的同时设置文字行距行距
- Label自适应
- label自适应
- ios 中内容label高度自适应
- iOS中label的自适应高度
- Label字体大小自适应label
- Label--Label自适应
- cell自适应
- Tableview中cell的自适应高度
- eclipse中安装tomcat插件
- CocoaPods安装和使用教程:出处:http://code4app.com/article/cocoapods-install-usage
- iOS项目代码行数统计
- java代码中将金额阿拉伯数字转化为中文大写
- PAT 1085. Perfect Sequence (25)
- label在cell中 自适应
- [Android] AsyncTask 的学习
- 好用的http工具
- Apache Shiro 集成-Cas
- win7开启无线共享上网
- RYU入门教程
- 如何允许跨域?什么是Access-Control-Allow-Origin:*
- java 开发微型公众账号应用
- ASE 登录 报错Msg 4002, Level 14, State 1:Login failed. CT-LIBRARY error: ct_connect(): protocol speci