Cannot convert value of type NSAttributedString.DocumentAttributeKey to .DocumentReadingOptionKey

前端 未结 8 1990
鱼传尺愫
鱼传尺愫 2020-12-09 07:18

I found this string extension somewhere on SO that allows me to turn html code into an attributed string:

func html2AttributedString() -> NSAttributedStri         


        
8条回答
  •  臣服心动
    2020-12-09 08:03

    Swift 4.x & 5.x

    if let rtfPath = Bundle.main.url(forResource: "FileName", withExtension: "rtf") {
            do {
                let attributedString: NSAttributedString = try NSAttributedString(url: rtfPath, options: [NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.rtf], documentAttributes: nil)
                debugPrint(attributedString)
            } catch {
                print("Error while reading the file - \(error.localizedDescription)")
            }
     }
    

    Getting NSAttributedString from file will be changed in Swift 3.x which is as below:

    let attributedStringWithRtf: NSAttributedString = try NSAttributedString(url: rtfPath, options: [NSDocumentTypeDocumentAttribute:NSRTFTextDocumentType], documentAttributes: nil)
    

    all the other code same as Swift 4.x & 5.x.

    For more details please read the Apple document related to the NSAttributedStringDocumentType

提交回复
热议问题