Search code examples
iosnsattributedstring

How to apply color for underline style in NSAttributedString (ios)?


Let's say i'm adding underline style in NSAttributedString:

[attrStr addAttribute:NSUnderlineStyleAttributeName value:@(NSUnderlineStyleSingle) range:range];

Is it possible to apply color (not only underline style) to underline without changing text color?


Solution

  • Yes. Use NSUnderlineColorAttributeName to set the underline color.

    [attrStr addAttribute:NSUnderlineColorAttributeName value:[UIColor redColor] range:range];