动态改变UILabel的字体大小
我目前有一个 UILabel
:
factLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 100, 280, 100)];
factLabel.text = @"some text some text some text some text";
factLabel.backgroundColor = [UIColor clearColor];
factLabel.lineBreakMode = UILineBreakModeWordWrap;
factLabel.numberOfLines = 10;
[self.view addSubview:factLabel];
在我的 iOS 应用程序的整个生命周期中,factLabel
获取一堆不同的值。有些有多个句子,有些只有 5 或 6 个单词。
如何设置 UILabel
以便更改字体大小,以便文本始终适合我定义的边界?
I currently have a UILabel
:
factLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 100, 280, 100)];
factLabel.text = @"some text some text some text some text";
factLabel.backgroundColor = [UIColor clearColor];
factLabel.lineBreakMode = UILineBreakModeWordWrap;
factLabel.numberOfLines = 10;
[self.view addSubview:factLabel];
Throughout the life of my iOS application, factLabel
gets a bunch of different values. Some with multiple sentences, others with just 5 or 6 words.
How can I set up the UILabel
so that the font size changes so that the text always fits in the bounds I defined?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
单行:
上面的代码会将文本的字体大小调整为(例如)
8
,尝试使文本适合标签。numberOfLines = 1
是强制性的。多行:
对于
numberOfLines > 1
有一种方法可以通过 NSString 计算出最终文本的大小sizeWithFont:... UIKit 添加 方法,例如:之后,您可以使用生成的
lLabelSize
调整标签大小,例如(假设您仅更改标签的高度):iOS6
单行:
从 iOS6 开始,
minimumFontSize
已被弃用。该行可以更改为:
iOS7
多行:
从 iOS7 开始,
sizeWithFont
已被弃用。多行大小写减少为:
iOS 13 (Swift 5):
Single line:
The above code will adjust your text's font size down to (for example)
8
trying to fit your text within the label.numberOfLines = 1
is mandatory.Multiple lines:
For
numberOfLines > 1
there is a method to figure out the size of final text through NSString's sizeWithFont:... UIKit addition methods, for example:After that you can just resize your label using resulting
lLabelSize
, for example (assuming that you will change only label's height):iOS6
Single line:
Starting with iOS6,
minimumFontSize
has been deprecated. The linecan be changed to:
iOS7
Multiple lines:
Starting with iOS7,
sizeWithFont
becomes deprecated.Multiline case is reduced to:
iOS 13 (Swift 5):
iOS 6 已弃用
minimumFontSize
。您可以使用minimumScaleFactor
。这将根据标签和文本的宽度来处理您的字体大小。
minimumFontSize
has been deprecated with iOS 6. You can useminimumScaleFactor
.This will take care of your font size according width of label and text.
单行- 有两种方式,您可以简单地更改。
1- 务实(Swift 3)
只需添加以下代码
2- 使用 UILabel 属性检查器
Single line- There are two ways, you can simply change.
1- Pragmatically (Swift 3)
Just add the following code
2 - Using UILabel Attributes inspector
根据 @Eyal Ben Dov 的回答,您可能需要创建一个类别,以使其可以在您的其他应用程序中灵活使用。
观察:我已经更新了他的代码以兼容 iOS 7
-头文件
-实现文件
-用法
干杯
Based on @Eyal Ben Dov's answer you may want to create a category to make it flexible to use within another apps of yours.
Obs.: I've updated his code to make compatible with iOS 7
-Header file
-Implementation file
-Usage
Cheers
那是 2015 年。我不得不去找一篇博客文章,解释如何使用 Swift 在最新版本的 iOS 和 XCode 上进行操作,以便它可以处理多行。
来源:
http://beckyhansmeyer.com/2015/04/09/autoshrinking-text -in-a-multiline-uilabel/
It's 2015. I had to go to find a blog post that would explain how to do it for the latest version of iOS and XCode with Swift so that it would work with multiple lines.
Source:
http://beckyhansmeyer.com/2015/04/09/autoshrinking-text-in-a-multiline-uilabel/
斯威夫特版本:
Swift version:
这是 UILabel 的 Swift 扩展。它运行二进制搜索算法,根据标签边界的宽度和高度调整字体大小。经测试可与 iOS 9 和自动布局配合使用。
用法: 其中
是您预定义的需要调整字体大小的 UILabel
默认情况下,此函数在 5pt 和 300pt 字体大小范围内搜索并设置字体使其文本“完美”适合边界内(精确到 1.0pt 以内)。例如,您可以定义参数,以便它通过以下方式在 1pt 和 标签的当前字体大小 之间精确搜索 0.1pts :
将以下代码复制/粘贴到您的文件中
注意:您可以自行决定删除每个
layoutIfNeeded()
调用Here's a Swift extension for UILabel. It runs a binary search algorithm to resize the font based off the width and height of the label's bounds. Tested to work with iOS 9 and autolayout.
USAGE: Where
<label>
is your pre-defined UILabel that needs font resizingBy Default, this function searches in within the range of 5pt and 300pt font sizes and sets the font to fit its text "perfectly" within the bounds (accurate within 1.0pt). You could define the parameters so that it, for example, searches between 1pt and the label's current font size accurately within 0.1pts in the following way:
Copy/Paste the following code into your file
NOTE: Each of the
layoutIfNeeded()
calls can be removed at your own discretion它有点不复杂,但这应该可行,
例如,假设您想将 uilabel 限制为 120x120,最大字体大小为 28:
Its a little bit not sophisticated but this should work,
for example lets say you want to cap your uilabel to 120x120, with max font size of 28:
只需将 sizeToFit 消息发送到 UITextView 即可。它将调整自己的高度以适合其文本。它不会改变自己的宽度或原点。
Just send the sizeToFit message to the UITextView. It will adjust its own height to just fit its text. It will not change its own width or origin.
Swift 2.0 版本:
Swift 2.0 Version:
该解决方案适用于多行:
在阅读了几篇文章之后,并且需要一个能够自动缩放文本并调整行数以最适合给定标签大小的函数,我自己编写了一个函数。 (即,短字符串可以很好地适合一行并使用大量标签框架,而长字符串会自动分成 2 或 3 行并相应地调整大小)
请随意重复使用它并进行调整必需的。确保在
viewDidLayoutSubviews
完成后调用它,以便设置初始标签框架。This solution works for multiline:
After following several articles, and requiring a function that would automatically scale the text and adjust the line count to best fit within the given label size, I wrote a function myself. (ie. a short string would fit nicely on one line and use a large amount of the label frame, whereas a long strong would automatically split onto 2 or 3 lines and adjust the size accordingly)
Feel free to re-use it and tweak as required. Make sure you call it after
viewDidLayoutSubviews
has finished so that the initial label frame has been set.下面是实现动画字体大小变化的 UILabel 子类的填充代码:
Here is the fill code of a UILabel subclass that implements animated font size change: