带自定义下划线的NSAttributedString

时间:2019-03-19 14:21:20

标签: ios swift nsattributedstring

是否可以自定义NSAttributedString的下划线? 我想自定义:

  • 下划线颜色
  • 下划线宽度
  • 文字和下划线之间的距离

enter image description here

也许甚至可以在文本下创建自定义视图?

1 个答案:

答案 0 :(得分:0)

I don't think it is a perfect answer here, just a half proof.

enter image description here

I add some sample code here:

    let text = NSMutableAttributedString.init(string: "hello")

    let range = NSMakeRange(0, text.length)
    // add large fonts
    text.addAttribute(NSAttributedString.Key.backgroundColor, value: UIColor.red, range: range)
    text.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.blue, range: range)
    text.addAttribute(NSAttributedString.Key.font, value: UIFont.systemFont(ofSize: 72), range: range)


    // add underline
    text.addAttribute(NSAttributedString.Key.underlineStyle, value: NSNumber(value:  NSUnderlineStyle.double.rawValue), range: range)
    text.addAttribute(NSAttributedString.Key.underlineColor, value: UIColor.green, range: range)

    textView.attributedText = text