UIButton set Text on top of image - ios

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 !

try setting the backgroundImage property of button
[self.buttonWithTextAndImage setBackgroundImage:[UIImage imageNamed:#"image.png"] forState:UIControlStateNormal];
[self.buttonWithTextAndImage setTitle:#"Button Title" forState:UIControlStateNormal];

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

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];

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];

[yourButton setBackgroundImage:[UIImage imageNamed:#"test.png"] forState:UIControlStateNormal];
[yourButton setTitle:#"Click" forState:UIControlStateNormal];
This must work.

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];

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];

Related

Objective C How can we change the background image of a button in just when it is clicked and then go back to normal?

[myButton setImage:[UIImage imageNamed:#"btn_normal"] forState:UIControlStateNormal];
[myButton setImage:[UIImage imageNamed:#"btn_highlighted"] forState:UIControlStateHighlighted];
[myButton setImage:[UIImage imageNamed:#"btn_highlighted"] forState:UIControlStateSelected];
myButton.showsTouchWhenHighlighted = YES;
I have tried this code and it is not working.
You should use the setBackgroundImage:forState. The setImage:forState changes the image which is on the same depth level as the button title. Additionally the backgroundImage is automatically fit to the button's view bounds. The image is not.
Implement onTouchDown action of that button and set button background image in that function by [btn setBackgroundImage: forState:]

any example for creating an iOS custom button?

I would like to create a custom button, which have a custom background image, icon and a label.
It looks very similar like the Twitter button i found:
Any good suggestion is appreciated!
You need to import quartz
#import <QuartzCore/QuartzCore.h>
then to create button like your twitter button simply write this code:
UIButton *yourButton = [UIButton buttonWithType:UIButtonTypeCustom];
yourButton.frame = CGRectMake(50, 50, 100, 50);
yourButton.layer.cornerRadius = 10;
yourButton.clipsToBounds = YES;
yourButton.backgroundColor = [UIColor colorWithRed:57.0/255.0 green:178.0/255.0 blue:235.0/255.0 alpha:1.0];
//if you want to set image as background
//[yourButton setBackgroundImage:[UIImage imageNamed:#"background_image"] forState:UIControlStateNormal]
[yourButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[yourButton setTitle:#"Twitter" forState:UIControlStateNormal];
[yourButton setImage:[UIImage imageNamed:#"twitter_icon"] forState:UIControlStateNormal];
Yes, you can set the button type to "custom" in interface builder. Then you can set the background image, icon, text, and colors to whatever you prefer.
To get rounded corners, you'll have to add some code:
button.layer.cornerRadius = 10; // Change to the value you desire.
button.clipsToBounds = YES;

Cocoa Hyperlink label issue?

I have a hyperlink label in my application.Clicking on it opens the application i want.
The problem is after i click on it the font size of the label shrinks automatically.
Is there a way to avoid this??
Please suggest a solution for this issue. Thanks in advance
I would suggest you to use UIButton with UIButtonTypeCustom and clear background instead of UILabel. It will look like UILabel and in the action method you can perform any task.
UIButton *btn = [[UIButton alloc] initWithFrame:btn_frame];
[btn setBackgroundColor:[UIColor clearColor]];
[btn addTarget:self action:#selector(DoSomething) forControlEvents:UIControlEventTouchUpInside];
[btn setTitle: #"Button_Title" forState: UIControlStateNormal];
[btn setTitleColor: #your_choice forState:UIControlStateNormal];
[btn setTitleColor: #your_choice forState:UIControlStateHighlighted];
btn.titleLabel.font = #your_choice
btn.titleLabel.textAlignment = #your_choice;
btn.contentVerticalAlignment = #your_choice;
if still you want UILabel here is link might help you..
Attributed Label

How to change the UIButton color in iOS based on selection? for the specified UI

I am working on a tab like appearance in ios 7, am not able to make the selection of button to change its colour. i have already tried setting the different colours for each state.
i want to know how can i change the selected button to a colour and all other buttons to black colour.
I cant get what you are exactly looking for, Try this it will hopefully helps you,..
- (IBAction)clickButton:(id)sender {
if (button.state == UIControlStateHighlighted) {
[button setBackgroundColor:[UIColor COLOR_YOU_WANT]];
(or)
[button setBackgroundImage:[self imageWithColor:[UIColor COLOR_YOU_WANT]] forState:UIControlStateHighlighted];
(or)
[button setBackgroundImage:[UIImage imageNamed#"ColourImage.png"] forState:UIControlStateHighlighted];
}
}
If you're using images, you can use:
[myButton setBackgroundImage:[UIImage imageNamed#"Collor1"] forState:(some state)];
Where the state could be one of these:
UIControlStateDisabled
UIControlStateHighlighted
UIControlStateSelected
UIControlStateNormal
[myButton setBackgroundImage:[self imageWithColor:[UIColor redColor]] forState:UIControlStateSelected];
first you should use custom button, then on selection button you should use below code in target method :
[myButton setBackgroundColor:[UIColor YOUR_CHOICE_COLOR] forState:UIControlStateNormal];
I hope it will work. If any clarification needed then let me know..

Why is my UIButton.tintColor not working?

My build target is set for IOS5 which is where I understand UIButton.tintColor was introduced...
I have this in my viewDidLoad for the View Controller
[timelineButton setTintColor:[UIColor blackColor]];
[timelineButton setTitle:#"test" forState:UIControlStateNormal];
The text changes properly but the button isn't black?
Thanks!
According to the documentation:
This property is not valid for all button types.
You need to switch your buttonType to another one of these. (Unfortunately, the documentation does not specify which specific button types support this property.)
Make sure your button "type" is set to System. If it is set to Custom it could be the reason the color of your button isn't changing. This is what happened to me and changing the type to System fixed it.
It tint your highlighted State color.
When you tap/click on the UIButton the color specified with tintColor appears as long as you hold the tap/click on the UIButton.
resetButton.tintColor = [UIColor colorWithRed:0.764 green:1.000 blue:0.000 alpha:1.000];
The button is white in normal state.
But if I tap on the button the color turns red, but only then.
IF you need to change the button so it looks like a red or blue one in the UIControlStateNormal then
Change the UIButtonType to UIButtonTypeCustom in Interface Builder or programmatically with
UIButton *resetButton = [UIButton buttonWithType:UIButtonTypeCustom];
Change the attributes on your own and recreate the rounded corners
resetButton.backgroundColor = [UIColor redColor];
resetButton.layer.borderColor = [UIColor blackColor].CGColor;
resetButton.layer.borderWidth = 0.5f;
resetButton.layer.cornerRadius = 10.0f;
As stated in other answers tint does not work for Custom Button types. Make sure you explicitly declare the button type. Do not just use [UIButton alloc] init]
This will work:
UIButton *mybutton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[mybutton setImage:[UIImage imageNamed:#"myImage"] forState:UIControlStateNormal];
mybutton.tintColor = [ODTheme blueTintColor];
I found 3 things must be followed.
1. Render image as Default
Go to Images.xcassets > Your Image > Show the Attributes inspector > Render As > Default
2. Make sure your button type is System
3. Now change button's tintColor.
Done.
Today, I also meet this problem. I use delegate to solve it.
[button addTarget:self action:#selector(buttonPress:) forControlEvents:UIControlEventTouchDown];
[button addTarget:self action:#selector(buttonPressReset:) forControlEvents:UIControlEventTouchUpInside | UIControlEventTouchUpOutside];
-(void)buttonPress:(id)sender{
UIButton* button = (UIButton*)sender;
[button setBackgroundColor:[UIColor greenColor]];
NSLog(#"buttonPressed");
}
-(void)buttonPressReset:(id)sender{
UIButton* button = (UIButton*)sender;
[button setBackgroundColor:[UIColor redColor]];
NSLog(#"buttonPressReset");
}
Should try this method:
- (void)setTitleColor:(UIColor *)color
forState:(UIControlState)state
In iOS 13 you can tint the image and then set it to the button:
let coloredImage = UIImage().withTintColor(UIColor.red)
UIButton().setImage(coloredImage, for: .normal)
Simply set your UIButton to type System in Storyboard.
And then in code just use:
myButton.tintColor = UIColor.whiteColor()
If your UIButton type is system then and then only tint colour property is work. So first you need to set your button type to the system then apply tint colour for the specific state.
let btn = UIButton.init(type: .system)
[btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
To change the color of the Button text you can use:
resetButton.setTitleColor(UIColor.blackColor(), forState: .Normal)
Or OBJ-C:
- (void)setTitleColor:(UIColor *)color
forState:(UIControlState)state
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIButton_Class/#//apple_ref/occ/instm/UIButton/setTitleColor:forState:
Simply use:
[yourButton setBackgroundImage:[yourButton.currentBackgroundImage imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate] forState:UIControlStateNormal];
[yourButton setTintColor:[UIColor blackColor]];
Swift 4 :
yourButton.setTitleColor(UIColor.white, for: .normal)
I couldn't really change the button type to System as I was inheriting from a closed internal library.
So, I changed the appearance API for UIButton
UIView.appearance(whenContainedInInstancesOf: [UIButton.self]).tintColor = .white

Resources