【发布时间】:2016-05-10 12:30:52
【问题描述】:
我只是像新闻推送一样开发和应用。一些单元格视图我必须将NSAttributedString 设置为 textview 并获取 textview 的确切高度。
在我的NSAttributedString 中有 HTML 内容。我必须在 textview 中设置,因为它在 web 视图中花费了太多时间。
问题是有时我得到了 textview 的完美高度,而有时我没有得到 textview 的高度。因为NSAttributedString 有时它会考虑字体高度,有时它不会考虑字体高度。
如果您查看我所做的代码,您可以了解更多。 templbl2 是 UITextview temptext2 是 UIView。
这是我的代码:
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *str3 = [NSString stringWithFormat:@"<span style=\"font-family: Frank-regular; font-size: 13\">%@</span>", strTerms];
NSAttributedString * attrStr2 = [[NSAttributedString alloc] initWithData:[str3 dataUsingEncoding:NSUTF8StringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSFontAttributeName : [UIFont fontWithName:@"Frank-regular" size:13.0]} documentAttributes:nil error:nil];
templbl2.attributedText = attrStr2;
[templbl2 sizeToFit];
[self textViewHeightForAttributedText:attrStr2];
}
- (CGFloat)textViewHeightForAttributedText:(NSAttributedString *)text
{
CGFloat width = [UIScreen mainScreen].bounds.size.width; // whatever your desired width is
CGRect paragraphRect =
[text boundingRectWithSize:CGSizeMake(300.f, CGFLOAT_MAX)
options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading)
context:nil];
temptext2.frame = CGRectMake(0, 0, width, paragraphRect.size.height+60);
return paragraphRect.size.height;
}
【问题讨论】:
-
设置你的 textview 字体大小 13 和字体名称 Frank-regular?
-
我已经设置好了。我只能在属性字符串之前设置。之后我什么都做不了。一旦我设置,则没有属性字符串和大小的平均值。因为属性字符串是动态的。我不知道那里有什么 html 内容。
标签: ios uiview uitextview nsattributedstring