NSStrikethroughStyleAttributeName not working on device iOS 10.3 beta

NSStrikethroughStyleAttributeName is not working on device on iOS 10.3, but it works on simulator...

any thoughts ?

Replies

Try to wrap the NSUnderlineStyle enum in an NSNumber. It worked for me.

Example: attributes[NSStrikethroughStyleAttributeName] = NSNumber(value: NSUnderlineStyle.styleSingle.rawValue)

Add `NSBaselineOffsetAttributeName` to your attributes dictionary. The associated value needs to be an NSNumber containing a float.


The value will determine where your strikethrough appears in the text. (The default value is `0.0`; you'll need to change it to something else. I set this one to `1.5` and it seems to be about centered to the appended text, which doesn't have any strikethrough.)


Here's how I set up the attributes dictionary for the strikethrough string part in Swift 3.1.


let lineString = String(repeating: "\u{00A0}", count: numCharsToRepeat) 

let lineStringAttributes: [String:Any] = [
       NSBaselineOffsetAttributeName : NSNumber(floatLiteral: 1.5),
       NSStrikethroughStyleAttributeName : 1,
    ]


let attributedLineString = NSMutableAttributedString(string: lineString, attributes: lineStringAttributes)


// then I appended the line string to a prior attributed string that didn't use strikethrough - all worked fine!



As a side note, I wanted to create a line using the strikethrough, rather than actually striking through letters. It wouldn't work with a space (`" "`), but it did work with a Unicode Non-Breaking Space: `"\u{00A0}"`, as shown in line 1 of my code example.