IOS UIlabel, UIButton add underline

Source: Internet
Author: User
Tags vars

1. Add an underscore to the Uilabel

UILabel *label = [[UILabel alloc] Initwithframe:cgrectmake (0, -, -, -)]; Label.backgroundcolor=[Uicolor Redcolor]; Label.numberoflines=3; Nsmutableattributedstring*content = [[Nsmutableattributedstring alloc]initwithstring:[nsstring stringWithFormat:@"your To Do business section FASDFASDFOSADFJASDKLJFKLASJKLFJASDLKJFKLASDJKLFJALSKDJFLKADSJ"]]; Nsrange Contentrange= {0, [content length]}; [Content Addattribute:nsunderlinestyleattributename Value:[nsnumber Numberwithinteger:nsunderlinestylesingle]  Range:contentrange]; Label.attributedtext=content; [Self.view Addsubview:label];

2. http://blog.csdn.net/chaoyuan899/article/details/38306141

Iosuibuttonuilabel Underline

Method One:

[OBJC]View Plaincopyprint?
    1. nsmutableattributedstring *str = [[nsmutableattributedstring alloc] initwithstring:@ "View all winning records"];
    2. Nsrange Strrange = {0,[str length]};
    3. [Str addattribute:nsunderlinestyleattributename value:[nsnumber Numberwithinteger:  Nsunderlinestylesingle] Range:strrange];
    4. [_awarddisplaybtn setattributedtitle:str Forstate:uicontrolstatenormal];
 Method Two:

HyperlinksButton.h

[OBJC]View Plaincopyprint?
    1. #import <UIKit/UIKit.h>
    2. @interface Hyperlinksbutton:uibutton
    3. {
    4. Uicolor *linecolor;
    5. }
    6. -(void) SetColor: (uicolor*) color;
    7. @end

Hyperlinksbutton.m

[OBJC]View Plaincopyprint?
  1. #import "HyperlinksButton.h"
  2. @implementation Hyperlinksbutton
  3. -(ID) initWithFrame: (CGRect) Frame
  4. {
  5. Self = [super Initwithframe:frame];
  6. if (self) {
  7. }
  8. return self ;
  9. }
  10. -(void) SetColor: (uicolor *) color{
  11. LineColor = [color copy];
  12. [self setneedsdisplay];
  13. }
  14. -(void) DrawRect: (cgrect) rect {
  15. CGRect textrect = self. Titlelabel. Frame;
  16. Cgcontextref contextref = Uigraphicsgetcurrentcontext ();
  17. CGFloat descender = self. Titlelabel. Font. descender;
  18. if ([LineColor Iskindofclass:[uicolor class]]) {
  19. Cgcontextsetstrokecolorwithcolor (Contextref, LineColor.  Cgcolor);
  20. }
  21. Cgcontextmovetopoint (Contextref, Textrect. Origin. x, Textrect. Origin. Y + textrect. Size.  Height + descender+1);
  22. Cgcontextaddlinetopoint (Contextref, Textrect. Origin. x + textrect. Size. width, textrect. Origin  . Y + textrect. Size. Height + descender+1);
  23. Cgcontextclosepath (CONTEXTREF);
  24. Cgcontextdrawpath (Contextref, Kcgpathstroke);
  25. }
  26. @end
Copy this class directly into the project, and then change the required underlined Button class name to Hyperlinksbutton, provide setcolor: This interface, you can set the underline color, the code is very simple, not explained. UILabel have the same benefit.

Example results:

The code is here.

IOS UIlabel, UIButton add underline

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.