什么是Objective-C中Android的“html-textview”等价物
Posted
技术标签:
【中文标题】什么是Objective-C中Android的“html-textview”等价物【英文标题】:What is the equivalent of Android's "html-textview" in Objective-C 【发布时间】:2018-04-19 11:20:33 【问题描述】:从后端获取 html 文本,在 uitableview 中使用带有自动布局的动态高度的 uilabel,我正在删除 html 标签,它在滚动和打开应用程序时会影响性能。
现在我尝试了 uiwebview,尝试了代码
dynamicWebview.delegate = self;
dynamicWebview.scrollView.contentInset = UIEdgeInsetsZero;
dynamicWebview.scrollView.scrollEnabled = false;
heightConstraint.constant = dynamicWebview.scrollView.contentSize.height;
[dynamicWebview loadHTMLString:[syntable valueForKey:@"extraDesc"] baseURL:nil];
并在 webviewdidfinishload 中
- (void)webViewDidFinishLoad:(UIWebView *)aWebView
CGRect frame = aWebView.frame;
frame.size.height = 1;
aWebView.frame = frame;
CGSize fittingSize = [aWebView sizeThatFits:CGSizeZero];
frame.size = fittingSize;
aWebView.frame = frame;
但在此我面临 uiwebview 和行高的动态高度问题,在重新加载单元格后,它会超出行高。在尝试了这个之后,我认为只有 uilabel 才是实现我所尝试的最好的方法。
我试过RTLabel 和TTTAttributedLabel RTLabel 支持 html 标签但无法在 IB 中使用,TTTAttributedLabel 支持 IB 但不支持 HTML 标签
在 android 中,他们使用了一个名为 html-textview 的库来实现这一点,它也在动态调整和处理 html 标签。
如果没有,是否还有其他 Objective c 方法可以做到这一点;我应该用什么。
编辑:我也尝试过像下面这样的 NSAttributedString,这也对性能影响很大。
NSString *extraDesc = [syntable valueForKey:@"extraDesc"];
NSAttributedString *attributedString = [[NSAttributedString alloc] initWithData:[extraDesc dataUsingEncoding:NSUnicodeStringEncoding] options:@ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType documentAttributes:nil error:nil];
splittext.attributedText = attributedString;
【问题讨论】:
使用一个名为 PTSMessenger cell 的库...它将解决您所有的动态单元格高度问题。我已经将这个库用于我的聊天应用程序......它很棒。 我下载了github.com/ppanopticon/PTSMessagingCell,它抛出错误“messageSize:”方法不在那个tableviewcell中。 +(CGRect)messageSize:(NSString*)message // return [message sizeWithFont:[UIFont systemFontOfSize:messageTextSize] constrainedToSize:CGSizeMake([PTSMessagingCell maxTextWidth], CGFLOAT_MAX) lineBreakMode:NSLineBreakByWordWrapping];返回 [消息 boundingRectWithSize:CGSizeMake([PTSMessagingCell maxTextWidth], CGFLOAT_MAX) 选项:NSStringDrawingUsesLineFragmentOrigin 属性:@NSFontAttributeName : [UIFont systemFontOfSize:messageTextSize]context:nil]; 把这个meathod放到PTSMessenger cell.m中,然后在PTSMessenger cell.h文件中声明这个meathod。 No known class method for selector 'maxTextWidth' 我在上面的代码中遇到了这个错误。 【参考方案1】:试试这个
NSAttributedString *attributedString = [[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUnicodeStringEncoding] options:@ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType documentAttributes:nil error:nil];
self.htmlLabel.attributedText = attributedString;
【讨论】:
我也试过了,它也不像 uiwebview 或 tttattributedlabel 那样流畅,忘记添加我的问题。马上更新,谢谢。以上是关于什么是Objective-C中Android的“html-textview”等价物的主要内容,如果未能解决你的问题,请参考以下文章
在 Swift 中使用 Objective-C 类别的正确方法是啥?