gpt4 book ai didi

ios - 使用 uitextview 在 ios 版本 6 和 7 中显示 html 文本

转载 作者:行者123 更新时间:2023-11-28 22:05:50 27 4
gpt4 key购买 nike

我正在使用网络服务在我的 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 下面的 View 。

为了计算 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中显示fontsize的解决方案是什么。

提前致谢斯卡尼亚

最佳答案

很简单。我希望这对正在为 Uitextview 动态高度问题苦苦挣扎的人有所帮助。

[_txt_desc setValue:@"html text here" forKey:@"contentToHTMLString"];
[_txt_desc sizeThatFits:CGSizeMake(_txt_desc.frame.size.width,FLT_MAX)].height;

谢谢斯卡尼亚。

关于ios - 使用 uitextview 在 ios 版本 6 和 7 中显示 html 文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23992718/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com