Underlining text in UIButton

后端 未结 18 1862
终归单人心
终归单人心 2020-11-29 15:56

Can anyone suggest how to underline the title of a UIButton ? I have a UIButton of Custom type, and I want the Title to be underlined, but the Interface Builder does not pr

18条回答
  •  爱一瞬间的悲伤
    2020-11-29 16:18

    Expanding on the answer by @Nick H247, I experienced an issue where firstly the underline was not redrawing when the button resized on rotation; this can be solved by setting your button to redraw like so:

    myButton.contentMode = UIViewContentModeRedraw; 
    

    This forces the button to redraw when the bounds change.

    Secondly, the original code assumed you only had 1 line of text in the button (my button wraps to 2 lines on rotation) and the underline only appears on the last line of text. The drawRect code can be modified to first calculate the number of lines in the button, then put an underline on every line rather than just the bottom, like so:

     - (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);
    
    CGSize labelSize = [self.titleLabel.text sizeWithFont:self.titleLabel.font
                                constrainedToSize:self.titleLabel.frame.size
                                    lineBreakMode:UILineBreakModeWordWrap];
    
    CGSize labelSizeNoWrap = [self.titleLabel.text sizeWithFont:self.titleLabel.font forWidth:self.titleLabel.frame.size.width lineBreakMode:UILineBreakModeMiddleTruncation ];
    
    int numberOfLines = abs(labelSize.height/labelSizeNoWrap.height);
    
    for(int i = 1; i<=numberOfLines;i++) {
     //        Original code
     //        CGContextMoveToPoint(contextRef, textRect.origin.x, textRect.origin.y + textRect.size.height + descender + PADDING);
     //        
     //        CGContextAddLineToPoint(contextRef, textRect.origin.x + textRect.size.width, textRect.origin.y + textRect.size.height + descender);
    
        CGContextMoveToPoint(contextRef, textRect.origin.x, textRect.origin.y + (labelSizeNoWrap.height*i) + descender + PADDING);
    
        CGContextAddLineToPoint(contextRef, textRect.origin.x + textRect.size.width, textRect.origin.y + (labelSizeNoWrap.height*i) + descender);
    
        CGContextClosePath(contextRef);
    
        CGContextDrawPath(contextRef, kCGPathStroke);
    
    }
    
    
    }
    

    Hope this code helps someone else!

提交回复
热议问题