button label colour change after click IOS - ios

I have a piece of code which creates a button, in the right space size colour etc:
UIButton *learnmorebutton = [UIButton buttonWithType:UIButtonTypeCustom];
[self.view addSubview:learnmorebutton];
learnmorebutton.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.3];
learnmorebutton.frame = CGRectMake(0.0, 518.0, 159.0, 50.0);
[learnmorebutton setTitle:#"learn more" forState:UIControlStateNormal];
[learnmorebutton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
learnmorebutton.titleLabel.font = [UIFont fontWithName:#"Helvetica Light" size:17.0];
I then have a trigger for when the button is pressed:
[learnmorebutton addTarget:self action:#selector(learnMoreClickEvent:) forControlEvents:UIControlEventTouchUpInside];
Then I want the buttons background to change and also the text colour, so:
sender.titleLabel.textColor = [UIColor redColor];
sender.backgroundColor = [UIColor whiteColor];
The background changes colour but the text disappears. Like its gone white too.

You need to set the color of the label via
[sender setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
The button accept no color change on the label directly cause its appearance changes with his state. That is why you need to use the specific UIButton API for this.

If you asked "how to change the color when the button is pressed" you can easily change it like this:
self.titleLabel.textColor = [UIColor redColor];
self.*yourlabel*.textColor = [UIColor *desiredColor*];

[learnmorebutton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
Change to
[learnmorebutton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
when you click on uibutton get Highlight with white color that why your text looks like text disappears.

Related

Image and Title to UIbutton is not being set

I'm trying to implement an image as well as title on UIButton. Size of UIButton is 86 x 96. I need to embed an image of size 86 x 76 on top and title of size 86 x 20 just below the image.
The code is
UIButton *generalButton = [UIButton buttonWithType:UIButtonTypeCustom];
[generalButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
generalButton.titleLabel.font = [UIFont fontWithName:#"Arial" size:13];
[generalButton setImageEdgeInsets:UIEdgeInsetsMake(0,0,20,0)];
[generalButton setTitleEdgeInsets:UIEdgeInsetsMake(76,0,0,0)];
generalButton.frame = CGRectMake(32,20,86,96);
[generalButton setImage:[UIImage imageNamed:#"flower_icon.png"] forState:UIControlStateNormal];
[generalButton setTitle:#"Flower" forState:UIControlStateNormal];
[self addSubview:generalButton];
But the problem is that only image is being shown, whereas the title is not showing up. And if I hide the image by commenting the code, then title shows up perfectly.
Please Help
Please try this code:
UIButton *generalButton = [UIButton buttonWithType:UIButtonTypeCustom];
[generalButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
generalButton.titleLabel.font = [UIFont fontWithName:#"Arial" size:13];
[generalButton setImageEdgeInsets:UIEdgeInsetsMake(20,22,20,0)];
[generalButton setTitleEdgeInsets:UIEdgeInsetsMake(50,-30,0,0)];
generalButton.frame = CGRectMake(32,20,86,96);
[generalButton setBackgroundColor:[UIColor yellowColor]];
[generalButton setImage:[UIImage imageNamed:#"Facebook"] forState:UIControlStateNormal];
[generalButton setTitle:#"Flower" forState:UIControlStateNormal];
[self.view addSubview:generalButton];
Note:
[generalButton setImageEdgeInsets:UIEdgeInsetsMake(20,22,20,0)];
[generalButton setTitleEdgeInsets:UIEdgeInsetsMake(50,-30,0,0)];
These two line are performing main role to repositioning your title and image so set all parameters as per your image height & width and title length.
In this code I change image name as I have and also changed back colour of button so please change according to your data.
Hope this will help you.
try this code:
UIButton *generalButton = [UIButton buttonWithType:UIButtonTypeCustom];
[generalButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
generalButton.titleLabel.font = [UIFont fontWithName:#"Arial" size:13];
[generalButton setImageEdgeInsets:UIEdgeInsetsMake(0,0,20,0)];
[generalButton setTitleEdgeInsets:UIEdgeInsetsMake(30,-50,0,0)];
generalButton.frame = CGRectMake(32,20,86,96);
[generalButton setImage:[UIImage imageNamed:#"RepresentativeIcon.png"] forState:UIControlStateNormal];
[generalButton setTitle:#"Flower" forState:UIControlStateNormal];
[self.view addSubview:generalButton];

iOS bordered button behaviour on tap

I need a pre-iOS UIButton with border in my app. I use:
btn.layer.borderColor = [UIColor blackColor].CGColor;
btn.layer.borderWidth = 1.0;
btn.layer.cornerRadius = 10;
This works ok but on tap the border does not flash(button highlight state). Any way to easily implement this ?
Implement this when creating the button:
[self.botonConfigurar addTarget:self action:#selector(setBgColorForButton:) forControlEvents:UIControlEventTouchDown];
[self.botonConfigurar addTarget:self action:#selector(clearBgColorForButton:) forControlEvents:UIControlEventTouchDragExit];
[self.botonConfigurar addTarget:self action:#selector(setBgColorForButton:) forControlEvents:UIControlEventTouchDragEnter];
and these 2 functions:
-(void)setBgColorForButton:(UIButton*)sender {
[self.botonConfigurar setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
self.botonConfigurar.layer.backgroundColor = [UIColor colorWithRed:89/255.0 green:194/255.0 blue:237/255.0 alpha:1.0].CGColor;
}
-(void)clearBgColorForButton:(UIButton*)sender {
[self.botonConfigurar setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
self.botonConfigurar.layer.backgroundColor = [UIColor colorWithRed:61/255.0 green:169/255.0 blue:222/255.0 alpha:1].CGColor;
}
Do whatever you want with the button inside these functions.
PD: Make your button a [UIButton buttonWithType:UIButtonTypeCustom]
PD2: In clearBgColorForButton:sender write your UIButton's original parameters.
This is Just Example
[button setBackgroundImage:[self imageWithColor:[UIColor blueColor]] forState:UIControlStateHighlighted];

Programmatically changing UIButton font color depending on state

I'm trying to programmatically create a UIButton. However by default it's turning up white (which is the default color for my navigation bar, I feel that's relevant). I want it to just be Apple's default blue color in iOS7. I've managed to change the color for it's default state, but the moment I select it the text becomes white again. I cannot figure out how to keep it blue.
Could someone please explain to me how I can programmatically create a UIButton and have it act the same as though I created it in storyboard?
Current code:
UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
cancelButton.frame = CGRectMake(320 - 150, 0, 140, 40);
[cancelButton setTitle:#"Cancel" forState:UIControlStateNormal];
cancelButton.titleLabel.tintColor = [UIColor colorWithRed:0.0 green:122.0/255.0 blue:1.0 alpha:1.0];
cancelButton.titleLabel.textColor = [UIColor colorWithRed:0.0 green:122.0/255.0 blue:1.0 alpha:1.0];
Thank you.
Try this code :
UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
[cancelButton setFrame:CGRectMake(320 - 150, 0, 140, 40)];
[cancelButton setBackgroundColor:[UIColor clearColor]];
[cancelButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[cancelButton setTitleColor:[UIColor blueColor] forState:UIControlStateHighlighted]; // This will helps you during click time title color will be blue color
[cancelButton setTitle:#"Cancel" forState:UIControlStateNormal];
[cancelButton addTarget:self action:#selector(button_Action) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:cancelButton];
Just tried with
[cancelButton setTitleColor:[UIColor colorWithRed:0.0 green:122.0/255.0 blue:1.0/255.0 alpha:1.0] forState:UIControlStateNormal];
For more information read official documentation of UIButton.
Have you tried this
[cancelButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[cancelButton setTitleColor:[UIColor blueColor] forState:UIControlStateSelected];
This will solve your problem,
[cancelButton setTitleColor:YOUR COLOR forState:SPECIFIED STATE];
The state values are :
UIControlStateNormal
UIControlStateHighlighted
UIControlStateDisabled
UIControlStateSelected
Try using
- (void)setTitleColor:(UIColor *)color forState:(UIControlState)state
for specifying colors for all the required states.
Swift:
let button = UIButton (type: UIButtonType.system)
This will create a button that uses the standard tint color just like a button added to a storyboard in interface builder.

Switched from UILabel to UIButton. Text disappears

I just switched from a UILabel to UIButton, intending to use the button's titleLabel property to display the information formerly displayed in the label replaced by the button.
Problem is, the text doesn't show up. I've checked to see if the button itself (normally with a transparent background, changed to red to check) is appearing, and it is, but the text isn't there. Here's a sample of the relevant code, which is identical to the original (working) code from the label, changing only lines like:
UILabel *firstLabel;
(...)
firstLabel.textColor = [UIColor blackColor];
to:
UIButton *firstLabel;
(...)
firstLabel.titleLabel.textColor = [UIColor blackColor];
Here's a full chunk for clarity:
firstLabel.frame = CGRectMake(thisRiser.bounds.origin.x, thisRiser.frame.size.height -focusBarEndHeight - 55, barWidth, 60);
firstLabel.backgroundColor = [UIColor clearColor];
firstLabel.titleLabel.textColor = [UIColor blackColor];
firstLabel.titleLabel.text = [NSString stringWithFormat:#"%#\n%.2f%%\nTime--%#",focusItemName,focusItemPercent,actualDurationFocusItem];
[firstLabel.titleLabel setFont:[UIFont boldSystemFontOfSize:12]];
firstLabel.titleLabel.numberOfLines = 0;
firstLabel.titleLabel.textAlignment = NSTextAlignmentCenter;
[firstLabel setHidden:NO];
What am I missing? Any ideas?
Thanks!
UPDATE --
This may be related to a known bug!
Many thanks to the responders below. I implemented the first recommended fix, which resolved my initial problem, so my text now appears in the label. However, the UIControlStateHighlighted and UIControlStateSelected don't work.
For example, this code produces no discernible effect to the text when clicked:
firstLabel.backgroundColor = [UIColor clearColor];
[firstLabel setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[firstLabel setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
[firstLabel setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
After searching around SO for a while, I'm coming to the conclusion that there is a bug (at least in iOS 7) that prevents proper functioning of these methods. Unfortunately, I'm not smart enough to provide more detailed information, but plenty of recent posts point to a major problem in this area of UIControl.
I'll be ecstatic to be proven wrong, because I'd like to use this functionality.
UIButton responds to different messages, you can use the button state to change its title.
You can set the title like this;
UIButton *button = [UIButton buttonWithType: UIButtonTypeCustom];
[button setTitle:#"Title goes here" forState:UIControlStateNormal];
See Control State: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIControl_Class/Reference/Reference.html#//apple_ref/c/tdef/UIControlState
To set a title in an UIButton, you have to init the button:
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(10, 10, 60, 20)];
and then use:
[button setTitle:#"text" forState:UIControlStateNormal];
where you must specific the control state (highlighted, selected, etc.)
try for example,
UIButton *firstLabel = [UIButton buttonWithType:UIButtonTypeCustom];
firstLabel.frame = CGRectMake(0, 60, 100, 50); //set the frame
[firstLabel setTitle:#"Hello" forState:UIControlStateNormal];
[firstLabel setBackgroundColor:[UIColor redColor]];//for test
[firstLabel setTitleColor:[UIColor greenColor] forState:UIControlStateNormal]; //it will always displays green for normal
[firstLabel setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];//if u touch it text changed to white instantly
[firstLabel setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];//if touch it and hold it shows white color

Custom UIButton in UIscrollview

I am using xcode 4.6 to develop an app. Here i want to add UIButton programmatically to UIscrollview. This is the code i follow.
UIButton *bt =[[UIButton alloc]initWithFrame:frame];
bt=[UIButton buttonWithType:UIButtonTypeCustom];
[bt setTitle:#"Custom Button" forState:UIControlStateNormal];
[bt addTarget:self action:#selector(userTappedOnLink:) forControlEvents:UIControlEventTouchUpInside];
bt.backgroundColor = [UIColor grayColor];
bt.titleLabel.textColor=[UIColor blueColor];
[self.mainscrollview addSubview:bt];
[self.mainscrollview bringSubviewToFront:bt];
Now the problem is that Button gets disappeared (technically its textcolor becomes white) on click. I checked keeping UIscrollview color to red that th button was still in the view but i cant get the reason why its text color changed and how do i undo dis.
Basically I wan to create a clickable link using UIbutton.
I know uitextview approach (datadetectortype) but its of no use as i want to show different text in the label for the link and the actual link.
Note: The textcolor doesnt change back to blue and remains white only.
Thanks in advance.
Try the below code
UIButton *bt =[UIButton buttonWithType:UIButtonTypeCustom];
bt.frame = CGRectMake(50.0, 50.0, 100.0, 50.0);
[bt setTitle:#"Custom Button" forState:UIControlStateNormal];
[bt addTarget:self action:#selector(userTappedOnLink:) forControlEvents:UIControlEventTouchUpInside];
bt.backgroundColor = [UIColor grayColor];
bt.titleLabel.textColor=[UIColor blueColor];
[self.scrollTest addSubview:bt];
-(void)userTappedOnLink:(UIButton*)sender
{
NSLog(#"Test ..");
[self performSelector:#selector(changeBtnTextColor:) withObject:sender afterDelay:1.0];
}
-(void)changeBtnTextColor:(UIButton*)btn
{
btn.titleLabel.textColor=[UIColor blueColor];
}
hope it will work for you.
use below code you will get your solution
UIButton *bt =[[UIButton alloc]initWithFrame:frame];
bt=[UIButton buttonWithType:UIButtonTypeCustom];
[bt setTitle:#"Custom Button" forState:UIControlStateNormal];
[bt addTarget:self action:#selector(userTappedOnLink:) forControlEvents:UIControlEventTouchUpInside];
[bt setBackgroundColor:[UIColor grayColor]];
[bt setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[self.mainscrollview addSubview:bt];
[self.mainscrollview bringSubviewToFront:bt];
Use the below code:
[bt setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
Problem is you are set Title to titleLabel and you are changing textColor of buttonTitle color.
All the best !!!
check Button's Fram .. is it valid or not ??
and remove bt=[UIButton buttonWithType:UIButtonTypeCustom]; line from your code.

Resources