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 ...
随机推荐
- 微信小程序之阻止冒泡事件
众所周知,在微信小程序给标签绑定点击方法大家都会想到 "bindtap" 但是在页面中会遇到 点击 会冒泡而触发其他元素的时间发生 那么怎么办呢 就把引发冒泡事件的始作俑者的 bi ...
- P2983 [USACO10FEB]购买巧克力
P2983 [USACO10FEB]购买巧克力 题解 注意题目开 long long 贪心策略:价格从低到高,买够为止 反证:若剩下的有一个K”,比K小,那么交换,稳赚不赔 所以,在买K之前,所有比他 ...
- LC 889. Construct Binary Tree from Preorder and Postorder Traversal
Return any binary tree that matches the given preorder and postorder traversals. Values in the trave ...
- Linux安全加固之中间件Tomcat
(注:皆为参考操作配置) 这次是tomcat的Linux加固,分为身份鉴别.访问控制.安全审计.资源控制和入侵防范5个方面大部分加固基于xml配置文件进行修改,也应根据实际需求制定方案.寻找配置文件目 ...
- Spring学习之==>IoC
一.概述 Spring的三大核心思想:IoC(控制反转),DI(依赖注入),AOP(面向切面编程).本问讲着重介绍一下控制反转. 何谓控制反转:Spring 通过一种称作控制反转(IoC)的技术促进了 ...
- httplib/urllib实现
httplib模块是一个底层基础模块,可以看到建立HTTP请求的每一步,但是实际的功能比较少.在python爬虫开发中基本用不到 下面详细介绍httplib提供的常用类型和方法: httplib.HT ...
- redhat网卡设置
在终端中输入:vi /etc/sysconfig/network-scripts/ifcfg-eth0 开始编辑,填写ip地址.子网掩码.网关.DNS等.其中“红框内的信息”是必须得有的. 编 ...
- PYTHON TDD学习(一)-->Python 3.4版本环境安装Django及其启动
1.安装Python3.4版本,原因:3.4及其以后版本默认自带pip工具,非常好用 2.django 安装命令(c:\Python34\Scripts):pip install django 3.s ...
- python-Web-django-时间插件-三级联动
时间插件: 第一步:下载 https://www.layui.com/laydate/ 下载插件包,放在项目的static 第二步:在html里引入js: <script src="/ ...
- (转) pip Fatal error in launcher: Unable to create process using
接上篇“Eclipse启动报错:JVM terminated. Exit code=2”,今天把Python的安装位置也从C盘剪切到了D盘, 然后修改了Path环境变量中对应的盘符:D:\Python ...