Underline the uilabel or uibutton title

Source: Internet
Author: User

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.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.