当前位置:网站首页>根据宽度计算文字大小
根据宽度计算文字大小
2022-04-22 23:52:00 【~废弃回忆 �༄】
@implementation UILabel (****)
/** * @brief 计算文字的大小 * * @param font 字体(默认为系统字体) * @param 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
版权声明
本文为[~废弃回忆 �༄]所创,转载请带上原文链接,感谢
https://blog.csdn.net/eastWind1101/article/details/117434772
边栏推荐
- Introduction to opencv (II) -- affine transformation and perspective transformation
- 合并两个有序的数组
- Thought of reducing Governance -- detailed summary of binary search
- LeetCode 1446 - 1449
- 邦纳超声波传感器T30UXDA
- Hello, I want to open an account for crude oil futures. How can I open an account for futures safely and reliably?
- STM32F103 independent watchdog
- 【DVCon2020】采用先进 Save & Restore 技术加速仿真周转时间 (Simulation Turnaround Time)
- [dvcon2020] software brothers shout: Hardware brothers, please be personal
- [PCIe 6.0] new features of PCIe 6.0 - detailed explanation of dmwr (deferred memory write)
猜你喜欢

共轭梯度法(Conjugate Gradients)(4)

STM32F103 independent watchdog
![[annual summary] carry forward the past and forge ahead into the future: look back on the unreliable 2021 and hope for the reliable 2022](/img/0f/c50f7b392a15bb0b190d684fcfa385.png)
[annual summary] carry forward the past and forge ahead into the future: look back on the unreliable 2021 and hope for the reliable 2022

2022-04-22:给你一个大小为 m x n 的矩阵 board 表示甲板,其中,每个单元格可以是一艘战舰 ‘X‘ 或者是一个空位 ‘.‘ ,返回在甲板 board 上放置的 战舰 的数量。 战舰

【DVCon2020】基于Signoff Abstract Model的低功耗设计层级验证加速

SystemVerilog verification - Test Platform writing guide learning notes (3): connecting design and test platform

Basic use of redis
![[turtle confession collection]](/img/81/b4bacc23691e58e403f1330d0ca7cf.jpg)
[turtle confession collection] "the moon at the bottom of the sea is the moon in the sky, and the person in front of us is the sweetheart." More joy and peace for the rest of your life ~ (with 3 sourc

Vscode uses emmylua plug-in to debug unity project tolua code

SystemVerilog verification - Test Platform preparation guide learning notes (0): introduction to verification
随机推荐
Typora樣式調優
【Pygame小游戏】Chrome上的小恐龙竟可以用代码玩儿了?它看起来很好玩儿的样子~
Online fox U3D client game configuration loading failed couldn't connect to server
Vs-写汇编
Visual studio 2019 restore default settings
FileNotFoundError: [Errno 2] No such file or directory: 'image/1.jpg'问题解决
51 单片机学习_4-2 数码管动态显示
Reg 正则表达式学习笔记
matlab-day01
FPGA(五)RTL代码之一(跨时钟域设计)
Interpretation of UVM source code, uvm-1.2 code review notes
【Objective-C 高级编程】—— GCD
【leetcode】二叉树,226翻转二叉树,116填充二叉树节点的右侧指针,114将二叉树展开为链表
IOT identity awareness
(MM-2018)用于行人重识别的局部卷积神经网络
FPGA (V) one of RTL codes (cross clock domain design)
2022-4-22 job MySQL single table query
条件判断(whilec循环&for&switch循环&if-else判断)
Wechat applet page Jump
Magic weapon for easy working time: log in to the server by means of key verification