当前位置:网站首页>Calculate text size based on width
Calculate text size based on width
2022-04-22 23:53:00 【~Abandoned memories】
@implementation UILabel (****)
/** * @brief Calculate the size of the text * * @param font typeface ( Default to system font ) * @param width Constraint width */
- (CGSize)df_sizeWithFont:(UIFont *)font constrainedToWidth:(CGFloat)width
{
UIFont *textFont = font ? font : [UIFont systemFontOfSize:[UIFont systemFontSize]];
CGSize textSize;
#if __IPHONE_OS_VERSION_MIN_REQUIRED < 70000
if ([self respondsToSelector:@selector(boundingRectWithSize:options:attributes:context:)]) {
NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];
paragraph.lineBreakMode = NSLineBreakByWordWrapping;
NSDictionary *attributes = @{
NSFontAttributeName: textFont,
NSParagraphStyleAttributeName: paragraph};
textSize = [self boundingRectWithSize:CGSizeMake(width, CGFLOAT_MAX)
options:(NSStringDrawingUsesLineFragmentOrigin |
NSStringDrawingTruncatesLastVisibleLine)
attributes:attributes
context:nil].size;
} else {
textSize = [self sizeWithFont:textFont
constrainedToSize:CGSizeMake(width, CGFLOAT_MAX)
lineBreakMode:NSLineBreakByWordWrapping];
}
#else
NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];
paragraph.lineBreakMode = NSLineBreakByWordWrapping;
NSDictionary *attributes = @{
NSFontAttributeName: textFont,
NSParagraphStyleAttributeName: paragraph};
textSize = [self.text boundingRectWithSize:CGSizeMake(width, CGFLOAT_MAX)
options:(NSStringDrawingUsesLineFragmentOrigin |
NSStringDrawingTruncatesLastVisibleLine)
attributes:attributes
context:nil].size;
#endif
return CGSizeMake(ceil(textSize.width), ceil(textSize.height));
}
@end
版权声明
本文为[~Abandoned memories]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204222352443375.html
边栏推荐
- Visual studio always conflicts with Sogou input method
- Tracup|减少压力和更多的成功,帮助您摆脱工作焦虑的利器
- People's understanding of the industrial Internet is becoming clearer and closer to the industrial Internet
- 在windows7下安装vs2017插件 GLSL language integration v0.10.120.vsix报错:microsoft.visualc.redist.12
- [dvcon2020] software brothers shout: Hardware brothers, please be personal
- Detailed explanation of MySQL index
- Codeforce 1669 A and B
- 用C语言编写一个俄罗斯方块小游戏
- The implementation principle and function of closure and memory leakage
- Day81 (dynamic programming, cross tree traversal)
猜你喜欢

LabVIEW controls computer shutdown, hibernation, logout and restart

Introduction to cloud computing simulation framework cloudsim (screenshot version)

ssh: connect to host 172.31.8.132 port 22: Connection refused

"100 million" little technical feelings
![[leetcode] binary tree, maximum depth of 104 binary tree, diameter of 543 binary tree, maximum path and sum of 124 binary tree](/img/f8/5de0ecee78a6f9c9bd8562629355be.png)
[leetcode] binary tree, maximum depth of 104 binary tree, diameter of 543 binary tree, maximum path and sum of 124 binary tree

Reg regular expression learning notes

在windows7下安装vs2017插件 GLSL language integration v0.10.120.vsix报错:microsoft.visualc.redist.12

Online fox U3D client game configuration loading failed couldn't connect to server

Method of shrinking master-slave nodes in redis cluster cluster

Vscode uses emmylua plug-in to debug unity project tolua code
随机推荐
Typora style tuning
[experience sharing] share mangopapa's paper learning experience
Django creates table structure and adds, deletes, modifies and queries database
条件判断(whilec循环&for&switch循环&if-else判断)
字体自适应
[Objective-C advanced programming] - GCD
Mung bean sprouts on the 22nd day of home
[perseverance challenge] PCIe asks and answers every day (2022.04 in progress)
[transaction management]
共轭梯度法(Conjugate Gradients)(3)
2022硬刚PLUS德施曼年度峰会:多款领航旗舰发布,引领高端市场
字符集合(去重)
FileNotFoundError: [Errno 2] No such file or directory: 'image/1. Jpg 'problem solving
FileNotFoundError: [Errno 2] No such file or directory: 'image/1. Jpg 'problem solving
根据高度计算文字大小
AcWing 1842. Milk bucket traverses all situations
FPGA (V) one of RTL codes (cross clock domain design)
[day22] - [linked list] intersecting linked list
2022-4-22 job MySQL single table query
FileNotFoundError: [Errno 2] No such file or directory: 'image/1.jpg'问题解决