有时一个UILable的text内容是变化的,而且差异有很大,

  1. 需求上要求UILabel的大小高宽能够自适应text的内容。代码例子:
  2. myLable=[[UILabel alloc] initWithFrame:CGRectMake(0, 23, 175, 33)];
  3. [myLable setFont:[UIFont fontWithName:@"Helvetica" size:10.0]];
  4. [myLable setNumberOfLines:0];
  5. [myLable setBackgroundColor:[UIColor clearColor]];
  6. [myAdView addSubview:myLable];
  7. UIFont *font = [UIFont fontWithName:@"Helvetica" size:10.0];
  8. CGSize size = [text sizeWithFont:font constrainedToSize:CGSizeMake(175.0f, 2000.0f)
  9. lineBreakMode:UILineBreakModeWordWrap];
  10. CGRect rect=myLable.frame;
  11. rect.size=size;
  12. [myLable setFrame:rect];
  13. [myLable setText:text];
  14. 核心的是
  15. CGSize size = [text sizeWithFont:font constrainedToSize:CGSizeMake(175.0f, 2000.0f)
  16. lineBreakMode:UILineBreakModeWordWrap];
  17. 来预算text显示时宽高。
  18. 其中font是显示的字体,constrainedToSize是最大可接受的字符串宽高(例子中是宽175,高2000)
  19. lineBreakMode换行类型(UILineBreakModeWordWrap指的单词边界换行)

sizeWithFont:constrainedToSize:lineBreakMode:

Returns the size of the string if it were rendered with the specified constraints.

- (CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size lineBreakMode:(UILineBreakMode)lineBreakMode
Parameters
font

The font to use for computing the string size.

size

The maximum acceptable size for the string. This value is used to calculate where line breaks and wrapping would occur.

lineBreakMode

The line break options for computing the size of the string. For a list of possible values, see NSLineBreakMode.

Return Value

The width and height of the resulting string’s bounding box. These values may be rounded up to the nearest whole number.

Discussion

You can use this method to obtain the layout metrics you need to draw a string in your user interface. This method does not actually draw the string or alter the receiver’s text in any way.

This method computes the metrics needed to draw the specified string. This method lays out the receiver’s text and attempts to make it fit the specified size using the specified font and line break options. During layout, the method may break the text onto multiple lines to make it fit better. If the receiver’s text does not completely fit in the specified size, it lays out as much of the text as possible and truncates it (for layout purposes only) according to the specified line break mode. It then returns the size of the resulting truncated string. If the height specified in the size parameter is less than a single line of text, this method may return a height value that is bigger than the one specified.

Availability
  • Available in iOS 2.0 and later.
Declared In

UIStringDrawing.h

转载于:https://www.cnblogs.com/mohe/p/3586568.html

uilabel 自适应相关推荐

  1. UILabel自适应高、宽

    根据Label和字体大小自适应高度 - (void)getHeightWithLabel:(UILabel *)label andFontSize:(CGFloat)size {label.numbe ...

  2. ios label 高度紫石英_IOS开发中UILabel自适应大小显示的方法

    1,根据label中的文字内容自动换行显示的方法 1>,认识两个属性 @property(nonatomic) NSIntegernumberOfLines; @property(nonatom ...

  3. UILabel自适应高度和自动换行

    码: [plain] view plaincopy //初始化label UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0,0, ...

  4. iOS UILabel UITextView自适应文本,或文本大小自适应

    //UILabel自适应文本的高度 UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 100, 300, 100)];label ...

  5. iOS开发 UILabel实现自适应高宽

    UILabel是iOS开发常用的控件.UILabel的属性需要了解,UILabel的特殊显示效果也需要我们掌握.UILabel自适应高宽度是很多初学者遇到的技术性难题.比如段文字,要让他完全地分行显示 ...

  6. UILabel实现自适应宽高需要注意的地方(三)

    一.需求图如下所示  UILabel 的高度自适应 UILabel中的段落间距可设置 图片效果如下: 调整段落适应长宽高方式: 需求: 保证"游戏玩法" 章节,UILabel高度自 ...

  7. Swift之UILabel +使用讲解+富文本+自适应高度

    导语:本文重点放在富文本设置和自适应高度.接下来会陆陆续续发布swift中UI控件的使用技巧和方法.世界在变化,不久后Swift替代OC已不是说说而已,你还在等什么??Let's go! ###一.U ...

  8. IOS开发—IOS7.0以后UILabel高度自适应设置

    IOS7.0以后UILabel高度自适应 IOS7.0以后,UILabel自适应高度的方法发生了改进,以下根据代码展示如何对一个label做自适应文本高度的操作. 代码示例: UILabel *lab ...

  9. html自适应布局视频,2018年最新的8个响应式与自适应视频教程推荐

    在学习前端的过程中经常可以看到自适应布局和响应式布局,那么,什么是响应式布局?自适应布局又是什么呢?响应式布局设计就是一个网站能够兼容多个终端-而不是为每个终端做一个特定的版本:自适应布局设计是能使网 ...

最新文章

  1. centos防火墙端口配置
  2. 全面理解Javascript闭包和闭包的几种写法及用途
  3. Python3 对于中文文件的读写处理
  4. mac nginx apache mysql php 一键_Mac 下搭建 apache / nginx+php+mysql
  5. 浅谈严格模式和混杂模式
  6. 在杭州,吃过这60样东西,人生才算完美!
  7. 老男孩python全栈开发视频教程_老男孩Python全栈开发(92天全)视频教程 自学笔记08...
  8. 2019浙江C语言二级答案,2019年下半年二级C语言试题及答案
  9. window10内核Linux,windows 10中发布完整的Linux内核
  10. 开源一周岁,MindSpore新特性巨量来袭
  11. 多线程依次接收信息并发送
  12. Mybatis缓存机制及mybatis的各个组成部分
  13. zabbix通过jmx监控tomcat
  14. pygame安装超详细讲解
  15. 最详细AMD Ryzen CPU,VMware安装macOS(虚拟机安装黑苹果)文章索引
  16. 《社交网络》中Facemash算法分析
  17. es文件创建局域网服务器,大神来教你XBMC和ES文件浏览器局域网共享
  18. 出现“你的Windows许可证即将过期”提示
  19. dlib疲劳检测_dlib库检测人脸使用方法与简单的疲劳检测应用
  20. 【杂篇 · 技巧】WebStorm页面窗口与显示bug

热门文章

  1. 枚举windows进程
  2. 2016我的程序员总结
  3. [react] react中可以在render访问refs吗?为什么?
  4. [NodeJs] npm提供了哪些钩子?各有什么作用?
  5. 前端学习(2887):如何短时间内实现v-for proxy代理
  6. 前端学习(2881):初始化dom和数据化线程池
  7. “约见”面试官系列之常见面试题第十九篇之数组去重(建议收藏)
  8. 前端学习(2124):双向绑定的案例图示分析
  9. 前端学习(1032):jquery插件-瀑布流
  10. 第一百二十五期:程序员的自我救赎,使用Python开发性格分析工具