如何在iOS-swift 3.0中显示UILabel中的html文本

问题描述 投票:0回答:4

我想在UICollectionView中显示来自服务器的数据。数据来自html字符串,如下所示

  <span> recognized <a 

href='#/userProfile/NZ==/XQ=='> Punit Kumar </a> for Delivers on time</span>

我希望它像这样展示

enter image description here

我怎么能实现这一目标?有人请帮帮我。

ios swift3
4个回答
3
投票

斯威夫特4

answer的更新

    extension NSAttributedString {
        internal convenience init?(html: String) {
            guard let data = html.data(using: String.Encoding.utf16, allowLossyConversion: false) else {
                // not sure which is more reliable: String.Encoding.utf16 or String.Encoding.unicode
                return nil
            }
            guard let attributedString = try? NSMutableAttributedString(data: data, options: [NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.html], documentAttributes: nil) else {
                return nil
            }
            self.init(attributedString: attributedString)
        }
    }

    label.attributedText = NSAttributedString(html: "<span> recognized <a href='#/userProfile/NZ==/XQ=='> Punit Kumar </a> for Delivers on time</span>")

2
投票

您需要将HTML格式的字符串转换为NSAttributedString,然后将该属性字符串设置为标签的文本。您可以使用以下扩展名将HTML格式的字符串转换为NSAttributedString

extension NSAttributedString {
    internal convenience init?(html: String) {
        guard let data = html.data(using: String.Encoding.utf16, allowLossyConversion: false) else {
            return nil
        }
        guard let attributedString = try? NSMutableAttributedString(data: data, options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], documentAttributes: nil) else {
            return nil
        }
        self.init(attributedString: attributedString)
    }
}

您可以使用以下扩展名:let attributedString = NSAttributedString(html: ""<html><body> Some html string </body></html>"")

然后,您可以使用UILabel将属性字符串设置为myLabel.attributedText = attributedString的文本


0
投票

也许是这样的

迅速

let htmlText = "<p>etc</p>"

if let htmlData = htmlText.data(using: String.Encoding.unicode) {
    do {
        let attributedText = try NSAttributedString(data: htmlData, options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], documentAttributes: nil)
    } catch let e as NSError {
        print("Couldn't translate \(htmlText): \(e.localizedDescription) ")
    }
}

Objective-C的

 NSString * htmlString = @"<html><body> Some html string </body></html>";
NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];

UILabel * myLabel = [[UILabel alloc] init];
myLabel.attributedText = attrStr;

的WebView

[detailView loadHTMLString:[NSString stringWithFormat:@"<div style='text-align:justify; font-size:13px;font-family:HelveticaNeue;color:#362932;'>%@",[model.dict valueForKey:@"description"]] baseURL:nil];

0
投票

斯威夫特5

let htmlText = "<html><body> Some html string </body></html>"

guard let data = dicData.desc.data(using: String.Encoding.unicode) else { return }

let attributedText = try! NSAttributedString(data: data, options: [.documentType:NSAttributedString.DocumentType.html], documentAttributes: nil)
© www.soinside.com 2019 - 2024. All rights reserved.