Tag: IOS uibutton uilabel underline
Method 1:
Nsmutableattributedstring * STR = [[nsmutableattributedstring alloc] initwithstring: @ "view all winning records"]; nsange strrange = {0, [STR length]}; [STR addattrirange: Your value: [nsnumber numberwithinteger: nsunderlinestylesingle] range: strrange]; [_ awarddisplaybtn setattributedtitle: Str forstate: uicontrolstatenormal];
Method 2:
Hyperlinksbutton. h
#import <UIKit/UIKit.h>@interface HyperlinksButton : UIButton{ UIColor *lineColor;}-(void)setColor:(UIColor*)color;@end
Hyperlinksbutton. m
#import "HyperlinksButton.h"@implementation HyperlinksButton- (id)initWithFrame:(CGRect)frame{ self = [super initWithFrame:frame]; if (self) { } return self;}-(void)setColor:(UIColor *)color{ lineColor = [color copy]; [self setNeedsDisplay];}- (void) drawRect:(CGRect)rect { CGRect textRect = self.titleLabel.frame; CGContextRef contextRef = UIGraphicsGetCurrentContext(); CGFloat descender = self.titleLabel.font.descender; if([lineColor isKindOfClass:[UIColor class]]){ CGContextSetStrokeColorWithColor(contextRef, lineColor.CGColor); } CGContextMoveToPoint(contextRef, textRect.origin.x, textRect.origin.y + textRect.size.height + descender+1); CGContextAddLineToPoint(contextRef, textRect.origin.x + textRect.size.width, textRect.origin.y + textRect.size.height + descender+1); CGContextClosePath(contextRef); CGContextDrawPath(contextRef, kCGPathStroke);}@end
Copy the class directly to the project, and change the name of the button class to hyperlinksbutton. The setcolor: interface is provided to set the underline color. The code is very simple, no. Uilabel is also available.