简体   繁体   English

iOS如何动态确定UILabel的大小

[英]iOS how to determine the size of UILabel dynamically

I'm trying to figure out a way to know what would be the size of the UILabel base on the string and font size to know how define the CGRect dimensions. 我试图找出一种方法来了解UILabel基本上字符串和字体大小的大小,以了解如何定义CGRect维度。

Here what I have until know: 在此我知道了:

UILabel *myLabel = [[UILabel alloc] init];
myLabel.font = [UIFont fontWithName:@"BentonSansComp-Medium" size:50.0];
myLabel.text = @"This is a string example"  

The problem is I don't have a list of the posible strings but and need to center the label and the string should not be chop (using the sting above example: "This is a str...."). 问题是我没有可用字符串列表,但需要将标签居中,字符串不应该是剁(使用上面的例子:“这是一个str ......”)。 any of you knows how can I determine the size of the UILabel base on the string and font size? 你们中的任何人都知道如何根据字符串和字体大小确定UILabel的大小?

I'll really appreciate your help. 我真的很感谢你的帮助。

just use 只是用

 UILabel *myLabel = [[UILabel alloc] init];
 myLabel.font = [UIFont fontWithName:@"BentonSansComp-Medium" size:50.0];
 myLabel.text = @"This is a string example" 
 [myLabel sizeToFit]; //it will resize the label to just the text is set
 CGRectMake size = myLabel.frame; // u will get the frame of the label once is resized
 float height = size.size.height;  //to get the height of the label
 float width = size.size.width;  //to get the width of the label

hope it helps 希望能帮助到你

Make sure evrytime you change the text u call the [myLabel sizeToFit]; 确保改变文本的时间你调用[myLabel sizeToFit]; method 方法

Just had the same problem. 刚遇到同样的问题。 Calculating a lot of sizes of UILabel s or UITextView s is expensive, so I wondered if there's a cheaper way than configuring a label or text view and then asking it for the size. 计算很多大小的UILabelUITextView是很昂贵的,所以我想知道是否有比配置标签或文本视图更便宜的方式然后询问它的大小。 This is especially important when calculating row heights for long table views. 在计算长表视图的行高时,这一点尤其重要。

Turns out, there is: UILabel seems to be built up UIKit's string additions for layout and drawing while UITextView uses Text Kit (in iOS 7). 事实证明,UILabel似乎构建了UIKit的布局和绘图的字符串添加,而UITextView使用Text Kit(在iOS 7中)。 Both facilities can be configured to use the same settings and calculate string dimensions cheaper that using the UIKit classes. 两个工具都可以配置为使用相同的设置,并使用UIKit类计算更便宜的字符串尺寸。

For a UILabel here's what I found out: 对于UILabel来说,这是我发现的:

CGFloat labelHeight = [testString boundingRectWithSize:(CGSize){ labelWidth, CGFLOAT_MAX }
                                               options:NSStringDrawingUsesLineFragmentOrigin
                                            attributes:@{ NSFontAttributeName : labelFont };
                                               context:nil].size.height;
labelHeight = ceil(labelHeight);

This assumes numberOfLines = 0 . 这假设numberOfLines = 0 Note the need to round up the result. 注意需要对结果进行舍入。

I tested above code with about 4000 string rendered into 100 different widths and all results were equal to the UILabel I used for comparison. 我测试了上面的代码,大约4000个字符串渲染成100个不同的宽度,所有结果都等于我用于比较的UILabel。

I did the same to calculate dimensions of UITextView which is more complicated and required to set up an NSLayoutManager . 我做了同样的事情来计算UITextView尺寸,它更复杂,需要设置NSLayoutManager Here the speedup is even more impressive (~ 50 times faster than using the UITextView). 这里的加速比更令人印象深刻(比使用UITextView快约50倍)。

If anybody's interested here's the code and test project . 如果有人对此感兴趣的是代码和测试项目

I think what you need is multiple lines, that will add up the area needed below. 我认为你需要的是多行,这将增加下面所需的区域。 Follow this and there won't be ... anymore 按照这个,不会再......了

myLabel.lineBreakMode = NSLineBreakByWordWrapping;
myLabel.numberOfLines = 0;

Make sure to take a look at this question Adjust UILabel height depending on the text . 请务必查看此问题根据文字调整UILabel高度

Seems to be what you're looking for. 似乎是你在寻找什么。

You can calculate the width of the label by doing this: 您可以通过执行以下操作来计算标签的width

[@"STRING" sizeWithFont:[UIFont fontWithName:@"FONT" size:12] forWidth:200 lineBreakMode:NSLineBreakByWordWrapping];

Otherwise you can just set the string in the label, and then call sizeToFit , this will set the frame of the label based on the text. 否则你只需在标签中设置字符串,然后调用sizeToFit ,这将根据文本设置标签的frame

in iOS7 use NSString method boundingRectWithSize:options:context: . 在iOS7中使用NSString方法boundingRectWithSize:options:context: . Look docs here . 看看这里的文档。

CGRect rectYouNeed =
  [myLabel.text boundingRectWithSize:CGSizeMake(100, CGFLOAT_MAX)
                             options:NSStringDrawingUsesLineFragmentOrigin
                             attributes:@{ NSFontAttributeName : myLabel.font }
                             context:nil];

check my code , i have written it in one of my app. 检查我的代码,我已经在我的一个应用程序中写了它。

CGSize maximumLabelSize = CGSizeMake(270, FLT_MAX);
CGSize expectedLabelSize = [myString sizeWithFont:[UIFont fontWithName:@"Helvetica" size:16.0f] constrainedToSize:maximumLabelSize lineBreakMode:NSLineBreakByWordWrapping];

NSLog(@"Expected Size of label based on string--%@",NSStringFromCGSize(expectedLabelSize));
//adjust the label the the new height.
CGRect newFrame = myLabel.frame;//get initial frame of your label
newFrame.size.height = expectedLabelSize.height;
myLabel.frame = newFrame;
NSLog(@"new frame of label--%@",NSStringFromCGRect(newFrame));
[myLabel setText:attString];

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

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