Color Variables in Objective C - ios

How do I set and reuse a color variable in Obj C? I am trying to set a reusable color value as in this question:
Change background color with a variable iOS
but am unsuccessful.
UIColor *lightGrayHeader = [UIColor colorWithRed:246/255.f green:239/255.f blue:239/255.f alpha:1.0];
self.view.backgroundColor = [UIColor lightGrayHeader];
Returns an error: "Initializer element is not a compile-time constant."
Thanks for your ideas!

What you have defined is a local variable. It is used like this:
UIColor *lightGrayHeader = [UIColor colorWithRed:246/255.f green:239/255.f blue:239/255.f alpha:1.0];
self.view.backgroundColor = lightGrayHeader;
If you want to use a static method on UIColor to fetch a colour, you could do this:
#interface UIColor (MyColours)
+ (instancetype)lightGrayHeader;
#end
#implementation UIColor (MyColours)
+ (instancetype)lightGrayHeader {
return [self colorWithRed:246/255.f green:239/255.f blue:239/255.f alpha:1.0];
}
#end
And then as long as you import the UIColor (MyColours) header, you could use:
self.view.backgroundColor = [UIColor lightGrayHeader];

Since you've already created the lightGrayHeader color, just use it:
UIColor *lightGrayHeader = [UIColor colorWithRed:246/255.f green:239/255.f blue:239/255.f alpha:1.0];
self.view.backgroundColor = lightGrayHeader;
self.otherView.backgroundColor = lightGrayHeader;
...

UIColor *lightGrayHeader = [UIColor colorWithRed:246/255.f green:239/255.f blue:239/255.f alpha:1.0];
self.view.backgroundColor = [UIColor lightGrayHeader]; // error
It's a variable, not a method of UIColor:
self.view.backgroundColor = lightGrayHeader;

It works like this self.view.backgroundColor = lightGrayHeader;

Related

customly set button background color ios

I am trying to change the background color of UIButton to a custom color. All the default greycolor, bluecolor, etc do not suffice.
UIColor *myColor=[UIColor colorWithRed:1 green:1 blue:0 alpha:1.0f];
_button.backgroundColor = [UIColor myColor];
It gives error on 2nd line saying
No known class method for selector 'myColor'
your code is fine, the simple mistake is again you creted the [UIColor property] on second line
UIColor *myColor=[UIColor colorWithRed:1 green:1 blue:0 alpha:1.0f];
Not like
_button.backgroundColor = [UIColor myColor];
Do like
_button.backgroundColor = myColor;
Update
you can directly use the color property like
_button.backgroundColor = [UIColor colorWithRed:1 green:1 blue:0 alpha:1.0f];
just change your current code with below code
UIColor *myColor=[UIColor colorWithRed:1 green:1 blue:0 alpha:1.0f];
_button.backgroundColor = myColor; // because myColor is UIColor
Try this:
UIColor *myColor=[UIColor colorWithRed:1 green:1 blue:0 alpha:1.0f];
_button.backgroundColor = myColor;
myColor is not defined in UIColor. It's a variable that you defined.
It's much more convenient to use:
_button.backgroundColor = [UIColor colorWithRed:1.0/255.0 green:1.0/255.0 blue:0.0/255.0 alpha:1.0f];

Change label background color...first red, then yellow on second click

I cannot find anything relating to it. I'm a beginner but I'm assuming that I need a NSArray to change the colors.
Anyway, I have a button and when you click the button once it changes a label background to GREEN. How do I make it so the first click it changes the label background to green...and then the second click it changes the background to YELLOW
- (IBAction)studentOne:(id)sender {
_studentOne.backgroundColor = [UIColor greenColor];
_studentOne.backgroundColor = [UIColor yellowColor];
_studentOne.backgroundColor = [UIColor redColor];
_studentOne.backgroundColor = [UIColor blackColor];
_studentOne.backgroundColor = [UIColor whiteColor];
}
That would be my sequence.
Would it be something involving
NSArray *colorArray;
colorArray = [NSArray array with objects:
I tried this because I thought it may be like changing text color on a button click
- (IBAction)studentOne:(id)sender {
NSArray *colorArray;
colorArray = [NSArray arrayWithObjects:
#"_studentOne.backgroundColor = [UIColor greenColor]",
#"_studentOne.backgroundColor = [UIColor yellowColor]",
#"_studentOne.backgroundColor = [UIColor redColor]",
#"_studentOne.backgroundColor = [UIColor blackColor]",
#"_studentOne.backgroundColor = [UIColor whiteColor]",
nil];
}
You're right about the array, a little off on the syntax. Something like this will work:
- (IBAction)studentOne:(id)sender {
static NSArray *colors;
if (!colors) colors = #[[UIColor redColor], [UIColor greenColor], [UIColor yellowColor]];
UIColor *currentColor = myLabel.backgroundColor;
NSInteger index = [colors indexOfObject:currentColor];
index = (index == NSNotFound || index == colors.count-1)? 0 : index+1;
self.myLabel.backgroundColor = colors[index];
}

BackgroundColor of UItableviewCell Notworking

I am setting the Background color of my uitableviewCell like
cell.contentView.backgroundColor = [UIColor colorWithRed:8.0 green:210.0 blue:11.0 alpha:1.0];
its not working, however, if i do like
cell.contentView.backgroundColor = [UIColor grayColor];
it works then. Any help
UIColor has to be defined between 0 and 1 to get the RGB value to work (UIColor class reference):
cell.contentView.backgroundColor = [UIColor colorWithRed:8.0f/255.0f green:210.0f/255.0f blue:11.0f/255.0f alpha:1.0f];

Random color for custom view that cannot be reused [duplicate]

This question already has answers here:
Getting a random object from NSArray without duplication
(3 answers)
Closed 8 years ago.
I have a custom view, which is a circle view. The view has a custom UIColor randomColor background from another method.
Issue - What I am trying to do is only have the color used once. In this case I have three circle views and 6 colors. So I am looking for one color (randomly selected) for each of the three circle views. What is the best way to accomplish?
-(void)setupView {
self.translatesAutoresizingMaskIntoConstraints = NO;
float newRadius = self.frame.size.width/2;
self.layer.cornerRadius = newRadius;
self.layer.masksToBounds= YES;
self.layer.borderWidth = 5;
self.layer.borderColor = [UIColor colorWithRed:0.138 green:0.225 blue:1.000 alpha:1.000].CGColor;
self.backgroundColor = [self randomColor];
[self setupLabel];
}
-(UIColor *)randomColor {
static NSArray *__colors = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
__colors = #[
[UIColor colorWithRed:.11372549 green:.819607843 blue:.819607843 alpha:1.0], //29,209,99
[UIColor colorWithRed:.882352941 green:.466666667 blue:.709803922 alpha:1.0], //225,119,181
[UIColor colorWithRed:.647058824 green:.164705882 blue:.482352941 alpha:1.0], //165,42,123
[UIColor colorWithRed:.482352941 green:.17254902 blue:.733333333 alpha:1.0], //123,44,187
[UIColor colorWithRed:.219607843 green:.098039216 blue:.698039216 alpha:1.0], //56,25,178
[UIColor colorWithRed:.678431373 green:.843137255 blue:.274509804 alpha:1.0] //173,215,70
];
});
int index = arc4random_uniform((uint32_t)__colors.count);
return __colors[index];
}
Okey, If you want to avoid duplication you need remove color from array if it selected.
Try this:
-(UIColor *)randomColor {
static NSMutableArray *__colors = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
__colors = [NSMutableArray arrayWithArray:#[[UIColor colorWithRed:.11372549 green:.819607843 blue:.819607843 alpha:1.0], //29,209,99
[UIColor colorWithRed:.882352941 green:.466666667 blue:.709803922 alpha:1.0], //225,119,181
[UIColor colorWithRed:.647058824 green:.164705882 blue:.482352941 alpha:1.0], //165,42,123
[UIColor colorWithRed:.482352941 green:.17254902 blue:.733333333 alpha:1.0], //123,44,187
[UIColor colorWithRed:.219607843 green:.098039216 blue:.698039216 alpha:1.0], //56,25,178
[UIColor colorWithRed:.678431373 green:.843137255 blue:.274509804 alpha:1.0] //173,215,70
]];
});
int index = arc4random_uniform((uint32_t)__colors.count);
UIColor *color = __colors[index];
[__colors removeObjectAtIndex:index];
return color;
}
try to hold colours in NSMutableArray, and if you use this colour then delete it from array:
[mutableArray removeObject:color];

iOS: How to define some default objects to be used anywhere?

There are many places in my app that needs to set the UIColor. I want to define the color somewhere that I can reuse it without writing the same line again, it's hard to keep track and maintain.
UIColor *myColor = [UIColor colorWithRed:0.1 green:0.3 blue:0.7 alpha:1];
I tried to make it like
#define myColor = [UIColor colorWithRed:0.1 green:0.3 blue:0.7 alpha:1];
and reuse myColor but it doesn't work. :/
Thanks!
For your define, you could write:
#define myColor [UIColor colorWithRed:0.1 green:0.3 blue:0.7 alpha:1]
and where you use myColor, it'll be replaced outright with [UIColor colorWithRed:0.1 green:0.3 blue:0.7 alpha:1].
Alternatively, you could write a category on UIColor that provides a method that returns your colour.
Example:
#interface UIColor (MyColors)
+ (UIColor *)myAwesomeColor;
#end
#implementation UIColor (MyColors)
+ (UIColor *)myAwesomeColor
{
return [UIColor colorWithRed:0.1 green:0.3 blue:0.7 alpha:1];
}
#end
You would then use this like [UIColor myAwesomeColor] wherever you need it, just like you do with [UIColor blackColor].
answer given by mr Jasarien correct you can also follow above code and example
while declaring any thing in macro directives we don't assign any value so we cant use equal operators in directives "="sign
you can use code like this
#define mycolor [UIColor colorWithRed:0.1 green:0.3 blue:0.7 alpha:1];

Resources