Search code examples
iphoneobjective-ccocoa-touchios4

How Can I Underline the Button Text of UIButton?


The text is coming form a database. I would like to use it for a button and underline the text of the button. How can I do that?


Solution

  • For this you can subclass UILabel and overwrite its -drawRect method and then use your own UILabel and add a UIButton over it of custom type.

    Make your drawRect method in UILabel as

    - (void)drawRect:(CGRect)rect 
    {
        CGContextRef context = UIGraphicsGetCurrentContext();
    
        CGContextSetRGBStrokeColor(context, 207.0f/255.0f, 91.0f/255.0f, 44.0f/255.0f, 1.0f);
    
        CGContextSetLineWidth(context, 1.0f);
    
        CGContextMoveToPoint(context, 0, self.bounds.size.height - 1);
        CGContextAddLineToPoint(context, self.bounds.size.width, self.bounds.size.height - 1);
    
        CGContextStrokePath(context);
    
        [super drawRect:rect]; 
    
    }