2012-07-13 48 views
0

我已经走过了什么感觉就像这个方法在SO上的每一篇文章。 很多海报的答案都是把一个非常大的值作为高度限制。我如何正确使用NSString sizeWithFont constratinedToSize与NIAttributedLabel

但是,对我来说这是行不通的。这是我的代码:

//Create the contentLabel Label 
    CGSize size = [contentText sizeWithFont:[UIFont fontWithName:@"Helvetica" size:14] constrainedToSize:CGSizeMake(286, 9999) lineBreakMode:UILineBreakModeWordWrap]; 

    //Create the content label with its position 7 pixels below the title 
    contentLabel = [[NIAttributedLabel alloc] initWithFrame: 
        CGRectMake(7, titleContainerView.frame.origin.y + 7, 286, size.height)]; 

    //Set the provided text and the font 
    contentLabel.font = [UIFont fontWithName:@"Helvetica" size:14]; 
    contentLabel.numberOfLines = 0; 
    contentLabel.lineBreakMode = UILineBreakModeWordWrap; 
    contentLabel.text = contentText; 

我使用 NIAttributedLabel,认为这不应该真的有效果,因为该方法返回了我认为是错误的大小是 NSString一部分。

我没有苹果的文档中读取的方法有时会截断字符串,但我认为多数民众赞成在大高度约束是为

编辑: 我发现它是一个问题NIAttributedLabel,如果我使用普通的UILabel,它可以很好地工作。 这里有两个来源字符串和相应的截图,第一表明我的问题,其他的决定是罚款:

"Buying a Mobile\nHello - I'd like a Motorola Defy with a Smartphone 60 Plan.\nBroadband Problem\nMy Broadband’s out. I've tested the router and cables and ruled out my equipment. Is there a problem at your end?"

Screenshot 1

"Buying a Mobile\nI\'m Mrs Sina Manu-Harris. My account number is 156205169. I\'m going overseas in 6 months time on the 2nd of September and I\'d like to get organized in advance and buy a new mobile phone.\nBroadband Problem\nGood afternoon. It’s Mrs Sina Manu-Harris here. My account number is 156205169. My Broadband isn’t working. I’ve checked my network and phone cables and I've also checked my filters."

Screenshot 2

+0

描述它如何不工作。截图会很有帮助。你是否一步一步查看标签的最终高度是否确实是'sizeWithFont'中确定的高度?也许NIAttributedLabel实现中有一些怪癖。 – 2012-07-13 08:15:00

+0

我甚至有UILabel类似的问题... – Bharathi 2012-07-13 09:53:44

回答

0

你可以用” t依赖于NSString,使用与CoreText相同的字形放置算法。在确定NIAttributedLabel的大小时,建议您使用标签的sizeToFitsizeThatFits:方法,因为它们使用CoreText来确定标签的理想尺寸。

0

请检查字符串@“T \ nT \ nT”,它只会打印@“T \ nT \ n”。看起来_textFrame只显示两个可见的行而不是三个。

1

NimbusKit 1.0用于计算NIAttributedLabel的高度提供了一种新方法: NISizeOfAttributedStringConstrainedToSize()

我曾经使用sizeWithFont:和遇到相同的问题,因为你。 现在新的方法对我来说很完美了 (我没有任何图像在我attriubited标签)