3

Is there a way to customize the underline of a NSAttributedString? I would like to customize:

  • Underline color
  • Underline width
  • Distance between the text and the underline

enter image description here

Maybe even creating a custom view under text?

Luda
  • 7,282
  • 12
  • 79
  • 139
  • 2
    No, `NSAttributedString` does not support custom underlining. – rmaddy Mar 19 '19 at 14:33
  • I don't know of a way provided by NSAttributedString, but what about just using some views to underline the text and placing them at whatever distance you need. – surToTheW Mar 19 '19 at 14:34
  • You can change underline color and underline style(double line or single line etc.) – E.Coms Mar 19 '19 at 14:56
  • You'd have to play with CoreText to do so, override drawRect, TextStorage or other parts like that. – Larme Mar 19 '19 at 15:00
  • @Larme, can you please provide a code example and output in an answer? – Luda Mar 19 '19 at 18:57
  • https://stackoverflow.com/questions/25523910/customize-underline-pattern-in-nsattributedstring-ios7 – Larme Mar 19 '19 at 18:59
  • @Larme Thanks man, that is exactly what I was looking for. https://stackoverflow.com/a/53330387/1341180. Post it as an answer and I'll approve. – Luda Mar 20 '19 at 09:00
  • Since it's the same question, upvote the answers there, and mark your question as duplicate if you think that another user would have better chances to find yours and not the other one (and then see the duplicate link). If not, just delete yours. – Larme Mar 20 '19 at 09:03
  • 1
    Possible duplicate of [Customize underline pattern in NSAttributedString (iOS7+)](https://stackoverflow.com/questions/25523910/customize-underline-pattern-in-nsattributedstring-ios7) – Luda Mar 20 '19 at 14:33

1 Answers1

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
E.Coms
  • 11,065
  • 2
  • 23
  • 35
  • Have you forgotten to post the code to this answer? – Wai Ha Lee Mar 19 '19 at 18:27
  • His question has no answer. It’s a proof for my comment. – E.Coms Mar 19 '19 at 18:45
  • There is no code. I changed it in text view preview of IB – E.Coms Mar 19 '19 at 18:47
  • Hey @E.Coms, Thanks for taking the time to answer. The thing is that the text is dynamic and has to be set from the code. Any idea how I can achieve this functionality with code? – Luda Mar 19 '19 at 18:56
  • @E.Coms, hey again. What you suggesting is making the fonts bigger so that the underline will be bigger proportionally to the text. What I am looking for is preserving the text side and making only the underline thicker and different distances from the text. Not with preset values, but with custom values. – Luda Mar 20 '19 at 07:47
  • I make it bigger just for people can see it. You don’t need to make it bigger. Just use last two line code. I said there is no way to adjust the distance and you only can choose different style. – E.Coms Mar 20 '19 at 11:48