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];
Related
Hi I have a UIButton that I created programmatically but unfortunately its not working. When I click on the UIButton nothing happens. I don't know what is the problem. Any help would be appreciated.
UIButton *connectedStories = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//set the position of the button
connectedStories.frame = CGRectMake(10, 10, 1900, 30);
connectedStories.backgroundColor = [UIColor whiteColor];
[connectedStories setTitle:#"Button" forState:UIControlStateNormal];
[connectedStories setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[connectedStories addTarget:self action:#selector(buttonClicked) forControlEvents:UIControlEventTouchUpInside];
[label setText:storyTitle];
[label1 setText:storyDescription];
[contentView addSubview:backGroundImageView];
[contentView addSubview:connectedStories];
[contentView addSubview:label];
[contentView addSubview:label1];
[viewContainer addSubview:contentView];
return contentView;
- (void)buttonClicked
{
NSLog(#"button clicked");
}
I think labels recives touches instead of button.
Add userInteractionEnabled = NO; to your labels.
label.userInteractionEnabled = NO;
label1.userInteractionEnabled = NO;
Try changing the top part of your code to this:
CGRect frame = CGRectMake(10, 10, 1900, 30);
UIButton *connectedStories = [[UIButton alloc] initWithFrame:frame];
//set the position of the button
[connectedStories setBackgroundColor:[UIColor whiteColor]];
[connectedStories setTitle:#"Button" forState:UIControlStateNormal];
[connectedStories setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[connectedStories addTarget:nil action:#selector(buttonClicked) forControlEvents:UIControlEventTouchUpInside];
It may very well be the following:
[contentView addSubview:backGroundImageView];
[contentView addSubview:connectedStories];
[contentView addSubview:label];
[contentView addSubview:label1];
im sure you would need to add them in the following order:
[contentView addSubview:backGroundImageView];
[contentView addSubview:label];
[contentView addSubview:label1];
[contentView addSubview:connectedStories];
This is because when you call addSubview: it adds it to end of the receivers list of subViews and the most recent subView appears on top.
Check out the UIView Documentation here
the reason that the Interaction isn't being detected is that you are not passing the touchEvents through to the other subViews as they are being captured by label1 and stopping there.
By default the last subView captures all touch events and doesn't pass them on.
Add the colon in last of your method in selector
[connectedStories addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[label setText:storyTitle];
UIButton *custombutton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[custombutton addTarget:self
action:#selector(aMethod:)
forControlEvents:UIControlEventTouchUpInside];
[custombutton setTitle:#"Click" forState:UIControlStateNormal];
custombutton.frame = CGRectMake(80.0, 110.0, 160.0, 40.0);
custombutton.titleLabel.textColor = [UIColor colorWithRed: 2.0f/255.0f green: 155.0f/255.0f blue: 213.0f/255.0f alpha:1];
[custombutton setImage:[UIImage imageNamed:#"hh.png"] forState:UIControlStateNormal];
[view addSubview:custombutton];
I Have to set Title color for ffe1b1 to a UIButton.RGB value of ffe1b1 is 255,225,177.
I am trying this code, but its not properly reflect. I search on net Color components are floats between 0.0 and 1.0 !
So what is proper way for giving That RGB Values.Thanks
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom] ;
btn.frame = CGRectMake(134.0, 193.0, 80.0, 30.0);
[btn setBackgroundImage:img forState:UIControlStateNormal];
[btn setTitle:#"Contact" forState:UIControlStateNormal];
[btn setTitleColor:[UIColor colorWithRed:255.0 green:225.0 blue:177.0 alpha:0.6 ]forState: UIControlStateNormal];
Change the line like this
[btn setTitleColor:[UIColor colorWithRed:255.0 green:225.0 blue:177.0 alpha:0.6 ]forState: UIControlStateNormal];
to
[btn setTitleColor:[UIColor colorWithRed:(255.0/255) green:(225.0/255) blue:(177.0/255) alpha:0.6 ]forState: UIControlStateNormal];
in objective c you cannot directly use RGB values. follow this link:
http://www.touch-code-magazine.com/web-color-to-uicolor-convertor/
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
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];
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];
}