如何找到UILabel可以容纳的字符数?

时间:2022-05-17 16:35:47

I am trying to find the number of characters that a UILabel can hold. I am using

我试图找到UILabel可以容纳的字符数。我在用

lbl_Text = [[UILabel alloc]initWithFrame:CGRectMake(0,15,290,320)];
lbl_Text.lineBreakMode = UILineBreakModeWordWrap;
lbl_Text.numberOfLines = 20;
lbl_Text.font = [UIFont fontWithName:@"Helvetica-Bold" size:14];

to set the font and font size. Can anyone find the number of characters that this UILabel can hold ?

设置字体和字体大小。任何人都可以找到这个UILabel可以容纳的角色数量吗?

1 个解决方案

#1


1  

You can check the size of a particular string in a particular font with NSString's sizeWithFont: method. You can't ask "how many characters can I fit?" because in a proportional spaced font like Helvetica all the characters occupy different widths. Typically you know exactly what you need to display but size can vary, so you can check sizes with successively smaller fonts until you find the size that fits (which is why some of the native controls will automatically do this for you).

您可以使用NSString的sizeWithFont:方法检查特定字体中特定字符串的大小。你不能问“我能装多少个角色?”因为在像Helvetica这样的比例间隔字体中,所有字符都占据不同的宽度。通常,您确切地知道需要显示的内容,但大小可能会有所不同,因此您可以使用连续较小的字体检查大小,直到找到适合的大小(这就是为什么某些本机控件会自动为您执行此操作)。

#1


1  

You can check the size of a particular string in a particular font with NSString's sizeWithFont: method. You can't ask "how many characters can I fit?" because in a proportional spaced font like Helvetica all the characters occupy different widths. Typically you know exactly what you need to display but size can vary, so you can check sizes with successively smaller fonts until you find the size that fits (which is why some of the native controls will automatically do this for you).

您可以使用NSString的sizeWithFont:方法检查特定字体中特定字符串的大小。你不能问“我能装多少个角色?”因为在像Helvetica这样的比例间隔字体中,所有字符都占据不同的宽度。通常,您确切地知道需要显示的内容,但大小可能会有所不同,因此您可以使用连续较小的字体检查大小,直到找到适合的大小(这就是为什么某些本机控件会自动为您执行此操作)。