繁体   English   中英

我如何在NIAttributedLabel中正确使用NSString sizeWithFont constratinedToSize

[英]How do I properly use NSString sizeWithFont constratinedToSize with NIAttributedLabel

我已经遍历了关于SO上此方法的每篇文章的感觉。 许多张贴者的答案是将一个非常大的值用作高度限制。

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

//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一部分。

\n

我确实在Apple的文档中读到,该方法有时会截断字符串,尽管我认为那是大高度限制的原因

编辑:我发现这是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?"

屏幕截图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."

屏幕截图2

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

我曾经使用过sizeWithFont:,并且遇到过与您相同的问题。 现在,新方法非常适合我(我的标签中没有任何图像)

您不能使用与CoreText相同的字形放置算法来依赖NSString。 调整NIAttributedLabel的大小时,建议您使用标签的sizeToFitsizeThatFits:方法,因为它们使用CoreText来确定标签的理想尺寸。

请检查字符串@“ T \\ nT \\ nT”,它只会打印@“ T \\ nT \\ n”。 似乎_textFrame仅显示两条可见线,而不是三条。

暂无
暂无

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

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