UILabel Strikethrough
-
20-08-2019 - |
Question
Is it possible to stikethrough
a UILabel
at all? I can't seem to find the option...
Solution
This is an old question and newer information is available.
Starting in iOS 6, we have NSAttributedString.
For pre iOS 6, I would look at TTTAttributedLabel
OTHER TIPS
You could create another UILabel above your label and use an en dash character:
label.text = @"––––––––––––––––––";
Caveat: works with Helvetica (system default). It may not work with other fonts.
iPhone doesn't support attributed strings (which is usually the way you'd do this in Cocoa), so I don't believe it's possible.
You could subclass UILabel
and draw the strikethrough
yourself. I've also seen some people use a UIWebView
to do this type of thing, but that seems like overkill to me.
UIView* slabel = [[UIView alloc] initWithFrame:CGRectMake(label.frame.origin.x, label.frame.origin.y+10, label.frame.size.width, 2)];
[self addSubview:slabel];
[slabel setBackgroundColor:label.textColor];
You can add a view over UILabel and style it with label properties.
NSAttributedString *str=[[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"¥%.2f", productOne.priceBefore] attributes:@{NSStrikethroughStyleAttributeName:@(NSUnderlineStyleSingle)}];
cell.priceBefore.attributedText = str;