Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/113.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 对TextView的HTML解析(Swift 4版)_Ios_Swift_Swift4 - Fatal编程技术网

Ios 对TextView的HTML解析(Swift 4版)

Ios 对TextView的HTML解析(Swift 4版),ios,swift,swift4,Ios,Swift,Swift4,HtmlString=作为aaa 我有一个字符串扩展,它将字符串(html)解析为NSAttributedString 在swift 3上: guard let data = self.data(using: String.Encoding.utf16, allowLossyConversion: false) else { return nil } guard let htmlString = try? NSMutableAttributedString( data: data,

HtmlString=
作为aaa

我有一个字符串扩展,它将字符串(html)解析为NSAttributedString

在swift 3上:

guard let data = self.data(using: String.Encoding.utf16, allowLossyConversion: false) else { return nil }

guard let htmlString = try? NSMutableAttributedString(
    data: data,
    options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType],
    documentAttributes: nil) else { return nil }

let fullRange = NSRange(location: 0, length: htmlString.length);
    htmlString.enumerateAttribute(NSFontAttributeName, in: fullRange, options: NSAttributedString.EnumerationOptions.longestEffectiveRangeNotRequired, using: {
        (attributes, range, stop) -> Void in

        let newFont = UIFont.preferredFont(fontName: FontName.kHelveticaNeueLight, fontSize: FontSize.kTextView);
        htmlString.addAttribute(NSFontAttributeName, value: newFont, range: range);
    });

    htmlString.addAttribute(NSForegroundColorAttributeName, value:Color.kGrayDark, range:fullRange)

return htmlString;
在swift 4上:

    guard let data = self.data(using: String.Encoding.utf16, allowLossyConversion: false) else { return nil }
    guard let htmlString = try? NSMutableAttributedString(
        data: data,
        options: [NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.html],
        documentAttributes: nil) else { return nil }
    let fullRange = NSRange(location: 0, length: htmlString.length);

    htmlString.enumerateAttribute(NSAttributedStringKey.font, in: fullRange, options: NSAttributedString.EnumerationOptions.longestEffectiveRangeNotRequired, using: {
        (attributes, range, stop) -> Void in
        let newFont = UIFont(name: FontName.regular.value, size: FontSize.html)
        htmlString.addAttribute(NSAttributedStringKey.font, value: newFont ?? UIFont.systemFont(ofSize: FontSize.html), range: range);
    });

    htmlString.addAttribute(NSAttributedStringKey.foregroundColor, value:UIColor.html(), range:fullRange)
    return htmlString;
Swift 3结果:

Swift 4结果:


我错过了斯威夫特4号的边界。有什么建议吗?

错误是因为我将标签用于AttributeText,而不是TextView。它与textView配合使用效果很好

如果辅助功能字体不同于默认字体,则html中UILabel AttributeString的大小调整将很有趣。在ios 11.2上测试,您将发现它们