iOS 计算文字宽度的一个细节
设计师给到的图,经常是 “按钮的左边距离文字右边5px” 这样子的标注。于是我们需要计算文字的宽度。
有两种方法:
用
label或button的sizetofit方法[label sizeToFit];
NSLog(@"label size is %@",[NSValue valueWithCGSize:label.bounds.size]);
用
sizeWithAttributes方法CGSize size = [label.text sizeWithAttributes:@{NSFontAttributeName:label.font}];
NSLog(@"size WithAttributes is %@",[NSValue valueWithCGSize:size]);
结果如何呢?
label size is NSSize: {96.5, 20.5}
size WithAttributes is NSSize: {96.39697265625, 20.287109375}
可见两个返回值不同,第二种方法返回的值似乎精确,应该用哪个呢?
我们来看下文档怎么说的。
关于
sizeWithAttributes的文档This method returns fractional sizes; to use a returned size to size views, you must raise its value to the nearest higher integer using the ceil function.
关于
sizeToFit的文档Call this method when you want to resize the current view so that it uses the most appropriate amount of space.
在文档中,明确指出了使用 sizeWithAttributes 时,需要使用 ceil 方法取整。而 sizeToFit 方法会当前 view 的大小调整为 appropriate 大小。
这里的关键是对 appropriate 的理解。我说下我的理解,至少应该满足两个条件:
- 不截断
就是说返回的值应该不小于文字的宽度/长度,否则会被截断。 - 像素对齐
所有的 view 的起始和结束为止都应该在物理像素上。因此在 @2x 屏幕应该返回 0.5 的整数倍,在 @1x 屏幕上应该返回 1 的整数倍。
因此应该用第二种方法,或者把第一种方法的结果向上取整。
如果直接使用第一种方法的结果,会导致其他界面像素对不齐,有奇怪的表现。
想象一下计算机如何处理 “一条线段,起点是 3.452 像素,终点是 56.341 像素” 这种奇怪的东西。
iOS 计算文字宽度的一个细节的更多相关文章
- IOS 计算文字尺寸(UILabel)
方式1 :普通用法 #define MJNameFont [UIFont systemFontOfSize:14] /** * 计算文字尺寸 * * @param text 需要计算尺寸的文字 * ...
- IOS计算文字高度
1.计算文字长度 NSString* str = @"你好"; .f; NSStringDrawingOptions options = NSStringDrawingUsesLi ...
- ios 计算文字的尺寸
/** * 计算文字尺寸 * @param text 需要计算尺寸的文字 * @param font 文字的字体 * @param maxSize 文字的最大尺寸 */ - (CGSize)sizeW ...
- canvas 计算文字宽度(常用于文字换行)
var c=document.getElementById("myCanvas"); var ctx=c.getContext("2d"); ctx.font= ...
- 关于ios下字体描边的一个细节
转载请注明,来自:http://blog.csdn.net/skyman_2001 CGContextSetTextDrawingMode(context, kCGTextStroke); ...
- ios 根据文字数量计算UILabel高度(已修改)
由于留言的朋友给出了更好的方法,所以下面的代码都是它留言中给出的,优于我前面计算Lable高度方法,这个可以说非常的准,是IOS自带的计算UILABEL高度的方式. 一.实现代码 // 创建label ...
- iOS计算完整文字高度(适应iOS 10)
动态计算文字的高度:(切记LineSapcing>=2,不然会显示不全) +(CGSize) boundingALLRectWithSize:(NSString*) txt Font:(UIFo ...
- iOS开发之计算文字尺寸
/** * 计算文字尺寸 * * @param text 需要计算尺寸的文字 * @param font 文字的字体 * @param maxSize 文字的最大尺寸 */ - ( ...
- ios 拉伸图片和计算文字的大小
一.拉伸图片 /** * 传入图片的名称,返回一张可拉伸不变形的图片 * * @param imageName 图片名称 * * @return 可拉伸图片 */ + (UIImage *)resiz ...
随机推荐
- go_gc
如果想知道当前的内存状态,可以使用: // fmt.Printf("%d\n", runtime.MemStats.Alloc/1024) // 此处代码在 Go 1.5.1下不再 ...
- 105. Construct Binary Tree from Preorder and Inorder Traversal (Tree; DFS)
Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume that ...
- mac下git push避免每次都输入用户名和密码的配置
参考链接:http://www.linuxdiyf.com/linux/18389.html 链接2:https://git-scm.com/book/zh/v2/Git-%E5%B7%A5%E5%8 ...
- bloom
bloom bloom也能实现和HDR类似的效果,但bloom的是静态的,HDR是动态渐变的, bloom在细节表现.明暗对比不如HDR,但实现HDR效果的系统资源开销也比 bloom大,bloom也 ...
- Django基础学习三_路由系统
今天主要来学习一下Django的路由系统,视频中只学了一些皮毛,但是也做下总结,主要分为静态路由.动态路由.二级路由 一.先来看下静态路由 1.需要在project中的urls文件中做配置,然后将匹配 ...
- java 框架收藏
一.java 异步非阻塞编程框架 1.Spring Webflux 2.Vert.x 3.Ratpack 4.smart-socket 国产异步框架 二.微服务框架 1.Jboot :专为大型分布式项 ...
- ScrollView中嵌套ListView的问题
网上关于怎样在ScrollView中嵌套ListView的讨论有很多,我大概是搜索了一下,简单总结如下: 1.不要在ScrollView中嵌套ListView a.用一个LinearLayout来代替 ...
- Oracle GoldenGate 四、数据过滤和数据项匹配
写在开始前 从两周前我花了大量的业余时间阅读GoldenGate官方文档,并根据文档实践和进一步学习了解GoldenGate,以下便是根据官方文档理解总结的GoldenGate学习内容: Oracle ...
- asp.net core 1.1 mysqlsugarCore mysql.data 要 7.0.5.0
Message=Could not load file or assembly 'MySql.Data, Version=7.0.5.0, Culture=neutral, PublicKeyToke ...
- Windows sql语句正则匹配导出数据到本地 The MySQL server is running with the --secure-file-priv option so it cannot execute this statement
尝试使用 into outfile导出数据的时候出现错误: The MySQL server is running with the --secure-file-priv option so it c ...