Label 自适应文本(StoryBoard/xib)
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:
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. :)
Label 自适应文本(StoryBoard/xib)的更多相关文章
- 27、Label 自适应文本 xib
第一步: 第二步: 第三步: 第四步:
- label自适应文本大小
UILabel *label = [[UILabelalloc] initWithFrame:CGRectZero]; NSString *string = @"aa2fkoksdajfis ...
- 通过代码创建label 计算最佳尺寸 让其自适应文本高度或宽度
通过xib创建label 让label随着文本内容的变化而动态改变尺寸比较简单 只要不固定死尺寸就可以了 但是通过代码创建的话我们需要给label设置一个frame 否则label默认是没有尺 ...
- iOS UILabel UITextView自适应文本,或文本大小自适应
//UILabel自适应文本的高度 UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(, , , )]; label.numberOf ...
- 浅析 - Storyboard / Xib
大家都知道纯代码写应用的成本是很高的,特别是涉及到UI界面的实现,相当耗费时间.之前自己写应用时有了解过Storyboard,也简单使用过,但随着最近深入了解它之后,发现自己低估了它的作用和影响力,因 ...
- label自适应
//label自适应 self.label = [UILabel new]; self.label.font = [UIFont systemFontOfSize:14]; NSString *tit ...
- iOS Label 自适应高度
推荐第二个 测试一,只改变numberOfLines属性,label的高度不会自适应(会有text中的一部分内容称为......) NSString *str = @"jgreijgirje ...
- iOS开发笔记-根据frame大小动态调整fontSize的自适应文本及圆形进度条控件的实现
最近同样是新App,设计稿里出现一种圆形进度条的设计,如下: 想了想,圆形进度条实现起来不难,但是其中显示百分比的文本确需要自适应,虽然可以使用时自己设定文本字体的大小,但是这样显得很麻烦,也很low ...
- TableView中Label自适应高度
//Xcode6.3以后label自适应需要添加两个属性 _tableView.rowHeight = UITableViewAutomaticDimension; //给予预计行高 _tableVi ...
随机推荐
- 六十七:flask上下文之Local线程隔离对象
Local对象在flask中,类似于request对象,其实是绑定到了werkzeug.local.Local对象上,这样即使是同一个对象,在多线程中都是隔离的,类似的对象还有session以及g对象 ...
- Linux 源码安装nginx
编译参数详解:https://www.cnblogs.com/houyongchong/p/compileArgs.html 配置参数详解:https://www.cnblogs.com/houyon ...
- Python——GUI编程 利息计算器 作业9(python programming)
import sys from PyQt5.QtCore import * from PyQt5.QtGui import * from PyQt5.QtWidgets import * class ...
- Spark内核源码解析
1.spark内核架构常用术语 Application:基于spark程序,包含一个driver program(客户端程序)和多个executeor(线程) Driver Progrom:代表着sp ...
- springboot和springcloud版本冲突问题
最近搭建eureka项目,出现boot和cloud版本不匹配错误,记录下来 2019-12-06 14:00:20.043 ERROR 180780 --- [ main] o.s.boot.Spri ...
- 使用shell脚本常见的一些问题
Jdk版本:jdk-8u102-linux-x64 Tomcat版本:apache-tomcat-7.0.92 Redis版本:redis-5.0.0 由于公司项目的需要,要在多台服务器上面部署一些应 ...
- pthon基础知识(索引、切片、序列相加、乘法、检查元素是否是序列成员、计算序列长度、最大最小值)
序列 数据存储方式 数据结构 python 列表.元组.字典.集合.字符串 序列: 一块用于存放多个值的连续内存空间,并且按一定顺序排列,可以通过索引取值 索引(编号): 索引可以是负数 从左到 ...
- Django与Cookie
Cookie Cookie的由来 大家都知道HTTP协议是无状态的.无状态的意思是每次请求都是独立的,它的执行情况和结果与前面的请求和之后的请求都无直接关系,它不会受前面的请求响应情况直接影响,也 ...
- LeetCode.1071-字符串最大公约数(Greatest Common Divisor of Strings)
这是小川的第391次更新,第421篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第253题(顺位题号是1071).对于字符串S和T,当且仅当S = T + ... + T ...
- 字符串——AC自动机
目录 一.前言 二.思路 三.代码 四.参考资料 一.前言 以前一直没学AC自动机,主要是被名字吓到了,自动AC,这么强的名字肯定很难,学了后才发现,其实不难. AC自动机并不是Acept autom ...