17

I'm trying to create an attributed string with a strikethrough, however, this simple task seems to be harder to figure out than I anticipated. Here is what I have currently (which is not working). Thanks for the help!

NSAttributedString *theTitle = [[[NSAttributedString alloc] initWithString:@"strikethrough text" attributes:[NSDictionary dictionaryWithObjectsAndKeys:[NSColor whiteColor], NSForegroundColorAttributeName, NSUnderlinePatternSolid, NSStrikethroughStyleAttributeName, nil]] autorelease];
Luke
  • 11,426
  • 43
  • 60
  • 69
ambientdiscourse
  • 325
  • 1
  • 3
  • 13

4 Answers4

20

First, the value for NSStrikethroughStyleAttributeName must be an NSNumber, not a simple integer. Second, I think you have to include NSUnderlineStyleSingle:

...:[NSDictionary dictionaryWithObjectsAndKeys:
      ..., 
      [NSNumber numberWithInteger:NSUnderlinePatternSolid | NSUnderlineStyleSingle], 
      NSStrikethroughStyleAttributeName, 
      nil]...
Ole Begemann
  • 135,006
  • 31
  • 278
  • 256
  • This was very helpful. Thanks! Of note, you're missing a closing bracket to close out the `numberWithInteger:` message after `NSUnderlinePatternSolid | NSUnderlineStyleSingle` and before `, NSStrikethroughStyleAttributeName`. – morgant Jan 04 '13 at 22:26
17

You can also simply use:

NSAttributedString *theAttributedString;
theAttributedString = [[NSAttributedString alloc] initWithString:theString 
            attributes:@{NSStrikethroughStyleAttributeName:
            [NSNumber numberWithInteger:NSUnderlineStyleSingle]}];

Update :

Swift 2.0 version

let theAttributedString = NSAttributedString(string: theString, attributes: [NSStrikethroughColorAttributeName: NSUnderlineStyle.StyleSingle])
Arnaud Nelissen
  • 1,082
  • 11
  • 18
  • 2
    This is wrong, you can't assign style into color attribute. `[NSStrikethroughColorAttributeName: NSUnderlineStyle.StyleSingle]` – Edward Anthony Sep 09 '17 at 06:46
3
func addAttributes(attrs: [String : AnyObject], range: NSRange)

NSUnderlineStyleAttributeName: The value of this attribute is an NSNumber object containing an integer.

Since the NSUnderlineStyle enum's rawValue is Int Type, you should initialize a NSNumber Object with it

Swift2.1:

attrStr.addAttributes([NSStrikethroughStyleAttributeName: NSNumber(integer: NSUnderlineStyle.StyleSingle.rawValue)], range: NSMakeRange(x, y))

x is the location, the start of your text

y is the length of the text

Sangram Shivankar
  • 3,535
  • 3
  • 26
  • 38
Shawn Foo
  • 56
  • 4
  • Range is not Working when i write `NSRangeMake(0, 4)` that doesn't work , if i write `NSRangeMake(0, theAttributedString.lenght)` that work , but my requirment to put horizontal line on only First four char on String , how can i do this .?? – Dhiru May 17 '17 at 06:52
  • There is a known bug in iOS 10.3 where the strike through attribute won't work if applied in a range. As a workaround you can do: `attrStr.addAttributes([NSBaselineOffsetAttributeName: NSNumber(integerLiteral: 0)], range: NSMakeRange(x, y))` – jdev Aug 31 '17 at 08:31
1

Swift 4:

NSAttributedString(string: "test string", attributes: [.strikethroughStyle: NSUnderlineStyle.styleSingle.rawValue])

Swift 5:

NSAttributedString(string: "test string", attributes: [.strikethroughStyle: NSUnderlineStyle.single.rawValue])

Mind the fact that .strikethroughStyle and .underlineStyle expect an integer value (specifically an NSNumber), therefore we're using NSUnderlineStyle's .rawValue in the examples. (Setting NSUnderlineStyle causes unrecogognized selector exception)