【问题标题】:iOS: Resize all UILabel Fonts to matchiOS:调整所有 UILabel 字体以匹配
【发布时间】:2012-11-07 20:39:31
【问题描述】:
我有几个用文本填充的 UILabel。
UILabels 中的所有字体都设置为自动收缩。
如果其中一个 UILabel 包含太多文本(并且必须自动调整大小),我该如何更改剩余的 UILabel 字体大小以匹配已调整大小的那个?
在我的代码末尾,我使用了以下内容:
if (self.label.font.pointSize < 16){
labelTwo.font = [UIFont fontWithName:@"Noteworthy-Bold" size:self.label.font.pointSize];
我的默认字体大小是 16,所以我正在检查字体大小是否已减小。
【问题讨论】:
标签:
ios
cocoa-touch
uilabel
autoresize
【解决方案1】:
我现在不在我的 Mac 上,所以我不能为你编写实际的代码,但我可以为你模拟,这样你就知道该往哪个方向发展了。
Create a variable to store the smallest font size.
Foreach for each UILabel in the view.
Get the font size.
Check the font size against the smallest known so far.
If the font size is smaller than the smallest known, save it.
Else, disregard.
After all UILabels in view have been checked go back through them, and set the font size to that of the saved value
【解决方案2】:
我就是这样实现的
CGFloat fontSize = self.label1.font.pointSize;
CGFloat fontSize1 = self.label2.font.pointSize;
CGFloat fontSize2 = self.label3.font.pointSize;
NSArray *numbers = @[[NSNumber numberWithFloat:fontSize],[NSNumber numberWithFloat:fontSize1],[NSNumber numberWithFloat:fontSize2]];
float xmax = -MAXFLOAT;
float xmin = MAXFLOAT;
for (NSNumber *num in numbers) {
float x = num.floatValue;
if (x < xmin) xmin = x;
if (x > xmax) xmax = x;
NSLog(@"lowest number = %f",xmin);