Label 自适应文本(StoryBoard/xib)
2016-02-19 22:38
295 查看
To make your label automatically resize height you need to do following:
Set layout constrains for label
Set height constraint with low priority. It should be lower than ContentCompressionResistancePriority
Set numberOfLines = 0
Set ContentHuggingPriority higher than label's height priority
Set preferredMaxLayoutWidth for label. That value is used by label to calculate its height
For example:
Using Interface Builder
Set up four constraints. The height constraint is mandatory.
Then go to the label's attributes inspector and set number of lines to 0.
Go to the label's size inspector and increase vertical ContentHuggingPriority and vertical ContentCompressionResistancePriority.
Select and edit height constraint.
And decrease height constraint priority.
Enjoy. :)
Set layout constrains for label
Set height constraint with low priority. It should be lower than ContentCompressionResistancePriority
Set numberOfLines = 0
Set ContentHuggingPriority higher than label's height priority
Set preferredMaxLayoutWidth for label. That value is used by label to calculate its height
For example:
self.descriptionLabel = [[UILabel alloc] init]; self.descriptionLabel.numberOfLines = 0; self.descriptionLabel.lineBreakMode = NSLineBreakByWordWrapping; self.descriptionLabel.preferredMaxLayoutWidth = 200; [self.descriptionLabel setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical]; [self.descriptionLabel setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical]; [self.descriptionLabel setTranslatesAutoresizingMaskIntoConstraints:NO]; [self addSubview:self.descriptionLabel]; NSArray* constrs = [NSLayoutConstraint constraintsWithVisualFormat:@"|-8-[descriptionLabel_]-8-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(descriptionLabel_)]; [self addConstraints:constrs]; [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-8-[descriptionLabel_]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(descriptionLabel_)]]; [self.descriptionLabel addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[descriptionLabel_(220@300)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(descriptionLabel_)]];
Using Interface Builder
Set up four constraints. The height constraint is mandatory.
Then go to the label's attributes inspector and set number of lines to 0.
Go to the label's size inspector and increase vertical ContentHuggingPriority and vertical ContentCompressionResistancePriority.
Select and edit height constraint.
And decrease height constraint priority.
Enjoy. :)
相关文章推荐
- 最近关于evaluation model的进阶理解(待更新)
- Session
- 2016/02/19 codes
- 前端试题-cssText?
- 突破短板,传统桌面程序 使用webapi 扩展迎合web和移动端融合的需求
- 三思而行 --jsp基础篇
- HDU 5172 GTY's gay friends
- android 音乐播放bug
- ACM_普通DP
- [Introduction to programming in Java 笔记] 1.3.9 Factoring integers 素因子分解
- java高性能 tcp协议代码示例(一)
- 栈
- 1.1.3:可视化的累加器
- 20.php回调、匿名函数、和闭包操作
- HDU2535:Vote
- 矢量化的HTML5拓扑图形组件设计
- 数据离散化
- VM12即VMware Workstation 12 序列号
- 关于C语言include尖括号和双引号的对话
- Light OJ 1421 Wavio Sequence (简单DP)