2

I'm trying to make a new layout for my buttons.

Actualy when you set up a Custom UIButton with an image and a title, the two elements are on a line. But, on my view I need to have the title under the image. I've try to do it with the "Edge Inset" property, and it work.

There is a way to do it with a subclass of UIButton? And how to do it?

thanks in advence!

Gwennin

edit:

I have made like Jano propose in comment

code to generate the background image:

+(UIImage*)drawText:(NSString*)text inImage:(UIImage*)image {

UIFont *font = [UIFont boldSystemFontOfSize:16];
CGSize size = CGSizeMake(200, 200);
UIGraphicsBeginImageContext(size);
[image drawInRect:CGRectMake(100 - (image.size.width / 2), 0, image.size.width, image.size.height)];
CGRect rect = CGRectMake(100 - ([text length] * 8) / 2, image.size.height, 200, 200);
[[UIColor whiteColor] set];
[text drawInRect:CGRectIntegral(rect) withFont:font];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

return newImage;
}

Thanks to everyone!

Community
  • 1
  • 1
Gwennin
  • 25
  • 6
  • Create a button with the same image but a bigger frame space vertically and then paint the text using the code from this answer: http://stackoverflow.com/questions/6992830/how-to-write-text-on-image-in-objective-c-iphone/6993766#6993766 – Jano Aug 09 '11 at 13:38

3 Answers3

0

I will suggest you here is no need to subclass UIButton, you can make an image which has bottom part transparent in which you can display button's title and set button's title alignment to bottom so that you can display an image with a title at bottom in UIButton.

you can do something like this -

enter image description here

saadnib
  • 11,145
  • 2
  • 33
  • 54
0

You can subclass UIControl to make custom button.

Mikhail Grebionkin
  • 3,806
  • 2
  • 17
  • 18
0

The simple solution is add image and label make label userinteractionenabled = NO ,then add a buuton of custom type add it on both image and labels..otherwise UIConrol ad michail said..

sairam
  • 299
  • 1
  • 6
  • 17