简体   繁体   English

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

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

I've gone over what feels like every post about this method on SO. 我已经遍历了关于SO上此方法的每篇文章的感觉。 And a lot of the posters answers are to just put a really large value as the height constraint. 许多张贴者的答案是将一个非常大的值用作高度限制。

However, for me this is not working. 但是,对我来说这是行不通的。 This is my code: 这是我的代码:

//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;

I am using NIAttributedLabel , thought this shouldn't really have an effect because the method returning what I believe to be the wrong size is part of NSString . 我正在使用 NIAttributedLabel ,认为这实际上不会产生效果,因为返回我认为是错误大小的方法是 NSString一部分。

\n

I did read in Apple's documentation that the method will truncate the string sometimes, though I thought thats what the large height constraint was for 我确实在Apple的文档中读到,该方法有时会截断字符串,尽管我认为那是大高度限制的原因

EDIT: I've discovered that it is an issue with NIAttributedLabel , If I use a regular UILabel it works perfectly. 编辑:我发现这是NIAttributedLabel的问题,如果我使用常规的UILabel则可以正常工作。 Here are two source strings and corresponding screenshots, the first demonstrating my issue, the other deciding to be fine: 这是两个源字符串和相应的屏幕截图,第一个演示我的问题,另一个确定没问题:

"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 provide a new method for calculating height of NIAttributedLabel: NISizeOfAttributedStringConstrainedToSize() NimbusKit 1.0提供了一种计算NIAttributedLabel高度的新方法:NISizeOfAttributedStringConstrainedToSize()

I used to use sizeWithFont: and encounter same problem as you. 我曾经使用过sizeWithFont:,并且遇到过与您相同的问题。 Now the new method worked for me perfectly (I don't have any images in my attriubited label) 现在,新方法非常适合我(我的标签中没有任何图像)

You can't depend on NSString using the same glyph placement algorithm that CoreText will. 您不能使用与CoreText相同的字形放置算法来依赖NSString。 When sizing NIAttributedLabel it is recommended that you use the label's sizeToFit and sizeThatFits: methods because they use CoreText to determine the ideal size of the label. 调整NIAttributedLabel的大小时,建议您使用标签的sizeToFitsizeThatFits:方法,因为它们使用CoreText来确定标签的理想尺寸。

Please check with string @"T\\nT\\nT", it will only prints @"T\\nT\\n". 请检查字符串@“ T \\ nT \\ nT”,它只会打印@“ T \\ nT \\ n”。 It seems _textFrame only shows two visible lines instead of three. 似乎_textFrame仅显示两条可见线,而不是三条。

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

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