swathy krishnan
swathy krishnan

Reputation: 916

UIButton set Text on top of image

How can i add a Text in a button which already contains an image. I've tried out various methods but the text is not visible. How can i make the text visible on top of the image ?

Can anyone help me !

Upvotes: 2

Views: 1581

Answers (7)

swathy krishnan
swathy krishnan

Reputation: 916

I have found a better way.

UILabel *next=[ [UILabel alloc] initWithFrame:CGRectMake(0,0,375,65)];
next.text=@"SIGN IN";
next.textAlignment = NSTextAlignmentCenter;
next.font=[UIFont fontWithName:@"Helvetica" size:20.0 ];
next.textColor=[UIColor whiteColor];
[self.button addSubview:next];

Upvotes: 0

Nirmalsinh Rathod
Nirmalsinh Rathod

Reputation: 5186

If your button's image set in setImage state, then the text is not going to display on Button.

If you have to display name on button then we have to set setBackgroundImage and then you can set the title for button.

[btn setBackgroundImage:YOURIMAGE forState:UIControlStateNormal];
[btn setTitle:YOURTITLE forState:UIControlStateNormal];

Upvotes: 1

kilian
kilian

Reputation: 29

something, that worked fine for me: I set the Button in InterfaceBuilder and use the tiny Icons near "Position" to define the position of the text. Mouseover shows the code which is applied by InterfaceBuilder. Now if the text doesn't show, due to color, i use a reference to the button and set an attributedTitle:

NSDictionary *atts = [[NSDictionary alloc]initWithObjectsAndKeys:[NSColor whiteColor], NSForegroundColorAttributeName, nil];
    NSAttributedString *example = [[NSAttributedString alloc]initWithString:@"myText" attributes:atts];
    [myExampleButton setAttributedTitle:example];

Upvotes: 0

Prajeet Shrestha
Prajeet Shrestha

Reputation: 8108

[yourButton setBackgroundImage:[UIImage imageNamed:@"test.png"] forState:UIControlStateNormal];
[yourButton setTitle:@"Click" forState:UIControlStateNormal];

This must work.

Upvotes: 0

Sanjay Mohnani
Sanjay Mohnani

Reputation: 5967

Set background image and title on a button as-

[self.m_myBetButton setBackgroundImage:[UIImage imageNamed:@"buttonImage.png"] forState:UIControlStateNormal];
    [self.m_myBetButton setTitle:@"BUTTON TITLE" forState:UIControlStateNormal];

Upvotes: 0

rdelmar
rdelmar

Reputation: 104092

You need to set the background image of the button, instead of the image, using setBackgroundImage:forState:

Upvotes: 1

Saif
Saif

Reputation: 2968

try setting the backgroundImage property of button

[self.buttonWithTextAndImage setBackgroundImage:[UIImage imageNamed:@"image.png"] forState:UIControlStateNormal];
[self.buttonWithTextAndImage setTitle:@"Button Title" forState:UIControlStateNormal]; 

Upvotes: 3

Related Questions