Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Simple class for IOS SDK UILabel with strikethrough
@interface UILabelStrikethrough : UILabel {
int xOffset;
int yOffset;
int widthOffset;
int stroke;
UIColor* strokeColor;
}
@property (nonatomic) int xOffset;
@property (nonatomic) int yOffset;
@property (nonatomic) int widthOffset;
@property (nonatomic) int stroke;
@property (nonatomic,retain) UIColor* strokeColor;
-(id) initWithFrame:(CGRect)frame xOffset:(int)_xOffset yOffset:(int)_yOffset widthOffset:(int)_widthOffset stroke:(int)_stroke strokeColor:(UIColor*)_strokeColor;
@end
#import "UILabelStrikethrough.h"
@implementation UILabelStrikethrough
@synthesize xOffset, yOffset, widthOffset, stroke;
@synthesize strokeColor;
-(void) dealloc {
[super dealloc];
[strokeColor release];
}
-(id) initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
self.xOffset = 0;
self.yOffset = 0;
self.widthOffset = 0;
self.stroke = 2;
self.strokeColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:1];
}
return self;
}
-(id) initWithFrame:(CGRect)frame xOffset:(int)_xOffset yOffset:(int)_yOffset widthOffset:(int)_widthOffset stroke:(int)_stroke strokeColor:(UIColor*)_strokeColor {
frame = CGRectMake(frame.origin.x, frame.origin.y, frame.size.width+_widthOffset-_xOffset, frame.size.height);
self = [super initWithFrame:frame];
if (self) {
self.xOffset = _xOffset;
self.yOffset = _yOffset;
self.widthOffset = _widthOffset;
self.stroke = _stroke;
self.strokeColor = _strokeColor;
}
return self;
}
- (void)drawTextInRect:(CGRect)rect {
UIEdgeInsets insets = {0, 0-self.xOffset, 0, 0+self.widthOffset};
return [super drawTextInRect:UIEdgeInsetsInsetRect(rect, insets)];
}
- (void)drawRect:(CGRect)rect {
CGSize size = [self.text sizeWithFont:self.font constrainedToSize:self.frame.size lineBreakMode:self.lineBreakMode];
CGContextRef c = UIGraphicsGetCurrentContext();
const float* colors = CGColorGetComponents( self.strokeColor.CGColor );
CGContextSetStrokeColor(c, colors);
CGContextSetLineWidth(c, self.stroke);
CGContextBeginPath(c);
int halfWayUp = (size.height - self.bounds.origin.y) / 2 + self.yOffset;
CGContextMoveToPoint(c, self.bounds.origin.x + self.xOffset, halfWayUp );
CGContextAddLineToPoint(c, self.bounds.origin.x + size.width + self.widthOffset - self.xOffset, halfWayUp);
CGContextStrokePath(c);
[super drawRect:rect];
}
@end
@aniltv06

This comment has been minimized.

Show comment Hide comment
@aniltv06

aniltv06 Sep 20, 2011

This works fine.

This works fine.

@leolobato

This comment has been minimized.

Show comment Hide comment
@leolobato

leolobato Oct 8, 2011

I had to change all integers to floats, and use CGContextSetStrokeColorWithColor for this to work on SDK 4.3.

I had to change all integers to floats, and use CGContextSetStrokeColorWithColor for this to work on SDK 4.3.

@mikaelbartlett

This comment has been minimized.

Show comment Hide comment
@mikaelbartlett

mikaelbartlett Oct 10, 2011

Just make sure to round to nearest integer, as decimal precision make a line with 1 pixel line-height blurry.

Owner

mikaelbartlett commented Oct 10, 2011

Just make sure to round to nearest integer, as decimal precision make a line with 1 pixel line-height blurry.

@silvansky

This comment has been minimized.

Show comment Hide comment
@silvansky

silvansky Feb 11, 2012

Fix for label with line break:

- (void)drawRect:(CGRect)rect {
    
---    CGSize size = [self.text sizeWithFont:self.font constrainedToSize:self.frame.size];
+++    CGSize size = [self.text sizeWithFont:self.font constrainedToSize:self.frame.size lineBreakMode:self.lineBreakMode];
   CGContextRef c = UIGraphicsGetCurrentContext();

Fix for label with line break:

- (void)drawRect:(CGRect)rect {
    
---    CGSize size = [self.text sizeWithFont:self.font constrainedToSize:self.frame.size];
+++    CGSize size = [self.text sizeWithFont:self.font constrainedToSize:self.frame.size lineBreakMode:self.lineBreakMode];
   CGContextRef c = UIGraphicsGetCurrentContext();
@mikaelbartlett

This comment has been minimized.

Show comment Hide comment
@mikaelbartlett

mikaelbartlett Feb 11, 2012

Thanks,

Just ran in to this bug myself this week.

Owner

mikaelbartlett commented Feb 11, 2012

Thanks,

Just ran in to this bug myself this week.

@hemangshah

This comment has been minimized.

Show comment Hide comment
@hemangshah

hemangshah Nov 18, 2014

Is it possible that we can switch between strike through effect and without it when user taps on a label?

Is it possible that we can switch between strike through effect and without it when user taps on a label?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment