如何在NSAttributedString上设置字体大小

Mar*_*oon 24 objective-c nsattributedstring ios

编辑 - 这已被标记为重复,但正如我在下面所述,我正在寻找一个Swift解决方案.我发现的一切都是用Objective C写的.

我试图将HTML转换为NSAttributedString,但无法确定如何设置字体样式和大小.所有示例都在Objective 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
    }
Run Code Online (Sandbox Code Playgroud)

编辑......我尝试了几种方法.我无法让它发挥作用.我现在收到一条错误消息:表达式类型不明确,没有更多上下文.它指向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)
Run Code Online (Sandbox Code Playgroud)

Vik*_*put 32

  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
Run Code Online (Sandbox Code Playgroud)

字形

 let myAttribute = [ NSFontAttributeName: UIFont(name: "Chalkduster", size: 18.0)! ]
Run Code Online (Sandbox Code Playgroud)

阴影

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

强调

 let myAttribute = [ NSUnderlineStyleAttributeName: NSUnderlineStyle.StyleSingle.rawValue ]
Run Code Online (Sandbox Code Playgroud)

TEXTCOLOR

 let myAttribute = [ NSForegroundColorAttributeName: UIColor.blue ]
Run Code Online (Sandbox Code Playgroud)

BackGroud颜色

  let myAttribute = [ NSBackgroundColorAttributeName: UIColor.yellow ]
Run Code Online (Sandbox Code Playgroud)

  • 与:[/sf/answers/2258898281/](/sf/answers/2258898281/)有什么不同?已经在问题评论中建议了答案...请不要重复答案。 (2认同)

Osm*_*man 13

Swift 4您可以使用:

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


Ala*_*man 6

唯一对我有用的是将 HTML 包装在 a 中<span>并在那里应用样式:

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

试试这个扩展:

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
    }
}
Run Code Online (Sandbox Code Playgroud)

你像这样使用它:

someLabel.attributedString = someHTMLString.convertToAttributedString()
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你!

  • 谢谢它帮助了我! (3认同)
  • 它真的很有帮助!谢谢 (2认同)