ios不同版本下的UILabel自动换行问题
2016-02-23 21:26
417 查看
@interface ThreadPreviewCell : UITableViewCell @property (nonatomic, strong) UILabel *titleLabel; @end @implementation ThreadPreviewCell - (void)layoutSubviews{ [super layoutSubviews]; //ios6中需要加上这句话 self.titleLabel.preferredMaxLayoutWidth = self.titleLabel.bounds.size.width; } - (void)initSubviews { self.titleLabel = [[UILabel alloc]init]; self.titleLabel.numberOfLines = 0; self.titleLabel.lineBreakMode = NSLineBreakByWordWrapping; self.titleLabel.font = [UIFont systemFontOfSize:14]; self.titleLabel.backgroundColor = [UIColor clearColor]; [self.contentView addSubview:self.titleLabel]; } @end
表现为在iOS7以上的系统中,UILabel能够自动换行,多行显示的字符串,而在iOS6上面则不会自动换行,直接打省略号。
正常情况下,numberOfLines设置为0,UILabel就会自动换行了。
但是在iOS6下面需要设置preferredMaxLayoutWidth,autolayout才会判断到折行的位置,才能正确的显示多行的UILabel
但是preferredMaxLayoutWidth设置为多少才是正确的呢?
如果你知道一个确切的width当然是最好的,那么直接设置即可,
但是如果UILabel的宽度是自适应的,不确定,那么可以使用以上的代码设置
相关文章推荐
- STL_deque
- 1101. Quick Sort (25)
- UITableViewControl的编辑(删除,添加,移动)
- IOS-53-图片、button等空间是否可拉伸设置;以及UITabelView的headerView设置
- 等待控件(UIActivityIndicatorView)不转动的原因
- Universal-Image-Loader(UIL)图片加载框架使用简单介绍
- IOS-52-UITableView中cell分割线左对齐(解决iOS8版本适配问题)
- Rocky's Requirements for ideal HW
- hdu 5288 OO’s Sequence
- Implement Stack using Queues
- 4.UIViewController
- iOS涂鸦画板 主要自定义UIView
- unable to find valid certification path to requested target或Certificate chaining error的解决办法
- uibutton点击按钮时title变大 松开时变小
- Kinect NuiInitialize failed
- 错误:The request sent by the client was syntactically incorrect的解决
- Implement Queue using Stacks
- Volley源码解析<三> Request请求
- 使用requireJS加载不符合AMD规范的js文件:shim的使用方式和实现原理
- UIAlertView, UIAlertViewController