开发者

How to underline UILabel in iPhone SDK? [duplicate]

This question already has answers here: Closed 11 years ago.

Possible Dupli开发者_如何学Gocate:

underline text in UIlabel

Can any one provide a code to show me how to underline text in UILabel? In Interface Builder I underline the UILabel text but it's not working.


You can inheritate UILabel and draw a line yourself in drawTextInRec


you need to implement the drawRect: method of your UIView inherited class.

 - (void)drawRect:(CGRect)rect 
    {
            // Get the Render Context
        CGContextRef ctx = UIGraphicsGetCurrentContext();

        // Measure the font size, so the line fits the text.
        // Could be that "titleLabel" is something else in other classes like UILable, dont know.
        // So make sure you fix it here if you are enhancing UILabel or something else..
        CGSize fontSize =[self.titleLabel.text sizeWithFont:self.titleLabel.font 
                                                   forWidth:self.bounds.size.width
                                              lineBreakMode:UILineBreakModeTailTruncation];
        // Get the fonts color. 
        const float * colors = CGColorGetComponents(self.titleLabel.textColor.CGColor);
        // Sets the color to draw the line
        CGContextSetRGBStrokeColor(ctx, colors[0], colors[1], colors[2], 1.0f); // Format : RGBA

        // Line Width : make thinner or bigger if you want
        CGContextSetLineWidth(ctx, 1.0f);

        // Calculate the starting point (left) and target (right)   
        float fontLeft = self.titleLabel.center.x - fontSize.width/2.0;
        float fontRight = self.titleLabel.center.x + fontSize.width/2.0;

        // Add Move Command to point the draw cursor to the starting point
        CGContextMoveToPoint(ctx, fontLeft, self.bounds.size.height - 1);

        // Add Command to draw a Line
        CGContextAddLineToPoint(ctx, fontRight, self.bounds.size.height - 1);

        // Actually draw the line.
        CGContextStrokePath(ctx);

        // should be nothing, but who knows...
        [super drawRect:rect];   
    }

Taken form iPhone Programming how to underline UILabel

You could also inherit UILabel . See the belwo SO post, which does exactly the same.

Underline text in UIlabel

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜