swift 富文本粗斜体转换适用webHtml样式

2022-04-01  本文已影响0人  032c6843a285
/// 遍历斜体->转换字体
    func getObliqueFont(tempAttr:NSMutableAttributedString) ->NSMutableAttributedString{
        var needReplaceRanges: [NSRange] = []
        var needReplaceFonts: [AnyHashable] = []
        tempAttr.enumerateAttributes(in: NSMakeRange(0, tempAttr.length), options: NSAttributedString.EnumerationOptions.longestEffectiveRangeNotRequired) { (value, range, stop) in
            if let oblique = value[NSAttributedString.Key.obliqueness] as? CGFloat {
                print("有斜体-----\(oblique)")
                let oldFont = value[NSAttributedString.Key.font] as? UIFont
                let fontDescriptor = oldFont?.fontDescriptor
                let fontDescriptorSymbolicTraits = fontDescriptor?.symbolicTraits
                let isBold = (fontDescriptorSymbolicTraits!.rawValue & UIFontDescriptor.SymbolicTraits.traitBold.rawValue) != 0
                let newFont = generateSpecialFont(withBold: isBold, withItatic: (oblique == 0.3), fontSize: oldFont?.pointSize ?? 0)
                needReplaceRanges.append(NSValue(range: range) as! NSRange)
                if let newFont = newFont {
                    needReplaceFonts.append(newFont)
                }
            }
        }
        if (needReplaceRanges.count != 0) {
            for i in 0..<needReplaceRanges.count {
                let range = needReplaceRanges[i]
                let font = needReplaceFonts[i]
                tempAttr.addAttribute(.font, value: font, range: range)
             }
        }
        return tempAttr
    }
    
    ///生成webView支持展示的样式字体
     func generateSpecialFont(withBold bold: Bool, withItatic italic: Bool, fontSize: CGFloat) -> UIFont? {
        let font = UIFont.systemFont(ofSize: fontSize)
        var symbolicTraits = UIFontDescriptor.SymbolicTraits(rawValue: 0)
        if italic {
            symbolicTraits.insert(.traitItalic)
        }
        if bold {
            symbolicTraits.insert(.traitBold)
        }
         var specialFont: UIFont? = nil
         if let fontDescriptor = font.fontDescriptor.withSymbolicTraits(symbolicTraits) {
             specialFont = UIFont(descriptor: fontDescriptor, size: font.pointSize)
         }
         return specialFont
    }

使用

  
     let   tempAttr = getObliqueFont(tempAttr: self.textView.attributedText)
  let html =  tempAttr.codingToCompleteHtml()


 /// 将富文本编码成html标签
    func codingToCompleteHtml() -> String? {
        let exportParams: [NSAttributedString.DocumentAttributeKey : Any] = [NSAttributedString.DocumentAttributeKey.documentType: NSAttributedString.DocumentType.html, NSAttributedString.DocumentAttributeKey.characterEncoding: String.Encoding.utf8.rawValue]
        if let htmlData = try? self.rz.data(from: NSMakeRange(0, self.rz.length), documentAttributes: exportParams) {
            var string = String.init(data: htmlData, encoding: String.Encoding.utf8)
            string = string?.replacingOccurrences(of: "pt;", with: "px;")
            string = string?.replacingOccurrences(of: "pt}", with: "px}") 
            return string
        }
        return nil
    }
上一篇 下一篇

猜你喜欢

热点阅读