键入' String'没有会员' documentType' Swift 4.1

时间:2018-05-23 14:02:37

标签: nsattributedstring swift4.1

在Swift4.1和Xcode 9.3.1中我收到错误

  

键入' String'没有会员' documentType'

do {
        let modifiedFont = NSString(format:"<span style=\"font-family: '-apple-system', 'HelveticaNeue'; font-size: 16\">%@</span>" as NSString, string) as String

        let data = modifiedFont.data(using: String.Encoding.utf8, allowLossyConversion: true)
        if let d = data {

            let str = try NSAttributedString(data: d,
                                             options: [.documentType : NSAttributedString.DocumentType.html],
                                             documentAttributes: nil)
            return str
        }
    } 

This

1 个答案:

答案 0 :(得分:1)

这个解决方案对我有用。

 do {
        let modifiedFont = NSString(format:"<span style=\"font-family: '-apple-system', 'HelveticaNeue'; font-size: 16\">%@</span>" as NSString, string) as String

        let data = modifiedFont.data(using: String.Encoding.utf8, allowLossyConversion: true)
        if let d = data {
            let str = try NSAttributedString(data: d,
                                             options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], documentAttributes: nil)
            return str
        }
    } catch {
    }