在 swift 4 中将 html 字符串设置为标签时,如何使用常规字体和粗体字体设置自定义字体?

How to set custom font with regular and bold font while setting html string to label in swift 4?

我从 API 响应中得到 HTML 格式的字符串,所以我需要将它设置为标签,同时保持自定义字体(从我的应用程序开始)并应用样式(粗体,常规等)到标签。

我使用了一个扩展程序,可以将 HTML 字符串转换为带有换行符等的常规字符串。但是,我可以在此处设置字体,但只有一种字体,并且仅以常规字体显示,所以整个标签都是一种字体,我想要的是将粗体设置为粗体 HTML 部分,将常规设置为常规 HTML part/tag.

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()
        }
    }
    var htmlToString: String {
        return htmlToAttributedString.string
    }
}

//set converted html to string here
let whyAttendAttributedText: NSMutableAttributedString = NSMutableAttributedString(attributedString: attendData.whyAttendData?.desc?.htmlToAttributedString ?? NSAttributedString())

//set font here   
whyAttendAttributedText.addAttributes([NSMutableAttributedString.Key.font: CommonSettings.shared.getFont(type: .regular, size: descriptionLabel.font.pointSize), NSMutableAttributedString.Key.foregroundColor: UIColor.white], range: NSMakeRange(0, whyAttendAttributedText.length))

我想为文本设置粗体和常规字体,但由于我只设置了一种字体,我无法得到结果,有没有办法像 HTML 那样设置粗体和常规字体字符串?

可以使用以下方法将粗体和其他不同样式应用于文本。

extension String {

    func attributedString(with style: [NSAttributedString.Key: Any]? = nil,
                          and highlightedText: String,
                          with highlightedTextStyle: [NSAttributedString.Key: Any]? = nil) -> NSAttributedString {

        let formattedString = NSMutableAttributedString(string: self, attributes: style)
        let highlightedTextRange: NSRange = (self as NSString).range(of: highlightedText as String)
        formattedString.setAttributes(highlightedTextStyle, range: highlightedTextRange)
        return formattedString
    }
}

输入:"This is a test message"

预期输出:"This is a test message"

这可以通过以下方式实现。

let sampleInput = "This is a test message"
let boldtext = "test"

let output = sampleInput.attributedString(with: [.font: UIFont.systemFont(ofSize: 12.0, weight: .regular)],
                             and: boldtext, with: UIFont.systemFont(ofSize: 12.0, weight: .bold))

可以使用不同的属性键来应用不同的样式。希望这有帮助。

让我们假设您的字符串在解析 HTML 字符串后是:"This is your HTML string"

创建attributed string,

let attrStr = NSMutableAttributedString(string: "This is your HTML string")

添加 UIFont 属性值为 System-Regular,

attrStr.addAttribute(.font, value: UIFont.systemFont(ofSize: 14.0, weight: .regular), range: NSRange(location: 0, length: attrStr.length))

每当向 attributed string 添加 attribute 时,我们需要提供 stringrange,我们希望在其中反映 attribute

由于我们需要整个stringRegular字体,所以range计算为整个stringlength.

现在,将值为 System-BoldUIFont 属性添加到 string 的一部分,假设我们制作 HTML 加粗,

attrStr.addAttribute(.font, value: UIFont.systemFont(ofSize: 14.0, weight: .bold), range: (attrStr.string as NSString).range(of: "HTML"))

我们计算了整个字符串中 HTML 个单词的 range

同样,您可以将任何 attributes 添加到 string 中,给出相关的 range 值。

输出:This is yourHTMLstring

编辑-1:

要计算 <b> to </b>range 您需要手动计算。

示例:

let str = "This <b>is your HTML</b> string"
let range1 = (str as NSString).range(of: "<b>")
let range2 = (str as NSString).range(of: "</b>")
let requiredRange = NSRange(location: range1.location, length: range2.location + range2.length - range1.location)

以上示例适用于 string.

<b>/</b> 的单个实例

编辑-2:

string 包含 <b>/</b> 的多个实例时:

let htmlStr = "This is an <b>HTML</b> parsed <b>string</b>"
let arr = htmlStr.components(separatedBy: "</b>")
let attrStr = NSMutableAttributedString()
for str in arr {
    if !str.isEmpty {
        let range1 = (str as NSString).range(of: "<b>")
        let requiredRange = NSRange(location: range1.location, length: str.count - range1.location)

        let formattedStr = NSMutableAttributedString(string: str)
        formattedStr.addAttribute(.font, value: UIFont.systemFont(ofSize: 14.0, weight: .bold), range: requiredRange)
        attrStr.append(formattedStr)
        attrStr.append(NSAttributedString.init(string: "</b>", attributes: [.font : UIFont.systemFont(ofSize: 14.0, weight: .bold)]))
    }
}
self.label.attributedText = attrStr

输出:This is an<b>HTML</b>parsed<b>string</b>

这应该有帮助:

extension String {

func attributedString(withRegularFont regularFont: UIFont, andBoldFont boldFont: UIFont) -> NSMutableAttributedString {
    var attributedString = NSMutableAttributedString()
        guard let data = self.data(using: .utf8) else { return NSMutableAttributedString() }
        do {
            attributedString = try NSMutableAttributedString(data: data,
                                                             options: [.documentType: NSAttributedString.DocumentType.html,
                                                                       .characterEncoding:String.Encoding.utf8.rawValue],
                                                             documentAttributes: nil)
            let range = NSRange(location: 0, length: attributedString.length)
            attributedString.enumerateAttribute(NSAttributedString.Key.font, in: range, options: .longestEffectiveRangeNotRequired) { value, range, _ in
                let currentFont: UIFont         = value as! UIFont
                var replacementFont: UIFont?    = nil

                if currentFont.fontName.contains("bold") || currentFont.fontName.contains("Bold") {
                    replacementFont             =   boldFont
                } else {
                    replacementFont             =   regularFont
                }

                let replacementAttribute        =   [NSAttributedString.Key.font:replacementFont!]
                attributedString.addAttributes(replacementAttribute, range: range)
            }
        } catch let e {
            print(e.localizedDescription)
        }
       return attributedString
}
}