0

The problem is that UIButton clips title tail (for about 1-2 pixels at the end) when I use italic font like Helvetica Oblique with size bigger than 13. Does anybody know a solution for this problem?

esmirnov
  • 376
  • 2
  • 13
  • Set the wrap mode to something other than truncate tail. – Greg Jul 08 '11 at 12:39
  • Is the button big enough?! If you call sizeToFit *before* setting the text and font, this cannot work. – Eiko Jul 08 '11 at 12:58
  • 1
    more details please, but i think your case is "no matter how wide you make a button, any text, when made italic, gets clipped in a UIButton" - if that's the case, i don't know a workaround, and it's a bug, report it – bshirley Jul 08 '11 at 13:34
  • An image demonstrating the problem would also be helpful. – Alladinian May 15 '18 at 08:16
  • This is very old question. I believe that it is obsolete. To @bshirley - Yes it was no matter how wide a button. – esmirnov May 15 '18 at 14:27

4 Answers4

2

Subclass UIButton class and override setTitle with:

- (void)setTitle:(NSString *)title forState:(UIControlState)state { [super setTitle:[NSString stringWithFormat:@" %@ ", title] forState:state]; }

This will add additional spaces from both sides of your text. Easy & simple.

Tomasz Zabłocki
  • 1,326
  • 7
  • 14
  • Good thinking. A way that's even simpler is to just put a space before and after the standard UIButton's title. – Russ Hooper Apr 13 '15 at 20:23
  • Sorry but this is not a real solution, is a workaround at best, and padding the title with spaces for solving a layout/drawing issue is something that would likely bite you in the future. – Alladinian May 15 '18 at 08:14
0

I had this issue with my script font. Unfortunately there isn't a simple way to fix it. I tried everything and then ended up subclassing UIButton. You can read the details for implementation here: http://blog.tinymission.com/post/subclassing-uibutton

Basically, the easiest thing to do would be to put your own UILabel on top of the UIButton, but for me, that wouldn't work well so I subclassed UIButton.

Bek
  • 2,206
  • 20
  • 35
0

You can set Italic font by setting Uibutton property as

button.titleLabel?.font = UIFont.italicSystemFont(ofSize: 17)

Rahul Panzade
  • 1,302
  • 15
  • 12
0
  1. Create a custom subclass of UIButton
  2. Override method: - (CGRect)titleRectForContentRect:(CGRect)contentRect

Example:

- (CGRect)titleRectForContentRect:(CGRect)contentRect
{
    CGFloat textX = 0;
    CGFloat textY = 0;
    CGFloat textW = self.width;
    CGFloat textH = contentRect.size.height;
    return CGRectMake(textX, textY, textW, textH);
}
Majster
  • 3,611
  • 5
  • 38
  • 60
TOTORO
  • 1