简体   繁体   English

计算标签的高度和行数

[英]Calculate height and number of lines of the label

I have a text that is shown in a UILabel . 我有一个显示在UILabel的文本。 However, this text have several line spaces as shown below. 但是,此文本具有多个行间距,如下所示。 Now, I want to calculate the height of this label, considering the newline, Bold-text and font size. 现在,我要考虑换行符,粗体和字体大小来计算该标签的高度。 Since this text can not be placed in a Single line in the label, there might be several lines that we must determine at runtime. 由于此文本不能放在标签的一行中,因此在运行时可能必须确定几行。 Based on this height I want to increase the y cordinate of my UILabel so the UILabel will always be stuck to the bottom of the screen. 基于此高度,我想增加UILabel的y坐标,以便UILabel始终固定在屏幕底部。 (Only the height will increase (upwards)) (只有高度会增加(向上))

How can I solve this? 我该如何解决?

NSAttributedString *linespace = [[NSAttributedString alloc] initWithString: @"\n"];

NSMutableAttributedString *mutableString = [[NSMutableAttributedString alloc]initWithString:@"Mathews is a " attributes:@{NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue-Bold" size:40]}];  

NSAttributedString*    attributed = [[NSAttributedString alloc]initWithString:[NSString stringWithFormat:@"%@", @"Bad guy"] attributes:@{NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue-Bold" size:40]}];


[mutableString appendAttributedString: linespace];

[mutableString appendAttributedString: attributed];

[mutableString appendAttributedString: linespace];

You can get height by 你可以得到身高

CGRect rectCountry = [mutableString boundingRectWithSize:(CGSize){@“YOUR WIDTH”, CGFLOAT_MAX}
                                                     options:NSStringDrawingUsesLineFragmentOrigin
                                                     context:nil];
CGSize sizeCountry = rectCountry.size;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM