动态改变UILabel的字体大小 [英] Dynamically changing font size of UILabel
问题描述
我目前有一个 UILabel
:
factLabel = [[ UILabel alloc] initWithFrame:CGRectMake(20,100,280,100)];
factLabel.text = @一些文字一些文字一些文字一些文字;
factLabel.backgroundColor = [UIColor clearColor];
factLabel.lineBreakMode = UILineBreakModeWordWrap;
factLabel.numberOfLines = 10;
[self.view addSubview:factLabel];
在iOS应用程序的整个生命周期中, factLabel
得到一堆不同的值。有些有多个句子,有的只有5或6个单词。
如何设置 UILabel
以便字体大小发生变化,以便文本始终适合边界我定义了?
单行:
factLabel.numberOfLines = 1;
factLabel.minimumFontSize = 8;
factLabel.adjustsFontSizeToFitWidth = YES;
上面的代码会将文本的字体大小调整为(例如) 8
试图使你的文字符合标签。
numberOfLines = 1
是强制性的。
多行:
CGSize lLabelSize = [yourText sizeWithFont:factLabel.font forWidth:factLabel.frame.size.width lineBreakMode:factLabel.lineBreakMode];
之后,您可以使用结果 lLabelSize $ c例如(假设你只改变标签的高度):
factLabel.frame = CGRectMake(factLabel.frame .origin.x,factLabel.frame.origin.y,factLabel.frame.size.width,lLabelSize.height);
iOS6
<单行:
从iOS6开始, minimumFontSize
已被弃用。该行
factLabel.minimumFontSize = 8。
可以更改为:
factLabel.minimumScaleFactor = 8./factLabel.font.pointSize;
iOS7
<多行:
从iOS7开始,不推荐使用 sizeWithFont
。
多行情况简化为:
factLabel.numberOfLines = 0;
factLabel.lineBreakMode = NSLineBreakByWordWrapping;
CGSize maximumLabelSize = CGSizeMake(factLabel.frame.size.width,CGFLOAT_MAX);
CGSize expectSize = [factLabel sizeThatFits:maximumLabelSize];
factLabel.frame = CGRectMake(factLabel.frame.origin.x,factLabel.frame.origin.y,expectSize.width,expectSize.height);
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?
Single line:
factLabel.numberOfLines = 1;
factLabel.minimumFontSize = 8;
factLabel.adjustsFontSizeToFitWidth = YES;
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 UIKit addition methods, for example:
CGSize lLabelSize = [yourText sizeWithFont: factLabel.font forWidth:factLabel.frame.size.width lineBreakMode:factLabel.lineBreakMode];
After that you can just resize your label using resulting lLabelSize
, for example (assuming that you will change only label's height):
factLabel.frame = CGRectMake(factLabel.frame.origin.x, factLabel.frame.origin.y, factLabel.frame.size.width, lLabelSize.height);
iOS6
Single line:
Starting with iOS6, minimumFontSize
has been deprecated. The line
factLabel.minimumFontSize = 8.;
can be changed to:
factLabel.minimumScaleFactor = 8./factLabel.font.pointSize;
iOS7
Multiple lines:
Starting with iOS7, sizeWithFont
becomes deprecated.
Multiline case is reduced to:
factLabel.numberOfLines = 0;
factLabel.lineBreakMode = NSLineBreakByWordWrapping;
CGSize maximumLabelSize = CGSizeMake(factLabel.frame.size.width, CGFLOAT_MAX);
CGSize expectSize = [factLabel sizeThatFits:maximumLabelSize];
factLabel.frame = CGRectMake(factLabel.frame.origin.x, factLabel.frame.origin.y, expectSize.width, expectSize.height);
这篇关于动态改变UILabel的字体大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!