Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cropped UIButton Title

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.

like image 440
Jeshua Lacock Avatar asked Apr 17 '12 22:04

Jeshua Lacock


3 Answers

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;
}
like image 164
samsam Avatar answered Nov 19 '22 17:11

samsam


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.

like image 8
Jeshua Lacock Avatar answered Nov 19 '22 17:11

Jeshua Lacock


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.

like image 1
CodaFi Avatar answered Nov 19 '22 16:11

CodaFi