19

How can I use strike-through font in objective C??? More specifically in UITableViewCell

cell.textLabel.text = name;
cell.detailTextLabel.text = quantity ;
cell.XXX = ??
Josh Darnell
  • 11,304
  • 9
  • 38
  • 66
wal
  • 2,044
  • 3
  • 18
  • 22
  • 1
    can you select the correct answer in order not to mislead users who come over to find solution for striking trough text – Marin Todorov Mar 07 '14 at 10:44

10 Answers10

50

this is Marin, the author of the attributed strings chapter in "iOS6 by Tutorials".

Since iOS6 there is actually a native support for a bunch of different text attributes, including strike-trough.

Here's a short example, which you can use for your cell text label:

NSDictionary* attributes = @{
  NSStrikethroughStyleAttributeName: [NSNumber numberWithInt:NSUnderlineStyleSingle]
};

NSAttributedString* attrText = [[NSAttributedString alloc] initWithString:@"My Text" attributes:attributes];
cell.textLabel.attributedText = attrText;

That's all. Good luck!

Marin Todorov
  • 6,377
  • 9
  • 45
  • 73
10

EDIT: This answer is out of date as of iOS 6. Please see the more recent answers below

There is not native support for strike-through or underline fonts. You have to draw the lines yourself over the views for the labels.

This is silly since the font inspector for IB has options to set strike-through and underline, but these are promptly ignored if you try to set them.

Brandon Bodnar
  • 8,202
  • 2
  • 36
  • 42
  • Is there any documentation somewhere to back this up? (Not that I doubt it, just wondering.) – Tim Mar 22 '10 at 01:39
  • @Tim, Other than that NSAttributedString and the Attributed String Programming Guide don't exist in the iPhone Dev References, no. The lack of documentation is the best I can do. – Brandon Bodnar Mar 22 '10 at 01:43
  • 1
    I've ever tried to change to strike-through in IB, but it was ignored as you said. – wal Mar 22 '10 at 01:48
  • 1
    IB is showing fonts and settings available on Mac OS X, but this has little or nothing to do with the iPhone's capabilities. It's IB not being aware enough of the iPhone-- the iPhone simply doesn't have strikethrough fonts to use. – Tom Harrington Mar 22 '10 at 04:00
  • 2
    It is now possible with iOS 6. See Ican's answer. – Steve Moser Jan 08 '13 at 19:20
4

here is how you strikethrough your label. But remember, it only works after ios 6.0

NSNumber *strikeSize = [NSNumber numberWithInt:2]; 

NSDictionary *strikeThroughAttribute = [NSDictionary dictionaryWithObject:strikeSize 
forKey:NSStrikethroughStyleAttributeName];

NSAttributedString* strikeThroughText = [[NSAttributedString alloc] initWithString:@"Striking through it" attributes:strikeThroughAttribute];

strikeThroughLabel.attributedText = strikeThroughText;
amgalanBE
  • 41
  • 3
4
CGRect frame = sender.titleLabel.frame;
UILabel *strikethrough = [[UILabel alloc] initWithFrame:frame];
strikethrough.opaque = YES;
strikethrough.backgroundColor = [UIColor clearColor];
strikethrough.text = @"------------------------------------------------";
strikethrough.lineBreakMode = UILineBreakModeClip;
[sender addSubview:strikethrough];
Scott Bossak
  • 2,471
  • 20
  • 17
3

1-Get the size of text which needs to strikethrough

CGSize expectedLabelSize = [string sizeWithFont:cell.titleLabel.font constrainedToSize:cell.titleLabel.frame.size lineBreakMode:UILineBreakModeClip];

2-Create an line and add it to the text

UIView *viewUnderline = [[UIView alloc] init];
viewUnderline.frame = CGRectMake(20, 12, expectedLabelSize.width, 1);
viewUnderline.backgroundColor = [UIColor grayColor];
[cell addSubview:viewUnderline];
[viewUnderline release]; 
VisioN
  • 143,310
  • 32
  • 282
  • 281
Zubair
  • 5,833
  • 3
  • 27
  • 49
2

Strikethrough is possible by using NSStrikeThroughAttributes and attributedText of UILabel. Here is the solution in Swift

    let strikeThroughAttributes = [NSStrikethroughStyleAttributeName : 1]
    let strikeThroughString = NSAttributedString(string: "Text of Label", attributes: strikeThroughAttributes)
    strikeThroughLabel.attributedText = strikeThroughString
Nick Wargnier
  • 1,461
  • 14
  • 8
2
NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:@"Your String here"];
[attributeString addAttribute:NSStrikethroughStyleAttributeName
                    value:@2
                    range:NSMakeRange(0, [attributeString length])];
yourLabel.attributedText = attributeString;
Hardik Mamtora
  • 1,642
  • 17
  • 23
1

Have you thought about finding your own strikethrough font and loading it yourself? It isn't that hard, just add the UIFont to your Info.plist file and put the name of the font in there. Then you can manually set the text to that new strikethrough font.

See this post on loading custom font.

Can I embed a custom font...

Community
  • 1
  • 1
Bill Burgess
  • 14,054
  • 6
  • 49
  • 86
0
UIView *strikeView = [[UIView alloc] initWithFrame:ccr(0, 0, myLabel.bounds.size.width, 1)];
strikeView.backgroundColor = [UIColor redColor];
strikeView.center = ccp(myLabel.bounds.size.width/2, myLabel.bounds.size.height/2);
[myLabel addSubview:strikeView];
Reporter
  • 3,897
  • 5
  • 33
  • 47
-2

This will strike through the whole cell. In case you need it to look like completed task:

CGSize size = cell.contentView.frame.size; // you'll draw the line in whole cell.

UIView *line = [[UIView alloc] initWithFrame:CGRectMake(15,size.height / 2,size.width - 30, 1)];
line.backgroundColor = [UIColor grayColor]; // set your preferred color
[cell addSubview:line];

You may indicate some other value in CGRectMake instead of 15 - it's offset by X. In this case you'd better then decrease width by doubled value (in my case it's 15*2 = 30) in order it look nice.