0

I'm trying to make a UIButton with an image above the text. To that end, I tried setting the edge insets of the button as so:

[button setImageEdgeInsets:UIEdgeInsetsMake(0.0f, 0.0f, 10, 0.0f)];
[button setTitleEdgeInsets:UIEdgeInsetsMake(10, 0.0, 0.0, 0.0)];

What I get is a button where the image is in the same place, and the text is squished all the way over to the side, in a space about one character wide.

How can I simply set the insets so I have the image above the text? Is that really so much to ask?

s73v3r
  • 1,751
  • 2
  • 22
  • 48
  • Check out this: http://stackoverflow.com/questions/2808078/is-it-possible-to-adjust-position-x-y-titlelabel-of-uibutton – marchinram Jun 18 '13 at 00:01

1 Answers1

1

You just wrongly calculated edgeInSets.They are not there the way you think they are. Here's my test code.It did cost me a while to put image and title in the right place.

UIButton *tmp_testBtn = [[UIButton alloc] initWithFrame:CGRectMake(50, 200, 60, 40)];
tmp_testBtn.backgroundColor = [UIColor grayColor];
[tmp_testBtn setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 20, 0)];
[tmp_testBtn setImage:[UIImage imageNamed:@"France"] forState:UIControlStateNormal];
[tmp_testBtn setTitleEdgeInsets:UIEdgeInsetsMake(20, -258, 0, 0)];
[tmp_testBtn setTitle:@"testbutton" forState:UIControlStateNormal];

CGRect contentRect = [tmp_testBtn contentRectForBounds:tmp_testBtn.bounds];
NSLog(@"contenRect:%f,%f,%f,%f",contentRect.origin.x,contentRect.origin.y,contentRect.size.width,contentRect.size.height);
CGRect titleRect = [tmp_testBtn titleRectForContentRect:contentRect];
NSLog(@"titleRect:%f,%f,%f,%f",titleRect.origin.x,titleRect.origin.y,titleRect.size.width,titleRect.size.height);
CGRect imageRect = [tmp_testBtn imageRectForContentRect:contentRect];
NSLog(@"imageRect:%f,%f,%f,%f",imageRect.origin.x,imageRect.origin.y,imageRect.size.width,imageRect.size.height);

[self.view addSubview:tmp_testBtn];
[tmp_testBtn release];

By the way, I won't do like this.I prefer customize a button with a UIImageView and a UILabel added on.

s73v3r
  • 1,751
  • 2
  • 22
  • 48
Hunter
  • 136
  • 1
  • 9
  • I'm not entirely following how to came to the numbers you used. You have a UIButton which is 60 wide by 40 tall. You inset the image 20 on the bottom. But then when you go to inset the text, you do 20 on the left, and -258 on the top? I think I get why it's negative, but why is the magnitude greater than that of the button? – s73v3r Jun 18 '13 at 19:57
  • Oh, if you're printing a frame rect or any other CGRect, there is the function NSStringFromCGRect which will print out your rect in a nice format, and means you don't have to type so much on your NSLog statements ;) – s73v3r Jun 18 '13 at 20:33
  • Yeah,thanks a lot.That will save me a lot of work.I did this earlier:"[tmp_testBtn setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 20, 0)];[tmp_testBtn setTitleEdgeInsets:UIEdgeInsetsMake(20, 0, 0, 0)];",and the NSLog of titleRect gave me this:"titleRect:256.000000,30.000000,-196.000000,0.000000" which bothers me,too.And this"you do 20 on the left, and -258 on the top" should be"20 at the top, and -258 on the left" – Hunter Jun 19 '13 at 01:30