Skip to content

Instantly share code, notes, and snippets.

@zypsg
Created March 27, 2012 09:52
Show Gist options
  • Save zypsg/2214495 to your computer and use it in GitHub Desktop.
Save zypsg/2214495 to your computer and use it in GitHub Desktop.
iOS: Underline the title of a UIButton
@interface UIUnderlinedButton : UIButton
+ (UIUnderlinedButton*) underlinedButton;
@end
implementation UIUnderlinedButton
+ (UIUnderlinedButton*) underlinedButton {
UIUnderlinedButton* button = [[UIUnderlinedButton alloc] init];
return [button autorelease];
}
- (void) drawRect:(CGRect)rect {
CGRect textRect = self.titleLabel.frame;
// need to put the line at top of descenders (negative value)
CGFloat descender = self.titleLabel.font.descender;
CGContextRef contextRef = UIGraphicsGetCurrentContext();
// set to same colour as text
CGContextSetStrokeColorWithColor(contextRef, self.titleLabel.textColor.CGColor);
CGContextMoveToPoint(contextRef, textRect.origin.x, textRect.origin.y + textRect.size.height + descender);
CGContextAddLineToPoint(contextRef, textRect.origin.x + textRect.size.width, textRect.origin.y + textRect.size.height + descender);
CGContextClosePath(contextRef);
CGContextDrawPath(contextRef, kCGPathStroke);
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment