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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/118.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 如何使OHAttributedLabel的高度与内容高度成比例?_Objective C_Ios_Cocoa Touch_Nsattributedstring - Fatal编程技术网

Objective c 如何使OHAttributedLabel的高度与内容高度成比例?

Objective c 如何使OHAttributedLabel的高度与内容高度成比例?,objective-c,ios,cocoa-touch,nsattributedstring,Objective C,Ios,Cocoa Touch,Nsattributedstring,我使用一个名为demoLbl的OhattributeLabel来显示带有格式化区域的文本。此标签使用Interface Builder进行布局,并连接到我的ViewController中的属性。将attributedText设置为标签后,我希望所有文本都显示在标签中。 如果我不调整标签的大小,那么文本将在标签的末尾被裁剪,因此剩下的文本将丢失 如果我使用[demoLbl sizeToFit];然后标签的高度比文本的高度大或小(大约10点,随文本的长度而变化),因此在视图的底部(滚动后)会出现空白

我使用一个名为demoLbl的OhattributeLabel来显示带有格式化区域的文本。此标签使用Interface Builder进行布局,并连接到我的ViewController中的属性。将attributedText设置为标签后,我希望所有文本都显示在标签中。
如果我不调整标签的大小,那么文本将在标签的末尾被裁剪,因此剩下的文本将丢失

如果我使用[demoLbl sizeToFit];然后标签的高度比文本的高度大或小(大约10点,随文本的长度而变化),因此在视图的底部(滚动后)会出现空白区域,加上标签的宽度增加了大约2点

如果在将原始文本(NSString)放入NSAttributedString并将其添加到标签的attributedText属性之前计算其高度,则计算的高度太小,无法将其设置为标签的高度

我是否可以应用一些技巧或技巧,以便根据NSAttribute字符串的高度调整标签的高度


PS:更具体地说,我想添加OHAttributedLabel作为标签,但我现在还不允许这样做。

你可以看看这个类别是否为你提供了更可靠的高度。

用法

attrLabel.frame.size.height = [attrLabel.attributedString boundingHeightForWidth:attrLabel.frame.size.width];

我是OHattributedLabel的作者

我最近对尺寸的计算做了一些修正。请检查它,它可能会解决您的问题

我还在
NSAttributedString+Attributes.h
中添加了一个名为
sizeConstrainedToSize:fitRange:
的方法,该方法返回给定NSAttributedString字符串的CGSize(与UIKit的
sizeWithFont:constrainedToSize:
的工作方式完全相同,但用于属性字符串和CoreText,而不是简单地Stinguikit)
实际上OHAttributedLabel的sizeThatFits:现在调用此方法本身。

我将此代码添加到OHAttributedLabel类的实现中:

// Toni Soler - 02/09/2011
// Overridden of the UILabel::sizeToFit method
- (void)sizeToFit
{
    // Do not call the standard method of the UILabel class, this resizes the frame incorrectly
    //[super sizeToFit];

    CGSize constraint = CGSizeMake(self.frame.size.width, 20000.0f);
    CGRect frame = self.frame;
    frame.size = [self sizeThatFits:constraint];
    [self setFrame:frame];
}
// End Toni Soler - 02/09/2011

谢谢Olivier分享你的代码

看来这类人就要做我想做的事了。如果我打印出NSAttributedString的高度,它会显示一个可能正确的值,但如果我将此高度指定给标签,它将不接受它。它比以前高,但没有显示文本所需的高。令人惊讶的是,打印出标签的高度显示了NSAttributed字符串的claculated和assigned高度。不知道错误在哪里。不幸的是,我无法检查您的更改是否有效。我被迫放弃使用NSAttributedString来交换使用带有样式文本的UIWebView。也许在未来的某个项目中,我能够测试它。如果发生这种情况,我会联系你。