iOS:根据UILabel的大小找到适合UILabel的正确字体大小 [英] iOS: finding correct font size to fit in a UILabel depending on its size
问题描述
我有一个 UILabel
,其属性 text
我想将文本段落设置为使用 NSString
。
I have a UILabel
whose property text
I would like to set paragraphs of text to using NSString
.
我有一个数组,我在其中存储一个代表文本段落的字符序列。
I have an array in which I store a sequence of characters that represent a text paragraph.
该段落不必完全符合/包含在 UILabel
中。如果段落没有结束,我会转到下一个标签。
The paragraph doesn't have to fit/included perfectly within this UILabel
. If the paragraph doesn't end, I would move on to the next label.
如果我有这个 UILabel
160 X 240的矩形大小,我怎么能够确定正确的字体大小,以便很好地填写 UILabel
的字符串?
Let say if I had rect size of this UILabel
160 X 240, how could I be able to determine the correct font size in order to fill this string of the UILabel
within nicely?
是否有数学方法根据屏幕上rect的大小计算字体大小?
Is there a mathematical way for the calculation of font size based upon the size of the rect on the screen?
例如:
UILabel *aLabel = [[UIlabel alloc] initwithFrame(CGRect){{0, 0}, 160, 240}];
aLabel.font = [UIFont fontWithName:@"Courier New" size:<???>]; //(font unit in points)
NSMutableString *aString = [[NSMutableString alloc] init];
//The following tries to fit the character one by one within the label rect based on
//the width and height of the UILabel by appending String
int index = 0;
for(int x = 0; x < 240; x+=<???>) //height of label (pixel unit)
{
for(int y = 0; y < 160; y+=<???>) //width of label (pixel unit)
{
[aString appendWithString:[paragraphArray objectAtIndex:index]];
index++;
}
[aString appendWithString:@\n"]; //line break
}
aLabel.text = aString;
我怎样才能确定???所在的值(即for循环的字体大小和像素大小)?
How can I be able to determine the values where the ??? are (namely the font size and the pixel size for the for loops)?
如果这不是执行此类任务的最佳方式,请另外建议我。
If this is not the optimal way to perform such a task, please kindly suggest me any other.
推荐答案
+(void)resizeFontForLabel:(UILabel*)aLabel{
// use font from provided label so we don't lose color, style, etc
UIFont *font = aLabel.font;
float lblWidth = aLabel.frame.size.width;
float lblHeight = aLabel.frame.size.height;
CGFloat fontSize = [font pointSize];
UIFont *newFont = font;
TRC_DBG(@"%@", aLabel.text);
CGFloat height = [aLabel.text sizeWithFont:font constrainedToSize:CGSizeMake(lblWidth,MAXFLOAT) lineBreakMode:aLabel.lineBreakMode].height;
TRC_DBG(@"Label Height %f Constraint height %f", lblHeight, height);
//Reduce font size while too large, break if no height (empty string)
while (height > lblHeight && height != 0) {
fontSize--;
newFont = [UIFont fontWithName:font.fontName size:fontSize];
height = [aLabel.text sizeWithFont:newFont constrainedToSize:CGSizeMake(lblWidth,MAXFLOAT) lineBreakMode:NSLineBreakByWordWrapping].height;
TRC_DBG(@"Constrained Height %f", height);
};
TRC_DBG(@"Font size before adjustment %f", aLabel.font.pointSize);
// Set the UILabel's font to the newly adjusted font.
aLabel.font = newFont;
TRC_DBG(@"Adjust to font size of %f", newFont.pointSize);
[aLabel setNeedsLayout];
}
这篇关于iOS:根据UILabel的大小找到适合UILabel的正确字体大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!