Change Existing Set UIColors on UIButton Press - ios

The objective is to change the font color and background color of my UITextView upon UIButton click and then revert them back to the original colors when the UIButton is clicked again; then repeat, ect.
Default Color Settings
Text: blackColor
Background: lightGrayColor
Upon Button Click (change to..)
Text: greenColor
Background: blackColor
(then if clicked again, change back to default color settings)
So far I have this:
- (IBAction) enableSleepMode:(id)sender {
[txtNotes setTextColor:[UIColor greenColor]];
[txtNotes setBackgroundColor:[UIColor blackColor]];
}
I apologize but I'm not exactly sure where to go from here. Thanks in advance.

You said to change the font color and background color of 'UITextView' not 'UIButton' Right? . Then did you add UItextViewDelegate?
I have done this like
EDIT:
- (void)viewDidLoad
{
[super viewDidLoad];
[txtView setBackgroundColor:[UIColor lightGrayColor]];
[txtView setTextColor:[UIColor blackColor]];
str = #"first";
}
-(IBAction)bt:(id)sender
{
if([str isEqualToString:#"first"])
{
[txtView setBackgroundColor:[UIColor blackColor]];
[txtView setTextColor:[UIColor greenColor]];
str = #"second";
}
else
{
[txtView setBackgroundColor:[UIColor lightGrayColor]];
[txtView setTextColor:[UIColor blackColor]];
str = #"first";
}
}

try this
(void)setTextColor:(UIColor *)color forState:(UIControlState)state
[button setTextColor:[UIColor greenColor] forState:UIControlStateNormal];
[button setTextColor:[UIColor blackColor] forState:UIControlStateHighlighted];
(void)setBackgroundColor:(UIColor *)color forState:(UIControlState)state
[button setBackgroundColor:[UIColor blackColor] forState:UIControlStateNormal];
[button setBackgroundColor:[UIColor lightgrayColor] forState:UIControlStateHighlighted];
something like that is i think what you're looking for

Related

UIButton set title color not working

I have created the array of UIButtons , having the same action,
if i click the first button, the first button Colour should be change, other button Colour should not be changed. how to achieve this?
for(titleStr in titleArray){
actionButton = [UIButton buttonWithType:UIButtonTypeCustom];
[actionButton addTarget:self action:#selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
actionButton.tag = count;
[actionButton setTitle:titleArray[count] forState:UIControlStateNormal];
[actionButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[actionButton setBackgroundColor:[UIColor clearColor]];//[UIColor greenColor]]; // AJS 3.3 change
CGSize expectedTitleSize = [actionButton.titleLabel sizeThatFits:maximumLabelSize];
CGRect buttonframe;
buttonframe=CGRectMake(contentOffset,0, expectedTitleSize.width+5.0, expectedTitleSize.height+25);
actionButton.frame = buttonframe;
[titlewidthArray addObject:[NSNumber numberWithFloat:expectedTitleSize.width]];
[contentoffsetArray addObject:[NSNumber numberWithFloat:contentOffset+3.0]];
if(count==0){
actionButton.titleLabel.font=[UIFont fontWithName:#"HelveticaNeue-Medium" size:15.0];
[actionButton setSelected:YES];
tempObj = actionButton;
}else {
actionButton.titleLabel.font=[UIFont fontWithName:#"HelveticaNeue-Medium" size:15.0];
}
[titleScrollView addSubview:actionButton];
contentOffset += actionButton.frame.size.width+5.0;
titleScrollView.contentSize = CGSizeMake(contentOffset, titleScrollView.frame.size.height);
// Increase the count value
count++;
}
-(void)buttonTapped:(id)sender {
if([sender tag]==0){
UIButton *tappedButton = (UIButton *)sender;
[actionButton setTitleColor:[UIColor yellowColor] forState:UIControlStateNormal];
actionButton.titleLabel.textColor = [UIColor whiteColor];
[actionButton setTitleColor:[UIColor whiteColor] forState:UIControlStateDisabled];
[tappedButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
}
}
Thanks advance
for that you have to take one extra UIButton Object in you header file.
While user click on any button you have to store that selected Button in refbutton object and use like below.
#interface YourViewController ()
{
UIButton *btnSelected;
}
Now Move to your Button Action:
-(IBAction)buttonTapped:(UIButton *)sender {
if(_btnSelected){
_btnSelected.titleLabel.textColor = [UIColor blackColor];
}
_btnSelected = sender;
sender.titleLabel.textColor = [UIColor whiteColor];
}
Hope this will help you.

How to reuse properties

I have many different buttons in my app, yet most of them have the same properties assigned to them:
login = [[UIButton alloc]initWithFrame:CGRectMake(8, CGRectGetMaxY(password.frame) + 16, loginView.frame.size.width - 16, 40)];
[login setTitle:#"Login" forState:UIControlStateNormal];
[login.titleLabel setFont:[UIFont fontWithName:#"Avenir Next" size:18]];
[login setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[login setTitleColor:[UIColor colorWithWhite:0.7 alpha:1] forState:UIControlStateHighlighted];
[login setTitleColor:[UIColor colorWithWhite:0.5 alpha:1] forState:UIControlStateDisabled];
Is there any way to create a class or something of a button that already has these default properties assigned? So I could simply go something like:
CustomButtom *btn = [CustomButton alloc]init];
Then the btn will have all of the above properties assigned?
Thanks.
Another way of handling this, is you can create a private method that will return the UIButton with the same properties. I think creating a subclass of UIButton is a little unnecessary.
You can do this by creating a CustomButton class
Xcode -> New File -> Cocoa Touch Class -> Next -> Name Your Button-> Select Subclass of UIButton
CustomButton.h file
#import <UIKit/UIKit.h>
#interface CustomButton : UIButton
#end
CustomButton.m file
#import "CustomButton.h"
#implementation CustomButton
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
//login = [[UIButton alloc]initWithFrame:CGRectMake(8, CGRectGetMaxY(password.frame) + 16, loginView.frame.size.width - 16, 40)];
[self setTitle:#"Login" forState:UIControlStateNormal];
[self.titleLabel setFont:[UIFont fontWithName:#"Avenir Next" size:18]];
[self setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[self setTitleColor:[UIColor colorWithWhite:0.7 alpha:1] forState:UIControlStateHighlighted];
[self setTitleColor:[UIColor colorWithWhite:0.5 alpha:1] forState:UIControlStateDisabled];
}
#end
Now, Call you button
CustomButton *customButton = [[CustomButton alloc]initWithFrame:CGRectMake(8, CGRectGetMaxY(password.frame) + 16, loginView.frame.size.width - 16, 40)];
[customButton addTarget:self action:#selector(loginButtonPressed:) forControlEvents:UIControlEventTouchDown];
[YourView addSubview:customButton];
You've got two options:
Make a function that you can pass in a UIButton to that sets the properties for you
Write a Category for UIButton that does the same as the above. Categories allow you to add functionality to a class without subclassing. See https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/CustomizingExistingClasses/CustomizingExistingClasses.html
Yes. You can subclass the UIButton. When you override the init method setting the properties you could get the button's with the same properties.

Showing different colours for UIButtons on press within IBAction for a Quiz

I have four UIButtons that represent 4 answers. I've managed to get everything hooked up and showing the correct answer in green and the wrong answer in Red.
My only problem is when a user selects a wrong answer I grey out all the answers. I want to show the user the correct answer as well. I've tried else if statements but am missing something, any suggestions?
-(IBAction)Answer1:(id)sender{
if (Answer1Correct == YES) {
[Answer1 setTitle:#"Correct" forState:UIControlStateNormal];
[Answer1 setBackgroundColor:[UIColor greenColor]];
[self RightAnswer];
}
else{
[Answer1 setBackgroundColor:[UIColor redColor]];
[Answer1 setTitle:#"Incorrect" forState:UIControlStateNormal];
[Answer2 setBackgroundColor:[UIColor colorWithRed:102.0/255.0 green:102.0/255.0 blue:102.0/255.0 alpha:1.0f]];
[Answer3 setBackgroundColor:[UIColor colorWithRed:102.0/255.0 green:102.0/255.0 blue:102.0/255.0 alpha:1.0f]];
[Answer4 setBackgroundColor:[UIColor colorWithRed:102.0/255.0 green:102.0/255.0 blue:102.0/255.0 alpha:1.0f]];
[self WrongAnswer];
}
}
The best way is inherit from UIButton. Create class QuizQuestionButton
This Class will have method
-setCorrectAnswer:(NSString *)correctAnswer and -setRealAnswer:(NSString *)realAnswer
If this both is same - set background color as Green, else as Red.
-(void)setCorrectAnswer:(NSString *)correctAnswer {
_correctAnswer = correctAnswer;
}
-(void)setRealAnswer:(NSString *)realAnswer {
if (realAnswer == _correctAnswer) {
self.backgroundColor = [UIColor greenColor];
} else {
self.backgroundColor = [UIColor redColor];
}
}
Also read about design pattern - Strategy. This pattern can help you avoid if-else statement.
Try this:
if (Answer1Correct == YES) {
[Answer1 setTitle:#"Correct" forState:UIControlStateNormal];
[Answer1 setBackgroundColor:[UIColor greenColor]];
[self RightAnswer];
}
else{
[Answer1 setBackgroundColor:[UIColor redColor]];
[Answer2 setBackgroundColor:[UIColor greyColor]];
[Answer3 setBackgroundColor:[UIColor greyColor]];
[Answer4 setBackgroundColor:[UIColor greyColor]];
if(Answer2isCorrect)
{ [Answer2 setBackgroundColor:[UIColor greenColor]]; }
else if(Answer3isCorrect)
{ [Answer3 setBackgroundColor:[UIColor greenColor]]; }
else if(Answer4isCorrect)
{ [Answer4 setBackgroundColor:[UIColor greenColor]]; }
}
if selected answer is wrong, first set all other buttons to grey, then find the correct answer and change that to green.
you may also want to disable the all buttons once an answer is clicked to prevent further clicks.

Unable to change UISearchBar cancel button title color after changing it's text.

I'm using this code to change UISearchBar cancel button title:
-(void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller{
self.searchDisplayController.searchBar.showsCancelButton = YES;
UIView* view=_otsinguRiba.subviews[0];
for (UIView *subView in view.subviews) {
if ([subView isKindOfClass:[UIButton class]]) {
UIButton *cancelButton = (UIButton*)subView;
if (cancelButton) {
[cancelButton setTitle:#"Test") forState:UIControlStateNormal];
[cancelButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
}
}
}
}
While changing the text works fine, changing the color doesn't. It stays black.
I found the answer to this on SO a while back, but I don't recall where. Here's the code that I'm using to set the color of the text.
[[UIBarButtonItem appearanceWhenContainedIn:[UISearchBar class], nil] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor redColor],NSForegroundColorAttributeName,
//[UIColor whiteColor],UITextAttributeTextShadowColor,
//[NSValue valueWithUIOffset:UIOffsetMake(0, 1)],UITextAttributeTextShadowOffset,
nil]
forState:UIControlStateNormal];
You can take advantage of the iOS Runtime Property _cancelButton to achieve this.
UIButton *cancelButton = [self.searchDisplayController.searchBar valueForKey:#"_cancelButton"];
[cancelButton setTitleColor:[UIColor yourColor] forState:UIControlStateNormal];

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