簡體   English   中英

如何在 NSAttributedString 上設置字體大小

[英]How to set font size on NSAttributedString

編輯 - 這已被標記為重復,但正如我在下面所述,我正在尋找一個 Swift 解決方案。 我發現的所有內容都是用 Objective C 編寫的。

我正在嘗試將 HTML 轉換為 NSAttributedString,但無法確定如何設置字體樣式和大小。 所有示例都在我不熟悉的目標 C 中。如何將字體樣式/大小設置為 System 15(作為示例)

private func stringFromHtml(string: String) -> NSAttributedString? {
        do {
            let data = string.data(using: String.Encoding.utf8, allowLossyConversion: true)
            if let d = data {
                let str = try NSAttributedString(data: d,
                                                 options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType],                                                  documentAttributes: nil)
                return str
            }
        } catch {
        }
        return nil
    }

編輯......我嘗試了幾種方法。 我無法讓它工作。 我現在收到一條錯誤消息:沒有更多上下文的表達式類型不明確。 它指向 NSAttributedString 我嘗試了以下操作:

let myAttribute = [ NSForegroundColorAttributeName: UIColor.blue ]
let data = string.data(using: String.Encoding.utf8, allowLossyConversion: true)
  if let d = data {
     let str = try NSAttributedString(data: d,
                       attributes: myAttribute,
                       options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], documentAttributes: nil)
  let myString = "Swift Attributed String"
  let myAttribute = [ NSForegroundColorAttributeName: UIColor.blue ]
  let myAttrString = NSAttributedString(string: myString, attributes: myAttribute) 

  // set attributed text on a UILabel
  myLabel.attributedText = myAttrString

字體

 let myAttribute = [ NSFontAttributeName: UIFont(name: "Chalkduster", size: 18.0)! ]

影子

let myShadow = NSShadow()
myShadow.shadowBlurRadius = 3
myShadow.shadowOffset = CGSize(width: 3, height: 3)
myShadow.shadowColor = UIColor.gray
let myAttribute = [ NSShadowAttributeName: myShadow ]

下划線

 let myAttribute = [ NSUnderlineStyleAttributeName: NSUnderlineStyle.StyleSingle.rawValue ]

文字顏色

 let myAttribute = [ NSForegroundColorAttributeName: UIColor.blue ]

背景顏色

  let myAttribute = [ NSBackgroundColorAttributeName: UIColor.yellow ]

斯威夫特 4 您可以使用:

 let stringWithAttribute = NSAttributedString(string: selectedFilter ?? "",
                                                  attributes: [NSAttributedStringKey.font: UIFont.systemFont(ofSize: 14.0)])

唯一對我有用的是將 HTML 包裝在<span>並在那里應用樣式:

let modifiedFontString = "<span style=\"font-family: Lato-Regular; font-size: 14; color: rgb(60, 60, 60)\">" + originalHTMLString + "</span>"

試試這個擴展:

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 ?? ""
    }

    func convertToAttributedString() -> NSAttributedString? {
        let modifiedFontString = "<span style=\"font-family: Lato-Regular; font-size: 14; color: rgb(60, 60, 60)\">" + self + "</span>"
        return modifiedFontString.htmlToAttributedString
    }
}

你像這樣使用它:

someLabel.attributedString = someHTMLString.convertToAttributedString()

希望有幫助!

檢查 swift 5:

extension String {
    func getAttributedString<T>(_ key: NSAttributedString.Key, value: T) -> NSAttributedString {
       let applyAttribute = [ key: T.self ]
       let attrString = NSAttributedString(string: self, attributes: applyAttribute)
       return attrString
    }
}

您可以在此處查看更多屬性樣式: https : //developer.apple.com/documentation/foundation/nsattributedstring/key

使用

// Background Color
let attributedString = "YOUR_STRING".getAttributedString(.backgroundColor, value: UIColor.blue)

// Forground Color
let attributedString = "YOUR_STRING".getAttributedString(.foregroundColor, value: UIColor.red)

// Font
let attributedString = "YOUR_STRING".getAttributedString(.font, value: UIFont.boldSystemFont(ofSize: 15))

// Underline
let attributedString = "YOUR_STRING".getAttributedString(.underlineStyle, value: NSUnderlineStyle.single)

// Underline Color
let attributedString = "YOUR_STRING".getAttributedString(.underlineColor, value: UIColor.black)

    
// set attributed text on a UILabel
yourLabel.attributedText = attributedString
請檢查這個
var attrString = NSAttributedString(string: "labelText", attributes: [NSForegroundColorAttributeName: UIColor.red, NSFontAttributeName: UIFont(name: "system", size: 12)]) label.attributedText = attrString

首先,您需要將數據轉換為字符串,然后使用這段代碼

let  attributes = [
   NSForegroundColorAttributeName: UIColor.black,
            ]
try NSAttributedString(string: "", attributes: attributes)

你可以這樣設置

let attribute: [String : Any] = [NSFontAttributeName: UIFont.systemFont(ofSize: UIFont.systemFontSize, weight: UIFontWeightThin),
    NSForegroundColorAttributeName: UIColor.red]

let attributedText = NSAttributedString(string: "Your String", attributes: attribute)

暫無
暫無

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

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