How to use NSUnderlineStyle.PatternDot
Asked Answered
A

1

6

I am trying to use NSMutableAttributedString with dotted underline below is my code but none of the Patterns seem to work am I missing something ?

var str : NSMutableAttributedString = NSMutableAttributedString(string: "HelloWorld")
        str.addAttribute(NSUnderlineStyleAttributeName  , value: NSNumber(integer:(NSUnderlineStyle.PatternDot).rawValue), range: NSMakeRange(0, str.length))
Albion answered 20/1, 2015 at 18:41 Comment(3)
Do you get a compiler error or wrong output or what? Here is a similar question, does that help #25612216 ?Printing
https://mcmap.net/q/158628/-how-to-underline-a-uilabel-in-swiftSou
You should award the answer to the author below!Uncinariasis
N
21

You have to do it like this:

Xcode 10 • Swift 4.2 or later

yourLabel.attributedText = NSAttributedString(string: "Hello World !!!", attributes: [.underlineStyle: NSUnderlineStyle.patternDot.union(.single).rawValue])

Note: for older Swift syntax check edit history

Nope answered 20/1, 2015 at 20:8 Comment(4)
I had to do this in Swifth "uStyle = NSUnderlineStyle(rawValue: NSUnderlineStyle.PatternDot.rawValue | NSUnderlineStyle.StyleSingle.rawValue)" - I had a hard time figuring out how to "or" the two values :-)Uncinariasis
@DavidH it works for me as it is using Swift 2.1.1 dropbox.com/s/xio2279u5if7oqk/…Nope
I'm curious why rawValue is necessary here.Parhelion
rawValue is actually what the dictionary wants [.underlineStyle: 1] works whereas [.underlineStyle: .single] does notValenza

© 2022 - 2024 — McMap. All rights reserved.