簡體   English   中英

更改歸因文本字體大小並保持格式

[英]Change Attributed Text Font Size and Keep Formatting

我正在從JSON接收的文本中創建屬性字符串。 然后,我將該文本放入文本視圖。

    let descriptionVC = BenefitDescriptionViewController()

    let attributedString = try! NSAttributedString(data: descriptionString.data(using: String.Encoding.unicode, allowLossyConversion: true)!, options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], documentAttributes: nil)

    descriptionVC.labelString = attributedString

descriptionVC

override func viewDidLoad() {
        super.viewDidLoad()

        // print("Attributed String: \(labelString)")

        if let descriptionString = labelString {
            descriptionTextView.attributedText = descriptionString
            descriptionTextView.textColor = UIColor.white
            descriptionTextView.font = UIFont.systemFont(ofSize: 17.0)
        }
    }

我想保持格式,例如某些文本為粗體,但使字體更大。 當我嘗試增加字體大小時,所有其他格式都消失了。

這是我打印labelString時得到的。

在此處輸入圖片說明

您需要找到范圍並為該范圍添加屬性。 假設“這是一個測試”。 在此文本中,您要將“ This”顯示為紅色和粗體,將“ Test”顯示為字體大小40和藍色,然后找到“ This”的范圍並為該范圍添加屬性(粗體,紅色)。 再次找到“測試”的范圍,並為該范圍添加屬性(字體大小,藍色)。

暫無
暫無

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

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