iOS label显示html,解决超出字符串不显示...问题

使用到NSAttributedString
通过它就可以设置加载HTML。iOS7之后才可以使用:

- (nullableinstancetype)initWithData:(NSData *)dataoptions:(NSDictionary<NSString *, id> *)options
  documentAttributes:(NSDictionary<NSString *, id> * __nullable* __nullable)dict
  error:(NSError **)errorNS_AVAILABLE(10_0, 7_0);

其中,options中的指定key为:

UIKIT_EXTERN NSString * const NSDocumentTypeDocumentAttribute NS_AVAILABLE(10_0, 7_0);  

时,它可以选择的值有:

UIKIT_EXTERN NSString * const NSPlainTextDocumentType NS_AVAILABLE(10_0, 7_0);
UIKIT_EXTERN NSString * const NSRTFTextDocumentType NS_AVAILABLE(10_0, 7_0);
UIKIT_EXTERN NSString * const NSRTFDTextDocumentType NS_AVAILABLE(10_0, 7_0);
UIKIT_EXTERN NSString * const NSHTMLTextDocumentType NS_AVAILABLE(10_0, 7_0);

其中,NSHTMLTextDocumentType就是设置要加载HTML了。

NSData *data = [htmlString dataUsingEncoding:NSUnicodeStringEncoding];
NSDictionary *options = @{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType};
NSAttributedString *html = [[NSAttributedString alloc]initWithData:data
                                                            options:options
                                                 documentAttributes:nil
                                                              error:nil];
self.htmlLabel.attributedText = html;

其中会有一个小问题,就是填充到label中超出字符串后面"..."不显示了,查看富文本属性会看到,转化过程中自动填充了段落属性

PittWong长得帅!{
    NSColor = "kCGColorSpaceModelRGB 0 0 0 1 ";
    NSFont = "<UICTFont: 0x7f90d56a49b0> font-family: \"Times New Roman\"; font-weight: normal; font-style: normal; font-size: 12.00pt";
    NSKern = 0;
    NSParagraphStyle = "Alignment 4, LineSpacing 0, ParagraphSpacing 0, ParagraphSpacingBefore 0, HeadIndent 0, TailIndent 0, FirstLineHeadIndent 0, LineHeight 15/0, LineHeightMultiple 0, LineBreakMode 0, Tabs (\n), DefaultTabInterval 36, Blocks (\n), Lists (\n), BaseWritingDirection 0, HyphenationFactor 0, TighteningForTruncation NO, HeaderLevel 0";
    NSStrokeColor = "kCGColorSpaceModelRGB 0 0 0 1 ";
    NSStrokeWidth = 0;
}他啦啦啦啦{
    NSColor = "kCGColorSpaceModelRGB 1 0 0 1 ";
    NSFont = "<UICTFont: 0x7f90d56a49b0> font-family: \"Times New Roman\"; font-weight: normal; font-style: normal; font-size: 12.00pt";
    NSKern = 0;
    NSParagraphStyle = "Alignment 4, LineSpacing 0, ParagraphSpacing 0, ParagraphSpacingBefore 0, HeadIndent 0, TailIndent 0, FirstLineHeadIndent 0, LineHeight 15/0, LineHeightMultiple 0, LineBreakMode 0, Tabs (\n), DefaultTabInterval 36, Blocks (\n), Lists (\n), BaseWritingDirection 0, HyphenationFactor 0, TighteningForTruncation NO, HeaderLevel 0";
    NSStrokeColor = "kCGColorSpaceModelRGB 1 0 0 1 ";
    NSStrokeWidth = 0;
}

移除段落属性...就能正常显示了

NSMutableAttributedString *string = [[NSMutableAttributedString alloc]initWithAttributedString: html];

[string removeAttribute:NSParagraphStyleAttributeName range: NSMakeRange(0, string.length)];

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,886评论 18 139
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,223评论 4 61
  • 好久没画水彩了,拿起笔来,手一直在哆嗦。 果真是不能放下啊,一放手就生。 临摹的极光丑到无敌! 还是临摹个小画适应下。
    猫饼干阅读 263评论 0 0
  • 今天,我们理解一项比较火爆的技术。它能在我们需要加载局部页面更新数据时,或者是需要大量的局部数据刷新或加载。不必重...
    风清扬101阅读 294评论 0 1
  • 最近公司项目在用sqlserver2012,因数据量非常大,程序跑起来临时表的数据量高达40多G,严重影响C盘的存...
    shakesbears阅读 9,502评论 0 0