22

I have uploaded a custom font and applied this font on the title of a UIbutton using the following code

videoButton.titleLabel.font = [UIFont fontWithName:@"LaurenScript" size:20];

The problem is that the title is being clipped on the top of the first letter (see photo below). I tried the same font on the UIlabel and it works fine so it is not a problem with the font. I tried also to change the rectFrame using

[videoButton.titleLabel setFrame:CGRectMake(0, 0, 300, 600)];

but that didn't do anything. Has anybody a clue of how I can fix this problem? Cheers

enter image description here

Antoine
  • 23,526
  • 11
  • 88
  • 94
Cyril Gaillard
  • 909
  • 9
  • 28

7 Answers7

45

I had a similar problem, where a diaeresis got cut off on top of the titlelabel. I made a UIButton subclass and used this code to fix the problem:

-(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;
}
Antoine
  • 23,526
  • 11
  • 88
  • 94
34

Select button in Interface builder and check for set a vertical alignment panel in the control section Below is example:

enter image description here

Hardik Darji
  • 3,633
  • 1
  • 30
  • 30
10

Not sure if this is still an issue for anyone, but I found that (with using a custom font) the above solutions did not ultimately fix the issue, especially for a custom UIButton created solely programmatically.

Here is how I managed to fix this issue, with 1 line in particular that resolved the clipping issue:

UIButton *button = [[UIButton alloc] init];
button = [UIButton buttonWithType:UIButtonTypeCustom];

[button setTitle:@"Nice!" forState:UIControlStateNormal];
[button setFont:[UIFont fontWithName:<CUSTOM FONT NAME> size:buttonWidth/3.0f]];
button = CGRectMake(0, 0, <WIDTH>, <HEIGHT>);

Here was the line that resolved the clipping:

[button setContentVerticalAlignment:UIControlContentVerticalAlignmentFill];

Hopefully this helps anyone else who still were stuck. Happy coding!

Will Von Ullrich
  • 2,129
  • 2
  • 15
  • 42
5

had same problem using a button with an image and text with a custom font. Everything had to be align centered vertically. And image not stretched. this worked out fine for me.

btn.contentVerticalAlignment = .fill
btn.contentMode = .center
btn.imageView?.contentMode = .scaleAspectFit
mate0
  • 191
  • 4
  • 7
4

I try this in swift 2.1, I adapt this code from Antoine answer. This may not good code but it solve my problem for now. You should make it better for you self.

import UIKit

class CustomUIButton: UIButton {

    override func layoutSubviews() {
        if var titleFrame : CGRect = titleLabel?.frame{

            titleFrame.size = self.bounds.size
            titleFrame.origin = CGPointZero
            self.titleLabel!.frame = titleFrame
            self.titleLabel!.textAlignment = .Center
        }
    }
}
Community
  • 1
  • 1
Mate
  • 109
  • 2
3

There is this (sad) solution: https://stackoverflow.com/a/10200908/352628

I have a similar problem. It seems that the titleLabel is just very uncontrollable, and to get control you need to inject a UILabel subview to the button... That makes me sad :(

Community
  • 1
  • 1
Ibmurai
  • 932
  • 7
  • 11
0

enter image description hereIn my case the button title label was aligned vertically, but text needed insets on top.

self.contentEdgeInsets = UIEdgeInsets(top: 5.0, left: 0.0, bottom: 0.0, right: 0.0)

M.Yessir
  • 202
  • 3
  • 11