1

I am trying to create a very thin UIButton with size = CGSizeMake(75, 23) to fit a customized background image exactly. And I would like the button to have a title.

After I call setTitle:forState: the button is resized to account for padding around the title, screwing up the background image (i.e. causing it to tile very unseemly). I have set the title font to quite a small size in the UILabel subview, so it's not the font that's causing the problem but the padding. Thus I would like to remove this padding. The default values of the contentEdgeInsets, imageEdgeInsets and titleEdgeInsets properties of UIButton are all UIEdgeInsetsZero, so I presume the padding is somewhere in the UILabel subview. If so, I think I can eliminate it by subclassing UILabel and overriding textRectForBounds:limitedToNumberOfLines: but I'm not sure (and I'd be happy to hear from someone who has tried this). In any event, I'm looking for an easier way out.

I've searched and searched and haven't found anything.

Any suggestions?

Andre Gregori
  • 1,150
  • 1
  • 10
  • 17
  • Did you try setting the frame of the button after you set the title? – lnafziger Dec 30 '12 at 07:38
  • Are you using PNG for the buttons? Try adding the label onto the PNG file itself, then you can avoid any padding that the label causes. This does of course mean you loose the ability to change the button labels on the fly. – jwknz Dec 30 '12 at 08:05
  • @lnafziger yep, tried that to no avail, unfortunately. – Andre Gregori Dec 30 '12 at 08:30
  • @Jeff Kranenburg, yep, I'm using PNG, but I do need to change the title dynamically, so hardwiring it in is not an option, unfortunately. What I'm considering doing is enlarging the PNG file's canvas with transparent pixels so as to cover the padding. This will mean, of course, that the button will include the transparent pixels, which is not desirable since those pixels will generate touch events. – Andre Gregori Dec 30 '12 at 08:31
  • @lnafziger I guess I should also mention that I've tried setting the frame of the `UILabel` subview after I've set the title, but again to no avail. – Andre Gregori Dec 30 '12 at 08:37
  • Sorry, I'm not sure what to do! – lnafziger Dec 30 '12 at 17:57

1 Answers1

0

That would work.

I have recommended this tool before but it's a miracle worker:-)

OBShapedButton

It will take care of your transparency problem That should solve your issue:-)

jwknz
  • 6,598
  • 16
  • 72
  • 115
  • Thanks @JeffKranenburg, `OBShapedButton` seems like a good solution and it does solve the transparency issue, but I will hold off on implementing it unless I cannot find another solution, since there will likely be some performance loss (there will be over twenty instances of this button on a view at any given time). – Andre Gregori Dec 30 '12 at 18:15
  • I see what you mean - I have an app and it uses 8 instances of it on the start screen. Haven't noticed anything. But I am guessing there has to be some give and take. Even if you implement it now until you find a better solution. – jwknz Dec 30 '12 at 20:08