Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strike-through font in objective C

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

cell.textLabel.text = name;
cell.detailTextLabel.text = quantity ;
cell.XXX = ??
like image 663
wal Avatar asked Mar 22 '10 01:03

wal


5 Answers

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!

like image 177
Marin Todorov Avatar answered Sep 29 '22 17:09

Marin Todorov


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.

like image 36
Brandon Bodnar Avatar answered Sep 29 '22 18:09

Brandon Bodnar


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];
like image 24
Scott Bossak Avatar answered Sep 29 '22 17:09

Scott Bossak


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;
like image 43
amgalanBE Avatar answered Sep 29 '22 17:09

amgalanBE


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]; 
like image 41
Zubair Avatar answered Sep 29 '22 18:09

Zubair