iOS中的UILabel自动计算高度和换行
2016-07-20 00:00
411 查看
UILabel自动计算高度和换行
self.dizhiLabel = [[UILabel alloc]init]; self.dizhiLabel.textColor = [UIColor grayColor]; //折行 self.dizhiLabel.lineBreakMode = NSLineBreakByWordWrapping; //必须写,否则只显示一行 [ self.dizhiLabel setNumberOfLines:0]; [self.contentView addSubview: self.dizhiLabel]; [ self.dizhiLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.zuobiaoImageView.mas_right).offset(20); make.right.equalTo(self.contentView.mas_right).offset(-20); make.top.equalTo( self.zuobiaoImageView.mas_top); }]; //根据文字内容和字体计算高度 CGSize textSize = CGSizeZero; // 多行必需使用NSStringDrawingUsesLineFragmentOrigin,网上有人说不是用NSStringDrawingUsesFontLeading计算结果不对 NSStringDrawingOptions opts = NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading; NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init]; [style setLineBreakMode:NSLineBreakByCharWrapping]; NSDictionary *attributes = @{ NSFontAttributeName : [UIFont systemFontOfSize:17], NSParagraphStyleAttributeName : style }; CGRect rect = [ self.dizhiLabel.text boundingRectWithSize:(CGSize){ self.dizhiLabel.frame.size.width, MAXFLOAT} options:opts attributes:attributes context:nil]; textSize = rect.size; self.dizhiLabel.frame = CGRectMake(85, 15, textSize.width, textSize.height);
相关文章推荐
- UIImage保存到沙盒
- Hibernate的事务模式与反模式
- UE4类修饰符
- UE4类修饰符
- $.ajax请求报400 bad request错误
- 【NGUI】ui tween 和 play tween
- 【一天一道LeetCode】#374. Guess Number Higher or Lower
- 【一天一道LeetCode】#374. Guess Number Higher or Lower
- String, StringBuilder, StringBuffer 之间的区别 - Java
- gulp+webpack+vue
- 关于UIBarButtonItem的style属性(Plain,Bordered,Done的区别)
- HUE的安装
- UISegmentedControl分段控件使用
- iOS 中UITableView的深理解
- 103.项目视图分析工具Reveal集成使用教程
- Hbuilder MUI用原生js添加或移除class属性
- 小米2s 用线刷,刷回MIUI V5了
- (2016.12.2更新)CnCrypt加密U盘1.18,将U盘划分为普通盘和加密盘,支持与U盘启动盘共存
- UVA - 123 Searching Quickly
- if+while+for+continue+break