【发布时间】:2014-07-22 11:07:50
【问题描述】:
我正在使用 web 服务在我的 ios 应用程序中显示内容。
在网络上,数据使用富编辑文本框存储。
所以作为响应,我正在显示 HTML 文本。
现在我使用 UITextView 在我的应用程序中显示 html 文本。
[_txt_desc setValue:@"html text here" forKey:@"contentToHTMLString"];
[_txt_desc setFont:[UIFont fontWithName:custom_font_name size:is_ipad?18:14]];
_txt_desc.editable = NO;
_txt_desc.scrollEnabled = NO;
现在我需要计算 uitextview 的大小,以便我可以重新定位 UItextview 下方的视图。
为了计算 uitextview 的高度,我使用了下面描述的函数。
+ (CGFloat)measureHeightOfUITextView:(UITextView *)textView withHtmlString:(NSString *)htmlString
{
if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1)
{
// This is the code for iOS 7. contentSize no longer returns the correct value, so
// we have to calculate it.
//
// This is partly borrowed from HPGrowingTextView, but I've replaced the
// magic fudge factors with the calculated values (having worked out where
// they came from)
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setLineBreakMode:NSLineBreakByWordWrapping];
CGFloat measuredHeight=0;
if ([htmlString length]>0) {
textView.translatesAutoresizingMaskIntoConstraints = NO;
// NSAttributedString *attributedString = [[NSAttributedString alloc] initWithData: [htmlString dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType ,NSParagraphStyleAttributeName : paragraphStyle,NSFontAttributeName:textView.font} documentAttributes:nil error:nil];
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType ,NSFontAttributeName:[UIFont fontWithName:custom_font_name size:is_ipad?16:14]} documentAttributes:nil error:nil];
textView.attributedText = attributedString;
// Take account of the padding added around the text.
}
CGRect frame = textView.bounds;
NSLog(@"content size %@",NSStringFromCGSize(textView.frame.size));
UIEdgeInsets textContainerInsets = textView.textContainerInset;
UIEdgeInsets contentInsets = textView.contentInset;
CGFloat leftRightPadding = textContainerInsets.left + textContainerInsets.right + textView.textContainer.lineFragmentPadding * 2 + contentInsets.left + contentInsets.right;
CGFloat topBottomPadding = textContainerInsets.top + textContainerInsets.bottom + contentInsets.top + contentInsets.bottom;
frame.size.width -= leftRightPadding;
frame.size.height -= topBottomPadding;
NSString *textToMeasure = textView.text;
if ([textToMeasure hasSuffix:@"\n"])
{
textToMeasure = [NSString stringWithFormat:@"%@-", textView.text];
}
// NSString class method: boundingRectWithSize:options:attributes:context is
// available only on ios7.0 sdk.
NSDictionary *attributes = @{ NSFontAttributeName: textView.font, NSParagraphStyleAttributeName : paragraphStyle,NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType };
CGRect size = [textToMeasure boundingRectWithSize:CGSizeMake(CGRectGetWidth(frame), MAXFLOAT)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:attributes
context:nil];
measuredHeight = ceilf(CGRectGetHeight(size) + topBottomPadding);
return measuredHeight;
}
else
{
CGSize constraintSize = CGSizeMake(textView.frame.size.width, MAXFLOAT);
CGSize labelSize = [textView.text sizeWithFont:textView.font constrainedToSize:constraintSize lineBreakMode:NSLineBreakByWordWrapping];
int occurance = 0;
occurance=(int)[[htmlString componentsSeparatedByString:@"<p>"] count];
float height=0;
if(occurance>1){
height =occurance*(is_ipad?20:10);
}
occurance = (int)[[htmlString componentsSeparatedByString:@"<br/>"] count];
if(occurance>1){
height +=occurance*(is_ipad?20:10);
}
occurance = (int)[[htmlString componentsSeparatedByString:@"<br>"] count];
if(occurance>1){
height +=occurance*(is_ipad?20:10);
}
occurance = (int)[[htmlString componentsSeparatedByString:@"<h2>"] count];
if(occurance>1){
height +=occurance*(is_ipad?30:20);
}
occurance = (int)[[htmlString componentsSeparatedByString:@"<h3>"] count];
if(occurance>1){
height +=occurance*(is_ipad?30:20);
}
return labelSize.height+(is_ipad?30:20)+textView.font.pointSize+height;
}
}
现在它正在正确返回高度。
但现在的问题是,当它在低于 ios 7.0 的版本中显示文本时,它的文本大小是根据以编程方式给出的,而在 ios 7 中它不显示给定字体大小的文本。
那么在ios 7中字体大小显示的解决方案是什么。
提前致谢 博斯卡尼亚
【问题讨论】:
标签: ios ios7 ios6 uitextview