1

I'm trying to underline label's text in UIButton. But it didn't work for me. My code in viewDidLoad method:

    CGFloat x = 13.0f;
CGFloat y = 15.0f;
if (self.numberOfFirstButton == 0) {
    self.numberOfFirstButton = 1;
}
for (NSUInteger i = 1; i <= 5; i++) {

    for (NSUInteger j = 0; j < 10; j++) {
        UIHouseButtons *button = [[UIHouseButtons alloc] init];
        [button setFrame:CGRectMake(x+(47*j), y, 30.0f, 30.0f)];
        [button setTitle:[NSString stringWithFormat:@"%i", self.numberOfFirstButton] forState:UIControlStateNormal];
        [button drawRect:CGRectMake(x+(47*j), y, 30.0f, 30.0f)];
        [self.view addSubview:button];
        self.numberOfFirstButton++;
    }
    y += 48.0f;
}

my drawRect method

- (void)drawRect:(CGRect)rect
{
CGRect textRect = self.titleLabel.frame;

// need to put the line at top of descenders (negative value)
CGFloat descender = self.titleLabel.font.descender;

CGContextRef contextRef = UIGraphicsGetCurrentContext();

// set to same colour as text
CGContextSetStrokeColorWithColor(contextRef, self.titleLabel.textColor.CGColor);

CGContextMoveToPoint(contextRef, textRect.origin.x, textRect.origin.y + textRect.size.height + descender);

CGContextAddLineToPoint(contextRef, textRect.origin.x + textRect.size.width, textRect.origin.y + textRect.size.height + descender);

CGContextClosePath(contextRef);

CGContextDrawPath(contextRef, kCGPathStroke);
[super drawRect:rect];
}

There are all my buttons in view but label's text is not underlined. What I'm doing wrong?

RomanHouse
  • 2,552
  • 3
  • 23
  • 44

3 Answers3

2

I recommend using an NSAttributedString label. There are a bunch of open source projects that will make this easy. The two leading projects are:

Nimbus' NIAttributedLabel
TTTAttributedLabel

Nimbus example:

myLabel.underlineStyle = kCTUnderlineStyleSingle;

featherless
  • 2,118
  • 19
  • 19
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];  

}

then put a custome button over that label.

PJR
  • 13,052
  • 13
  • 64
  • 104
  • Ok. But my UIButton is custom (UIHouseButtons). How can I join custom button and UILabel subclass? – RomanHouse Jun 26 '12 at 20:05
  • no need to join them , just remain button custom and put button on it. first add a custom label to the view and then put button on it.so you can see label with line and get button action by clicking on button. – PJR Jun 26 '12 at 20:07
0

I've made a simple UIButton subclass BVUnderlineButton (based on other code found elsewhere on the web) that you can drop straight into your projects.

It's on Github at https://github.com/benvium/BVUnderlineButton (MIT licence)

Ben Clayton
  • 80,996
  • 26
  • 120
  • 129