3

How to underline text in objective c iphone?? Is there any method for underline text of UILabel??

friedemann_bach
  • 1,418
  • 14
  • 29
Nirav Jain
  • 5,088
  • 5
  • 40
  • 61

4 Answers4

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
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