Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c “计算UILabel高度”给出了错误的值_Objective C - Fatal编程技术网

Objective c “计算UILabel高度”给出了错误的值

Objective c “计算UILabel高度”给出了错误的值,objective-c,Objective C,我试图“预测”标签的大小(宽度已知,只有高度)。 我正在尝试使用这个: CGSize possibleSize = [text sizeWithFont:[UIFont fontWithName:@"American Typewriter" size:16] constrainedToSize:CGSizeMake(self.collectionView.frame.size.width ,9999)

我试图“预测”标签的大小(宽度已知,只有高度)。 我正在尝试使用这个:

CGSize possibleSize = [text sizeWithFont:[UIFont fontWithName:@"American Typewriter" size:16]        
                       constrainedToSize:CGSizeMake(self.collectionView.frame.size.width ,9999)
                           lineBreakMode:NSLineBreakByWordWrapping];
这会给出非常不准确的结果(即使更改字体和大小也不会使效果更好,例如我得到的高度是30而不是80)

我已经读到其他人也没有得到好的结果。我用对了吗

我也尝试过:

UILabel *test=[[UILabel alloc] initWithFrame:self.collectionView.frame];
test.text=[dic objectForKey:@"text"];
test.font=[UIFont fontWithName:@"American Typewriter" size:12];
[test sizeToFit]; 

NSLog(@"%f",test.frame.size.height);
我必须知道高度会是多少,而这个方法甚至不接近。
有没有其他方法可以给出合理的结果呢?

@matt您已经了解了一些情况,但我要补充的是,在计算sizeWithFont之前,您应该将标签上的行数设置为0

您也可以尝试替换

CGSizeMake(self.collectionView.frame.size.width ,9999)

关键元素是“边界”,而不是框架

最后,确保您没有从[dic objectForKey:@“text”中得到任何东西

使用


sizeWithFont方法现在已被弃用,此新方法效果最佳

NSString *content = **Whatever your label's content is expected to be**
CGSize maximumLabelSize = CGSizeMake(self.label.frame.size.width, 9999);

NSDictionary *stringAttributes = [NSDictionary dictionaryWithObject:[UIFont fontWithName:@"American Typewriter" size:16] forKey: NSFontAttributeName];

CGSize expectedLabelSize = [content boundingRectWithSize:maximumLabelSize options:NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesLineFragmentOrigin attributes:stringAttributes context:nil].size;

CGFloat labelHeight = expectedLabelSize.height;
其中labelHeight是根据加载到标签中的文本量计算标签的高度


我希望这有帮助,干杯,吉姆。

首先,您需要允许UILabel是多行的,默认情况下它是一行(将行数设置为0)

第二,看起来您正在计算标签实际具有的较大字体的大小。考虑使用相同的大小以便正确计算。 <> P>此外,如果只支持iOS 7,则可以考虑使用<代码> Ssiz属性> <代码> >在iOS 7或<代码> BuffigRealthWist:选项:属性:上下文i/代码> -替换您从iOS 7使用的方法,并进一步进行文本大小计算。

最后(建议)如果你只需要高度值来设置标签的高度,也许你应该考虑使用自动布局(它会更容易处理)。默认情况下,

uIabable进入它的世界,其<代码>编号行>代码>设置为1。要使其高度根据需要而变化,请将其设置为
0
(这意味着“需要多少就有多少”)。第一个代码段的字体大小为16,而第二个代码段的字体大小为12。输入错误,或者这就是问题所在?我发现最有效的方法是使用一个虚拟(从未显示)UILabel对象,将文本放入其中,然后询问它有多大。(虽然我做了这件事已经有一段时间了,所以我不记得细节)。当内容字符串包含双换行符(\n\n)时,高度计算技术不会考虑它。实际上,我们得到的高度比实际需要的要低。
NSAssert(dic[@"text"]);
if ([dic[@"text"] isEqualToString:""]) {
    ; //empty string
}
NSString *content = **Whatever your label's content is expected to be**
CGSize maximumLabelSize = CGSizeMake(self.label.frame.size.width, 9999);

NSDictionary *stringAttributes = [NSDictionary dictionaryWithObject:[UIFont fontWithName:@"American Typewriter" size:16] forKey: NSFontAttributeName];

CGSize expectedLabelSize = [content boundingRectWithSize:maximumLabelSize options:NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesLineFragmentOrigin attributes:stringAttributes context:nil].size;

CGFloat labelHeight = expectedLabelSize.height;
yourLabel.numberOfLines = 0; // means - label can be multiline