7

I am using a custom font for the title of a UIButton. It works, except with this particular font a portion of the first character is clipped. For example:

enter image description here

I tried setting the contentEdgeInsets and titleEdgeInsets and can't seem to get it not clipped. I also tried setting the button.titleLabel.clipsToBounds property to NO;

Any suggestions would be greatly appreciated.

Jeshua Lacock
  • 5,730
  • 1
  • 28
  • 58

4 Answers4

10

Although Jeshua's solution works fine, it's not an optimal in my eyes. I'd rather recommend to subclass UIButton and overwrite it's layoutSubviews Method.

-(void)layoutSubviews
{
    [super layoutSubviews];

    CGRect frame = self.titleLabel.frame;
    frame.size.height = self.bounds.size.height;
    frame.origin.y = self.titleEdgeInsets.top;
    self.titleLabel.frame = frame;
}
samsam
  • 3,125
  • 24
  • 40
8

So I just ended up setting the UIButton title to nil and added my own UILabel as a subview to the UIButton. I set the UILabel frame to the same size as the button and set it to be centered.

Not the most elegant solution I am sure, but it gets the job done.

Jeshua Lacock
  • 5,730
  • 1
  • 28
  • 58
  • 2
    It really sucks that this is the only solution. The reason this happens is that font designers intentionally encode the bounds of their letters as less than they actually are so that they will nest. This is especially important for italic and cursive fonts. I wish Apple wouldn't have been so "precise" with the calculation of the width of the UIButton label, or at least allowed us to change it ourselves. I'm filing a bug report, you should too. – Bek Mar 07 '13 at 18:26
1

It looks to me like your content is aligned funkily. If the frame of the label is not directly accessible, use [button setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter]; to try to fix the horizontal alignment issue.

CodaFi
  • 43,043
  • 8
  • 107
  • 153
1

Swift code for above answer:

override func layoutSubviews() {
    super.layoutSubviews()
    titleLabel?.frame = CGRect(x: bounds.origin.x, y: titleEdgeInsets.top, width: frame.width, height: bounds.size.height)
}
Dhiraj Das
  • 174
  • 9