Underlining text in UIButton

后端 未结 18 1894
终归单人心
终归单人心 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:25

    Nick's answer is a great, quick way to do this.

    I added support in drawRect for shadows.

    Nick's answer doesn't take into account if your button title has a shadow below the text:

    enter image description here

    But you can move the underline down by the height of the shadow like so:

    CGFloat descender = self.titleLabel.font.descender;
    CGContextRef contextRef = UIGraphicsGetCurrentContext();
    CGFloat shadowHeight = self.titleLabel.shadowOffset.height;
    descender += shadowHeight;
    

    Then you'll get something like this:

    enter image description here

提交回复
热议问题