How to underline text in objective c iphone?? Is there any method for underline text of UILabel??
            Asked
            
        
        
            Active
            
        
            Viewed 5,509 times
        
    3
            
            
        - 
                    possible duplicate of http://stackoverflow.com/questions/2711297/underline-text-in-uilabel – Jhaliya - Praveen Sharma Apr 25 '11 at 10:50
 - 
                    search before you ask any question. – Inder Kumar Rathore Apr 25 '11 at 10:51
 
4 Answers
3
            Subclass UILabel and override drawRect method as below. Underline will have the same text color and text alignment as the label:
- (void)drawRect:(CGRect)rect
{
    if([[self.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length])
    {
        CGContextRef ctx = UIGraphicsGetCurrentContext();
        const CGFloat* colors = CGColorGetComponents(self.textColor.CGColor);
        CGContextSetRGBStrokeColor(ctx, colors[0], colors[1], colors[2], 1.0); // RGBA
        CGContextSetLineWidth(ctx, 1.0f);
        CGSize tmpSize = [self.text sizeWithFont:self.font constrainedToSize:CGSizeMake(200, 9999)];
        // check text alignment
        if(self.textAlignment == UITextAlignmentLeft) {
            CGContextMoveToPoint(ctx, 0, self.bounds.size.height - 1);
            CGContextAddLineToPoint(ctx, tmpSize.width, self.bounds.size.height - 1);
        }else if(self.textAlignment == UITextAlignmentCenter) {
            CGFloat startPoint = (self.frame.size.width - tmpSize.width) / 2;
            CGContextMoveToPoint(ctx, startPoint, self.bounds.size.height - 1);
            CGContextAddLineToPoint(ctx, tmpSize.width + startPoint, self.bounds.size.height - 1);
        }else if (self.textAlignment == UITextAlignmentRight) {
            CGFloat startPoint = (self.frame.size.width - tmpSize.width);
            CGContextMoveToPoint(ctx, startPoint, self.bounds.size.height - 1);
            CGContextAddLineToPoint(ctx, self.frame.size.width, self.bounds.size.height - 1);
        }
        CGContextStrokePath(ctx);
    }
    [super drawRect:rect];
}
        Pankaj
        
- 76
 - 4
 
2
            
            
        You may subclass from UILabel and override drawRect method:
- (void)drawRect:(CGRect)rect {
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextSetRGBStrokeColor(ctx, 207.0f/255.0f, 91.0f/255.0f, 44.0f/255.0f, 1.0f); // RGBA
    CGContextSetLineWidth(ctx, 1.0f);
    CGContextMoveToPoint(ctx, 0, self.bounds.size.height - 1);
    CGContextAddLineToPoint(ctx, self.bounds.size.width, self.bounds.size.height - 1);
    CGContextStrokePath(ctx);
    [super drawRect:rect];  
}
        visakh7
        
- 26,380
 - 8
 - 55
 - 69
 
- 
                    
 - 
                    If you want a link why don't you use a custom button with the title as the underlined text and open the link using the button action – visakh7 Apr 25 '11 at 11:00
 - 
                    Or you could even try placing a custom transparent button on top of the label. – visakh7 Apr 25 '11 at 11:07
 
1
            
            
        Simply put, there's no advanced formatting available in UILabel.
If what you're looking for is a link, then you're better of using an UIWebView, and feed it some "home-made HTML", like "My Link". Then you can handle click on the webview in your webview's delegate.
        Ecco
        
- 1,323
 - 1
 - 11
 - 15
 
0
            
            
        Use an attribute string:
NSAttributedString* attrString = [[NSAttributedString alloc] initWithString:@"Your String"]
[attrString addAttribute:(NSString*)kCTUnderlineStyleAttributeName 
                   value:[NSNumber numberWithInt:kCTUnderlineStyleSingle] 
                   range:(NSRange){0,[attrString length]}];
And then override the label - (void)drawTextInRect:(CGRect)aRect and render the text in something like:
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSaveGState(ctx);
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)attrString);
drawingRect = self.bounds;
CGMutablePathRef path = CGPathCreateMutable();
CGPathAddRect(path, NULL, drawingRect);
textFrame = CTFramesetterCreateFrame(framesetter,CFRangeMake(0,0), path, NULL);
CGPathRelease(path);
CFRelease(framesetter);
CTFrameDraw(textFrame, ctx);
CGContextRestoreGState(ctx);
Or better yet instead of overriding just use the OHAttributedLabel created by Olivier Halligon. He also has support for custom links, and for custom colors for them.
        Paulo Ferreira
        
- 321
 - 2
 - 6