UIButton - Keep text black when touched - iphone

I have an iPhone UIButton (Custom) which has a background image and text.
When touched, the image darkens (Good) but the text goes from the set Black to white.
How do I keep the text the same black so that when the button is touched, only the image changes color.

Most of the times, the following line will do:
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
If nothing happens, then use either this:
[button setTitleColor:[UIColor blackColor] forState:(UIControlStateSelected | UIControlStateHighlighted | UIControlStateNormal)];
Or this will do the trick:
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateSelected];
See comments below for reason this answer was edited/expanded to include the 2 separate lines.

[button setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted]

okButton.titleLabel.textColor = [UIColor redColor];
[okButton addTarget:self action:#selector(isPressingForgetButton:) forControlEvents:UIControlEventTouchDown];
[okButton addTarget:self action:#selector(didPressForgetButton:) forControlEvents:UIControlEventTouchUpInside];
- (void) isPressingForgetButton:(id)sender
{
UIButton * bt = (UIButton *)sender;
bt.titleLabel.textColor = [UIColor greenColor];
}
- (void) didPressForgetButton:(id)sender
{
UIButton * bt = (UIButton *)sender;
bt.titleLabel.textColor = [UIColor redColor];
[self gotoUnblockView];
}

Related

How do I get a UIButton to change its color by clicking it?

I want to make an app for iphone. It is a Nonogram something similar to this: http://www.puzzle-nonograms.com/ How can I make the UIButtons changes their color from white to black and reverse?
.h file
UIButton *button;
BOOL clicked;
.m file
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
clicked=YES;
button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:#selector(changeBtnColor)
forControlEvents:UIControlEventTouchDown];
[button setTitle:#" 1 " forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
[self.view addSubview:button];
}
-(void)changeBtnColor{
if (clicked) {
[button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
clicked=NO;
}
else{
[button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
clicked=YES;
}
}
I hope it will help. Best of luck..
No need to ivar a boolean, buttons have selected, highlighted and normal states built into them.
- (void)viewDidLoad
{
[super viewDidLoad];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setFrame:CGRectMake(0, 0, 320, 50)];
[button addTarget:self action:#selector(buttonPress:) forControlEvents:UIControlEventTouchUpInside];
[button setTitle:#"Normal State" forState:UIControlStateNormal];
[button setTitle:#"Selected State" forState:UIControlStateSelected];
[button setBackgroundImage:[UIImage imageNamed:#"normal_image"] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:#"selected_image"] forState:UIControlStateSelected];
[self.view addSubview:button];
}
-(void)buttonPress:(UIButton *)button{
button.selected = !button.selected;
//do your other code here for button selection
}
Otherwise set up the button states inside interface builder and just have the buttonPress selector handle the state change.

What's the right UIControlState for the UIButton being pressed?

I'm customizing the UIButton programmatically here:
button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setSelected:YES];
button.frame = CGRectMake(x, y, width, height);
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[button setTitleColor:[UIColor blueColor] forState:UIControlStateSelected];
[button setTitle:#"Button Title" forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[button setBackgroundImage:[UIImage imageNamed:#"button.png"] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:#"buttonActive.png"] forState:UIControlStateSelected];
[button setBackgroundImage:[UIImage imageNamed:#"buttonActive.png"] forState:UIControlStateHighlighted];
[button setBackgroundImage:[UIImage imageNamed:#"buttonActive.png"] forState:UIControlStateDisabled];
The issue is if I'm holding down the image background image disappears until I'm releasing it...
I think you're in overkill mode :). Try setting button.png for UIControlStateNormal and buttonActive.png for UIControlStateHighlighted. No need for the rest. See if this works.
EDIT:
Also, remember: Image file names are case sensitive
Are you testing on device? Image names are case sensitive for device builds, but not for the simulator. For example, if your actual image file is named buttonactive.png, but you're calling it as buttonActive.png from your code, it will show up on the simulator, but not on the device.
Please ensure that the case for both image names matches the name of the actual file.
EDIT #2:
Try this code
button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setSelected:YES];
button.frame = CGRectMake(x, y, width, height);
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[button setTitleColor:[UIColor blueColor] forState:UIControlStateSelected];
[button setTitle:#"Button Title" forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[button setBackgroundImage:[UIImage imageNamed:#"button.png"] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:#"buttonActive.png"] forState:UIControlStateHighlighted];
Figure it out, it works in this way:
[_whateverButtonTab setBackgroundImage:[UIImage imageNamed:#"ActivateButton.png"] forState:UIControlStateSelected];
[_whateverButtonTab setBackgroundImage:[UIImage imageNamed:#"ActivateButton.png"] forState:(UIControlStateHighlighted|UIControlStateSelected)];
While we add a button from IDE
sample:
.h file
-(IBAction)BtnAdd:(id)sender
in .m file it is
-(IBAction)BtnAdd:(id)sender
{
}
It is a method that cannot enable or disable.
so if you want to enable or disable button make it as -(IBOutlet)BtnAdd
add IBOutlet to .h file And connect it to the particular button
then BtnAdd.enabled=NO will work

How to set the buttons label text color for state UIControlStateHighlighted

I am creating an iPhone application in which i have a custom button. i have set the buttons title by creating a Label and adding it as subview. now when the button is highlighted i want to change the labels text color.
here is my code,
UIButton *button1= [UIButton buttonWithType:UIButtonTypeCustom];
[button1 setFrame:CGRectMake(68,162, 635, 101)];
[button1 setImage:[UIImage imageNamed:#"startwithouttext.png"] forState:UIControlStateNormal];
[button1 setImage:[UIImage imageNamed:#"startactivewithouttext.png"] forState:UIControlStateHighlighted];
UILabel *buttonLabel = [[UILabel alloc] initWithFrame:CGRectMake(button1.bounds.origin.x+50, button1.bounds.origin.y+20, button1.bounds.size.width-100, button1.bounds.size.height-40)];
[buttonLabel setFont:[UIFont fontWithName:#"Helvetica" size:28]];
buttonLabel.backgroundColor=[UIColor clearColor];
buttonLabel.textColor=[UIColor colorWithRed:83.0/255.0 green:83.0/255.0 blue:83.0/255.0 alpha:1.0];
buttonLabel.highlightedTextColor=[UIColor whiteColor];
buttonLabel.text = #"Long text string";
[button1 addSubview:buttonLabel];
[button1 bringSubviewToFront:buttonLabel];
[button1 setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
[button1 setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];
[button1 addTarget:self action:#selector(button1clicked:) forControlEvents:
[mainView button1];
can any body help me to change the text color when the button is highlighted?
Found the answer in a different question on StackOverflow:
UIButton color issues
[button1 setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
This is if you can work without creating a Label and adding it as subview as you mention above.
you can add target for UIControlStateHighlighted state of UIButton like
[button1 addTarget:self action:#selector(buttonHighlighted:) forControlEvents:UIControlStateHighlighted];
and in buttonHighlighted method you can change the color of your label's text
- (void) buttonHighlighted:(id)sender
{
//code here
}
Hope it gives you an idea.
For SelectedColor
[yourButton setTitleColor:[UIColor purpleColor] forState:UIControlStateSelected];
For HighlightedColor
[yourButton setTitleColor:[UIColor orangeColor] forState:UIControlStateHighlighted];

iPad - set text to a button programmatically

I'm new to this iPad business, and I'm creating some buttons inside a scrollview programmatically based on the contents of an XML file. I have this code on a for:
float x = (SLIDER_ELEMENT_HEIGHT * i) + 20;
CGRect frame = CGRectMake(x, 0, SLIDER_ELEMENT_WIDTH, SLIDER_ELEMENT_HEIGHT);
UIButton *button = [[UIButton alloc] initWithFrame:frame];
UIColor *bgColor = [[UIColor alloc] initWithRed:1.0 green:1.0 blue:1.0 alpha:1.0];
button.backgroundColor = bgColor;
NSString *titleForButton = [NSString stringWithFormat: #"This is my title"];
[button setTitle:titleForButton forState:(UIControlStateNormal | UIControlStateApplication | UIControlStateHighlighted)];
UIColor *fgColor = [[UIColor alloc] initWithRed:0.5 green:0.5 blue:0.5 alpha:1.0];
[button setTitleColor:fgColor forState:(UIControlStateNormal | UIControlStateApplication | UIControlStateHighlighted)];
[button addTarget:self action:#selector(buttonMethod:) forControlEvents:(UIControlEvents)UIControlEventTouchDown];
[scrl_lastIssues addSubview:button];
Now, the method listener I'm appending is working OK, but the text of the button never shows up... what am I doing wrong?
Try change the line:
[button setTitle:titleForButton forState:(UIControlStateNormal | UIControlStateApplication | UIControlStateHighlighted)];
to:
[button setTitle:titleForButton forState:UIControlStateNormal];
the forState parameter does not work the way you currently expected, it works like:
[button setTitle:#"Normal" forState:UIControlStateNormal];
[button setTitle:#"Highlighted" forState:UIControlStateHighlighted];
Also, if you only set the UIControlStateNormal text..it will be the text for all the control states that you have separated by bit wise ORs, which is, I believe, the functionality you are searching for.
[button setTitle:#"Return" forState:UIControlStateNormal];
[button setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
[button setBackgroundColor:[UIColor orangeColor]];
And if you want to set the same title for all the states then do this:
[button setTitle:titleForButton forState:UIControlStateNormal];
[button setTitle:titleForButton forState:UIControlStateHighlighted];

Why can't I set my button's shadow colour?

I'm trying to set the shadow colour on a UIButton, but all I seem to be able to get is a mid grey.
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(20, 20, 200, 100);
[button setTitle:#"a" forState:UIControlStateNormal];
[button addTarget:self
action:#selector(buttonPressed:)
forControlEvents:UIControlEventTouchUpInside];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[button.titleLabel setFont:[UIFont fontWithName:#"Helvetica" size:96]];
// set up the button colours
button.titleLabel.shadowColor = [UIColor blueColor];
[button.titleLabel setShadowOffset:CGSizeMake(5.0f, 5.0f)];
[self.view addSubview:button];
alt text http://img.skitch.com/20090825-xur3112ni5q2wrwwiix4jbcwc5.png
Am I setting the wrong property, or is it the way that I'm setting shadowColor that's wrong?
Thanks.
Have you tried:
[button setTitleShadowColor:[UIColor blueColor] forState:UIControlStateNormal];