UIButton to show BackgroundImage, Image and Title - objective-c

I want to create a UIButton that uses its setBackgroundImage, setImage, and setTitle properties, but when I add setTitle in combination with setImage and setBackgroundImage the title doesn't appear.
I've tried the following.
[button setBackgroundImage:[UIImage imageNamed:#"image3.jpg"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:#"month_pct_general.png"] forState:UIControlStateNormal];
[button setTitle:[NSString stringWithFormat:#"%d",[button tag]] forState:UIControlStateNormal];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[button.titleLabel setFont:[UIFont boldSystemFontOfSize:20.0f]];
[button setTitleEdgeInsets:UIEdgeInsetsMake(45, 0, 0, 25)];

your title on button and on title your all image it there so add label on the image like this.
UIButton *myButton = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
[myButton setFrame:CGRectMake(0,3,70,32)];
[myButton setImage:[UIImage imageNamed:#"image.png"] forState:UIControlStateNormal];
[myButton addTarget:self action:#selector(compete) forControlEvents:UIControlEventTouchUpInside];
UILabel *Label=[[UILabel alloc]initWithFrame:CGRectMake(6, 5, 60, 20)];
[Label setText:#"label"];
Label.backgroundColor=[UIColor clearColor];
[Label setFont:[UIFont fontWithName:#"Helvetica-Bold" size:12]];
Label.textColor=[UIColor whiteColor];
[myButton addSubview:Label];
your all image above the title only so your title is in background so use label on image view.

If you are making GUI using interface builder, then click on the label and go to Editor-> Arrange -> Send to Front.

Related

UIButton works with one background image, not with another

I have a UIButton being created programmatically with a background image. Here is the code:
self.shiftb = [UIButton buttonWithType:UIButtonTypeSystem];
[self.shiftb setBackgroundImage:[UIImage imageNamed:#"back.png"] forState:UIControlStateNormal];
[self.shiftb setTitle:#"sh" forState:UIControlStateNormal];
[self.shiftb.titleLabel setTextColor:[UIColor whiteColor]];
self.shiftb.titleLabel.font = [UIFont systemFontOfSize:30];
[self.shiftb setFrame:CGRectMake(0*ss.size.width/10, 2*ss.size.height/4, 1.5*ss.size.width/10,ss.size.height/4)];
[self.shiftb addTarget:self action:#selector(shift) forControlEvents:UIControlEventTouchDown];
[self.view addSubview:self.shiftb];
As you can see, i'm setting the background image to #"back.png". The above code works perfectly.
if I set it to #"shift1.png" like this:
[self.shiftb setBackgroundImage:[UIImage imageNamed:#"shift1.png"] forState:UIControlStateNormal];
When I use this code, the button appears with no background at all. Both images exist, in the same directory, and they are both the same size. Why could this be happening?

how to connect custom button with next viewcontroller in xcode 5

I created a custom button as shown in below:
-(void)addYellowBtn{
UIButton *customButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[customButton setBackgroundImage:[UIImage imageNamed: #"yellow3.jpg"]forState:UIControlStateNormal];
//sets background image for highlighted state
[customButton setBackgroundImage:[UIImage imageNamed: #"yellow5.jpg"] forState:UIControlStateHighlighted];
[customButton setFrame:CGRectMake(60, 100, 60, 60)];
[customButton setTitle:#"" forState:UIControlStateNormal];
CALayer *btnLayer = [customButton layer];
[btnLayer setMasksToBounds:YES];
[btnLayer setCornerRadius:12.0f];
[self.view addSubview:customButton];
[customButton addTarget:self action:(btnclick1:) forControlEvents:UIControlStateNormal];
}
when i click this button, next view will have to push.To do this,how can i write codings?
Change this:
[customButton addTarget:self action:(btnclick1:) forControlEvents:UIControlStateNormal];
to this
[customButton addTarget:self action:(btnclick1:) forControlEvents: UIControlEventTouchUpInside];
UIControlStateNormal is not a touch event.

create hard custom button

I can not figure out how to construct this button. I need help in the creation of this, note that the strip is not painted when the button is active. What elements should I use?
on link you can look this button
http://i.stack.imgur.com/0mmJ0.png
UIButton *clone = [[UIButton alloc] initWithFrame:frame];
[clone setTitle:strDate forState:UIControlStateNormal];
[clone setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
clone.frame = CGRectMake(19, post_offer_top*(i-1)+22, 281, 75);
clone.tag = my_id;
// [clone setTitle:[[buttons objectAtIndex:i-1] objectForKey:#"title"] forState:UIControlStateNormal];
clone.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
clone.contentEdgeInsets = UIEdgeInsetsMake(44, 31, 0, 0);
[clone setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[clone setTitleShadowColor:[UIColor whiteColor] forState:UIControlStateNormal];
[clone.titleLabel setFont:[UIFont fontWithName:#"PF DinDisplay Pro" size:10]];
[clone.titleLabel setShadowOffset:CGSizeMake(0.0f, 1.0f)];
any ideas?
You should initialize buttons like this [UIButton buttonWithType:UIButtonTypeCustom]

Special tag for UIButton?

I have 3 different UIButtons for each image grouped. I have IDs for each image. Right now, I have a special id for each image, and I set the button with that tag.
I want to change the background image of the one selected when you tap it. The problem is, is that 3 buttons have the same tag so I cannot change the right button's background image.
Here's what I have:
UIButton *likeButton = [UIButton buttonWithType:UIButtonTypeCustom];
[likeButton setBackgroundColor:[UIColor clearColor]];
[likeButton setBackgroundImage:[UIImage imageNamed:#"icon_like_button.png"] forState:UIControlStateNormal];
[likeButton setBackgroundImage:[UIImage imageNamed:#"icon_like_button_hit.png"] forState:UIControlStateSelected];
[likeButton setBackgroundImage:[UIImage imageNamed:#"icon_like_button_hit.png"] forState:UIControlStateHighlighted];
[likeButton setBackgroundImage:[UIImage imageNamed:#"icon_like_button_hit.png"] forState:UIControlStateDisabled];
[likeButton setFrame:CGRectMake(13, 52 + (285 * count), 51, 55)];
[likeButton addTarget:self action:#selector(likeDudle:) forControlEvents:UIControlEventTouchDown];
[likeButton setTag:theIdInt];
[likeButton setTitle:#"no_like" forState:UIControlStateNormal];
[scrollView addSubview:likeButton];
- (IBAction)likeDudle: (id)sender {
NSInteger tagId = ((UIControl*)sender).tag;
UIButton *tempButton = (UIButton*)[scrollView viewWithTag:tagId];
NSLog(#"likeDudle: %d // %#", tagId, tempButton.titleLabel.text);
if ([tempButton.titleLabel.text isEqualToString:#"no_like"]) {
[tempButton setBackgroundImage:[UIImage imageNamed:#"icon_like_button_hit.png"] forState:UIControlStateNormal];
[tempButton setTitle:#"like" forState:UIControlStateNormal];
} else if ([tempButton.titleLabel.text isEqualToString:#"like"]) {
[tempButton setBackgroundImage:[UIImage imageNamed:#"icon_like_button.png"] forState:UIControlStateNormal];
[tempButton setTitle:#"no_like" forState:UIControlStateNormal];
}
Is there a better way in doing this?
Thanks,
Coulton
If you have fewer than 10 images, then give the ith button the tag i*10+image.tag. Then you can retrieve the image.tag by button.tag % 10 and the button tags will be unique. You can even retrieve the button only information by int b = button.tag/10.
Also, you can access the button's image's tag with button.backgroundImage.tag, so then the button could have its own separate tagging system, depending on your uses.

Programmatically Create Button to URL

I've managed to create a simple rounded rect button in Xcode through code but I'm having a little trouble actually getting it to do more what I want.
Ideal situation is a custom button type with image in both states as image.png linking to a URL (say www.google.com) can anyone help me out? I presume I need to write a method to do the link. That I can handle, just not sure how to hook it up without Interface Builder (targetting iOS)
Existing Code
UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn addTarget:self action:#selector(supporturl) forControlEvents:UIControlEventTouchDown];
btn.frame = CGRectMake(20, 20, 150, 50);
[btn setTitle:#"Support and Help" forState:UIControlStateNormal];
[self addSubview:btn];
UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(20.0f, 20.0f, 150.0f, 50.0f)];
[btn setTitle:#"Support and Help" forState:UIControlStateNormal];
[btn setImage:[UIImage imageNamed:#"image.png"] forState:UIControlStateNormal];
[btn addTarget:self action:#selector(openWebPage) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
[btn release];
and in function
-(void) openWebPage
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"http://www.google.com"]];
}