2017-08-30 383 views
1

编辑 - 这已被标记为重复,但正如我下面所述,我正在寻找一个Swift解决方案。我找到的所有东西都写在Objective C中。如何在NSAttributedString上设置字体大小

我想将HTML转换为NSAttributedString,但无法计算如何设置字体样式和大小。所有的例子都在目标C这我不擅长,我如何设置字体样式/大小系统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) 
+2

希望这有助于:https://stackoverflow.com/questions/24666515/我怎么做 - 使归属字符串使用swift – Amit

+1

https://stackoverflow.com/questions/41412963/swift-change-font-on-an-html-string-that-has-its-自己的风格这一个。 – Larme

+0

@Larme ...我使用您提供的链接中的扩展示例解决了它。可悲的是我仍然不明白。我相信我不得不创建一个NSMutableAttributedString,而我只是想改变一个Attributed String?谢谢! –

回答

4
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 ] 

TEXTCOLOR

let myAttribute = [ NSForegroundColorAttributeName: UIColor.blue ] 

化背景颜色

let myAttribute = [ NSBackgroundColorAttributeName: UIColor.yellow ] 
+0

有什么不同:[https://stackoverflow.com/a/32269975/5575752](https://stackoverflow.com/a/32269975/5575752)?已经建议在问题评论中回答...请不要复制答案。 –

+0

如果有类似的问题要求,则必须提供重复的链接或评论,而不是答案的副本。谢谢 –

0
请检查这一项
var attrString = NSAttributedString(string: "labelText", attributes: [NSForegroundColorAttributeName: UIColor.red, NSFontAttributeName: UIFont(name: "system", size: 12)]) 
label.attributedText = attrString 
0

你可以设置这样

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

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

首先,你需要把数据转换为字符串,然后使用此代码peice的

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

谢谢。我收到以下错误: 无法将[String:NSObject]类型的值转换为预期的参数类型Autoreleasing不安全的指针错误指向我按照您的建议添加的属性变量。 –

+0

您正在使用哪个版本的swift? – Sakshi

+0

Swift 3.0 ...... –

相关问题