Masonry学习之UILabel
来源:互联网 发布:数组和指针作为形参 编辑:程序博客网 时间:2024/06/14 10:03
先看效果:
代码:
- (id)init { self = [super init]; if (!self) return nil; // text courtesy of http://baconipsum.com/ self.shortLabel = UILabel.new; self.shortLabel.numberOfLines = 1; self.shortLabel.textColor = [UIColor purpleColor]; self.shortLabel.lineBreakMode = NSLineBreakByTruncatingTail; self.shortLabel.text = @"Bacon"; self.shortLabel.backgroundColor = [UIColor redColor]; [self addSubview:self.shortLabel]; self.longLabel = UILabel.new; self.longLabel.numberOfLines = 8; self.longLabel.textColor = [UIColor darkGrayColor]; self.longLabel.lineBreakMode = NSLineBreakByTruncatingTail; self.longLabel.text = @"Bacon ipsum dolor sit amet spare ribs fatback kielbasa salami, tri-tip jowl pastrami flank short loin rump sirloin. Tenderloin frankfurter chicken biltong rump chuck filet mignon pork t-bone flank ham hock."; self.longLabel.backgroundColor = [UIColor greenColor]; [self addSubview:self.longLabel]; [self.longLabel makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.left).insets(kPadding); make.top.equalTo(self.top).insets(kPadding); }]; [self.shortLabel makeConstraints:^(MASConstraintMaker *make) { //make.top.equalTo(self.longLabel.lastBaseline);// 该行代码为原示例代码,使用它会造成崩溃,原因未知,以下一行暂时替代 make.top.equalTo(self.longLabel.bottom); make.right.equalTo(self.right).insets(kPadding); }]; return self;}
这里UILabel的设置都比较常规了,而长label和短label的约束只分别设置了left、top和right、top;
- (void)layoutSubviews { [super layoutSubviews]; // for multiline UILabel's you need set the preferredMaxLayoutWidth // 多行UILabel需要设置preferredMaxLayoutWidth // you need to do this after [super layoutSubviews] as the frames will have a value from Auto Layout at this point // 你必须在[super layoutSubviews]之后设置preferredMaxLayoutWidth,以为此时frames才会有值 // stay tuned for new easier way todo this coming soon to Masonry CGFloat width = CGRectGetMinX(self.shortLabel.frame) - kPadding.left; width -= CGRectGetMinX(self.longLabel.frame); self.longLabel.preferredMaxLayoutWidth = width; // need to layoutSubviews again as frames need to recalculated with preferredLayoutWidth // 再次布局,因为frames需要根据preferredLayoutWidth重新计算 [super layoutSubviews];}
我改变了一下文字内容进行了一些测试,感觉效果并不好。对于UILabel,以前用的最多的方式就是固定宽度而高度进行自适应,而这个例子的中,UILabel的高度和宽度都会随着内容的变化而变化,其用意我不是很理解。
阅读全文
0 0
- Masonry学习之UILabel
- Masonry学习之UIScrollView
- Masonry学习之Array
- OC学习之UILabel
- IOS学习之UILabel
- swift学习之UILabel
- Masonry学习之基本布局
- Masonry学习之更新约束
- Masonry学习之使用常量
- Masonry学习之复合edges
- Masonry学习之aspect fit
- Masonry学习之基本动画
- Masonry学习之链式属性
- Masonry学习之Distribute View
- Masonry学习之Layout Guides
- IOS控件学习之UILabel
- iOS之UI学习-UILabel
- iOS 自动布局框架学习之Masonry
- linux查找文件命令find
- Move Zeroes(leetcode)
- Unity3D --游戏体上所有Component组件复制
- Error:Please select Android SDK
- 态度支付,如何用聚合支付帮助商家赚钱
- Masonry学习之UILabel
- 算符优先系列之(一)Firstvt和Lastvt集 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Descri
- 字符解码时加号解码为空格问题探究
- 文章标题
- Linux 多线程编程
- Java调用Python测试成功的记录
- 手机内部储存的绝对地址
- GCD运行dispatch_block,并取消(dispatch_block_flags_t flags)
- http server 搭建攻略