动态改变UILabel的字体大小 [英] Dynamically changing font size of UILabel

查看:190
本文介绍了动态改变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 是强制性的。

多行:



对于 numberOfLines> 1 有一个方法可以通过

  CGSize lLabelSize = [yourText sizeWithFont:factLabel.font forWidth:factLabel.frame.size.width lineBreakMode:factLabel.lineBreakMode]; 

之后,您可以使用结果 lLabelSize

  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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆