如何找到UILabel的行数 [英] How to find UILabel's number of Lines

查看:206
本文介绍了如何找到UILabel的行数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用wrap方法在UILabel中显示文本。
现在我想要找到UILabel中有多少行。

I displayed the text in UILabel by using wrap method. Now I want to need to find the how many number of line is there in UILabel.

如果有任何可能的方法可以找到UILabel的行数计算。

If there is any possible way to find the UILabel's number of lines count.

谢谢。

推荐答案

正如所指出的,这篇文章涉及如何获得高度,而不是线的数量。要获得行数,

As pointed out, this post relates how to get the height, rather than the number of lines. To get the number of lines,


  1. 获取单个字母的高度,例如: @A

  2. 将字符串的高度除以上面1中获得的高度。

例如

CGFloat unitHeight = [@"A" heightForWidth:width usingFont:font];
CGFloat blockHeight = [text heightForWidth:width usingFont:font];
NSInteger numberOfLines =        ceilf(blockHeight / unitHeight); 
// need to #include <math.h> for ^^^^^

从iOS 7开始,的方式获得标签所需的高度已更改。要获得高度,可以使用以下代码:

As of iOS 7, the way of getting a label's desired height changed. To get the height, you can use the following code:

NSStringDrawingContext *context = [[NSStringDrawingContext alloc] init];
CGSize labelSize = (CGSize){width, FLT_MAX};
CGRect r = [self boundingRectWithSize:labelSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: font} context:context];

其中高度为 r.size.height 。请注意,必须提供 font 。为方便起见,您可以将其放入NSString的类别中,例如

where the height is r.size.height. Note that font must be provided. You can put this into a category for NSString for convenience, e.g.

@implementation NSString (HeightCalc)

- (CGFloat)heightForWidth:(CGFloat)width usingFont:(UIFont *)font
{
    NSStringDrawingContext *context = [[NSStringDrawingContext alloc] init];
    CGSize labelSize = (CGSize){width, FLT_MAX};
    CGRect r = [self boundingRectWithSize:labelSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: font} context:context];
    return r.size.height;
}

@end

(如果没有内存管理)使用ARC,顺便说一句。)

(Do memory management if not using ARC, btw.)

适用于iOS 6及以下版本:

让我们一起来假设您有 UILabel * myLabel 并且您想要找出标签的高度(通过一些调整,您可以通过将高度除以适当的高度来获得行数数字取决于字体大小。)

Let's say you have a UILabel *myLabel and you want to find out the height of the label (with some tweaking, you can get the # of lines by dividing the height with some appropriate number which depends on the font size).

UILabel *myLabel;
CGSize labelSize = [myLabel.text sizeWithFont:myLabel.font 
                            constrainedToSize:myLabel.frame.size 
                                lineBreakMode:UILineBreakModeWordWrap];
CGFloat labelHeight = labelSize.height;

希望有所帮助。如果它不起作用,请告诉我,我会进一步挖掘。此外,未经测试的代码,但参考工作。

Hope that helps. If it doesn't work let me know and I'll dig further. Also, untested code, but worked from reference.

有关更完整的示例,这里是我放在视图控制器的viewDidLoad:方法中的代码:

For a more complete example, here is code which I put in the viewDidLoad: method of a view controller:

[super viewDidLoad];
UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(50,50,200,350)];
myLabel.numberOfLines = 0;
myLabel.lineBreakMode = UILineBreakModeWordWrap;
myLabel.text = @"This is some text in a UILabel which is long enough to wrap around the lines in said UILabel. This is a test, this is only a test.";
[self.view addSubview:myLabel];
CGSize labelSize = [myLabel.text sizeWithFont:myLabel.font 
                            constrainedToSize:myLabel.frame.size 
                                lineBreakMode:UILineBreakModeWordWrap];
CGFloat labelHeight = labelSize.height;
NSLog(@"labelHeight = %f", labelHeight);
[myLabel release];

NSLog的输出为:

The output from the NSLog goes:

2010-11-15 18:25:27.817 so_labelheight[728:307] labelHeight = 126.000000

这篇关于如何找到UILabel的行数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆