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?
- nsmutableattributedstring *str = [[nsmutableattributedstring alloc] initwithstring:@ "View all winning records"];
- Nsrange Strrange = {0,[str length]};
- [Str addattribute:nsunderlinestyleattributename value:[nsnumber Numberwithinteger: Nsunderlinestylesingle] Range:strrange];
- [_awarddisplaybtn setattributedtitle:str Forstate:uicontrolstatenormal];
Method Two:
HyperlinksButton.h
[OBJC]View Plaincopyprint?
- #import <UIKit/UIKit.h>
- @interface Hyperlinksbutton:uibutton
- {
- Uicolor *linecolor;
- }
- -(void) SetColor: (uicolor*) color;
- @end
Hyperlinksbutton.m
[OBJC]View Plaincopyprint?
- #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 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