Skip to content

Instantly share code, notes, and snippets.

@mikaelbartlett
Created June 29, 2011 19:33
Show Gist options
  • Save mikaelbartlett/1054706 to your computer and use it in GitHub Desktop.
Save mikaelbartlett/1054706 to your computer and use it in GitHub Desktop.

Revisions

  1. Mikael Bartlett revised this gist Feb 11, 2012. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions UILabelStrikethrough.m
    Original file line number Diff line number Diff line change
    @@ -42,7 +42,7 @@ - (void)drawTextInRect:(CGRect)rect {

    - (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();
    const float* colors = CGColorGetComponents( self.strokeColor.CGColor );
    CGContextSetStrokeColor(c, colors);
    @@ -56,4 +56,4 @@ - (void)drawRect:(CGRect)rect {
    [super drawRect:rect];
    }

    @end
    @end
  2. Mikael Bartlett created this gist Jun 29, 2011.
    15 changes: 15 additions & 0 deletions UILabelStrikethrough.h
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    @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
    59 changes: 59 additions & 0 deletions UILabelStrikethrough.m
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,59 @@
    #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];
    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