簡體   English   中英

如何將 AttributedString 轉換為 NSMutableString Swift?

[英]How to convert AttributedString to NSMutableString Swift?

我有 HTML 並被轉換為 AttributedString。 現在,我需要更改生成的屬性字符串的字體,但我很難保留樣式(粗體、斜體或常規)。

我找到了解決方案,但問題是我不知道如何使用它。 他們使用 NSMutableAttributedString 作為擴展。 我在底部粘貼了我的代碼如何轉換和假設的解決方案。

謝謝你。

extension String {

 var htmlToAttributedString: NSAttributedString? {
        guard let data = data(using: .utf8) else { return NSAttributedString() }
        do {
            return try NSAttributedString(data: data, options: [.documentType: NSAttributedString.DocumentType.html, .characterEncoding:String.Encoding.utf8.rawValue], documentAttributes: nil)
        } catch {
            return NSAttributedString()
        }
    }

}






import Foundation

struct Service: Codable {

    var id: Int
    var name: String?
    var price: String?
    var description: String?
    var subtitle: String?
    var bodyPreview: String?
    var featuredImage: String? // For FindAll
    var imageList: [String]? // For FindByID

    private enum CodingKeys: String, CodingKey {
        case id
        case name
        case price
        case subtitle
        case description
        case bodyPreview = "body_preview"
        case featuredImage = "featured_image_url"
        case imageList = "images_url"
    }

}



class ServiceDetailViewController: UIViewController {


    private var service: Service?

    private func showServiceDetails() {
        detailLabel.attributedText = service?.description?.htmlToAttributedString
        collectionView.reloadData()
        startCollectionViewTimer()
    }

}

曼馬爾的解決方案:

extension NSMutableAttributedString {
    func setFontFace(font: UIFont, color: UIColor? = nil) {
        beginEditing()
        self.enumerateAttribute(
            .font,
            in: NSRange(location: 0, length: self.length)
        ) { (value, range, stop) in

            if let f = value as? UIFont,
                let newFontDescriptor = f.fontDescriptor
                    .withFamily(font.familyName)
                    .withSymbolicTraits(f.fontDescriptor.symbolicTraits) {

                let newFont = UIFont(
                    descriptor: newFontDescriptor,
                    size: font.pointSize
                )
                removeAttribute(.font, range: range)
                addAttribute(.font, value: newFont, range: range)
                if let color = color {
                    removeAttribute(
                        .foregroundColor,
                        range: range
                    )
                    addAttribute(
                        .foregroundColor,
                        value: color,
                        range: range
                    )
                }
            }
        }
        endEditing()
    }
}

讓 attriString = NSAttributedString(string:"attriString", 屬性: [NSAttributedString.Key.foregroundColor: UIColor.lightGray, NSAttributedString.Key.font: AttriFont])

您可以簡單地創建一個類似的擴展來返回一個可變的屬性字符串:

extension String {
    var htmlToMutableAttributedString: NSMutableAttributedString? {
        do {
            return try .init(data: Data(utf8), options: [.documentType:  NSAttributedString.DocumentType.html, .characterEncoding: String.Encoding.utf8.rawValue], documentAttributes: nil)
        } catch {
            print(error)
            return nil
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM