NSFontAttributeName not applied to NSAttributedString

前端 未结 2 701
粉色の甜心
粉色の甜心 2021-01-14 21:53

I\'m currently using this extension provided by this answer to convert html to strings within a UITextView. Everything works perfectly but for some strange reas

相关标签:
2条回答
  • 2021-01-14 22:50

    Here i have updated to SWIFT 3

    extension String {
    
        var utf8Data: Data? {
            return data(using: .utf8)
        }
    }
    
    extension Data {
        var attributedString: NSAttributedString? {
            do {
                return try NSAttributedString(data: self, options:[NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: String.Encoding.utf8.rawValue,NSForegroundColorAttributeName:UIColor.white,NSFontAttributeName:UIFont.systemFont(ofSize: 15)], documentAttributes: nil)
            } catch let error as NSError {
                print(error.localizedDescription)
            }
            return nil
        }
    
        var attributedStringHtml: NSMutableAttributedString? {
            do {
             let   attrStr = try NSMutableAttributedString(data: self, options: [NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType,NSCharacterEncodingDocumentAttribute:String.Encoding.utf8.rawValue], documentAttributes: nil)
                attrStr.addAttributes([NSFontAttributeName: UIFont.systemFont(ofSize: 17.0),NSForegroundColorAttributeName:UIColor.white], range: NSRange(location: 0, length: attrStr.length))
                return attrStr
            } catch let error as NSError {
                print(error.localizedDescription)
            }
            return nil
        }
    }
    

    Usage

     DispatchQueue.global(qos: .background).async {
                let attribut = self.sampleText.utf8Data?.attributedStringHtml
                DispatchQueue.main.async {
                    self.convertedTextLbl.attributedText = attribut
                }
            }
    
    0 讨论(0)
  • 2021-01-14 22:59

    I'm not sure why (probably because of having a range) but it works if you first create an NSMutableAttributedString and then you add the UIFont attributes:

    extension String {
    
        var html2AttributedString: NSMutableAttributedString? {
            guard
                let data = dataUsingEncoding(NSUTF8StringEncoding)
                else { return nil }
            do {
                let attrStr = try NSMutableAttributedString(data: data, options: [NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType,NSCharacterEncodingDocumentAttribute:NSUTF8StringEncoding], documentAttributes: nil)
                attrStr.addAttributes([NSFontAttributeName: UIFont.systemFontOfSize(17.0)], range: NSRange(location: 0, length: attrStr.length))
                return attrStr
            } catch let error as NSError {
                print(error.localizedDescription)
                return  nil
            }
        }
        var html2String: String {
            return html2AttributedString?.string ?? ""
        }
    }
    
    0 讨论(0)
提交回复
热议问题