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];
Related
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;
I set colors of the UITextField:
textField.backgroundColor = [UIColor whiteColor];
textField.textColor = [UIColor blackColor];
textField.alpha = 0.5f;
Alpha channel is set for bouth bkg and text colors.
How to set alpha separately for text color?
textField.textColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
try set textColor alpha.
I am having trouble with the MIMChart library to set the fixed set of colours. I am using Ritu Raj MIMChart library
MIMBarGraph *myBarChart;
myBarChart=[[MIMBarGraph alloc]initWithFrame:CGRectMake(50, 20, myTableView.frame.size.width- 50, myTableView.frame.size.width * 0.5)];
myBarChart.delegate=self;
myBarChart.tag=10+indexPath.row;
myBarChart.isGradient=YES;
myBarChart.groupedBars=YES;
myBarChart.glossStyle=GLOSS_STYLE_1;
myBarChart.xTitleStyle=X_TITLES_STYLE2;
[myBarChart drawBarChart];
[cell.contentView addSubview:myBarChart];
Try this Delegate
-(NSArray *)colorsForBarChart:(id)graph {
NSArray *colorsArray;
MIMColorClass *color1=[MIMColorClass colorWithRed:0.0f/255.0f Green:255.0f/255.0f Blue:127.0f/255.0f Alpha:1];
MIMColorClass *color2=[MIMColorClass colorWithRed:255.0f/255.0f Green:4.0f/255.0f Blue:0.0f/255.0f Alpha:1];
MIMColorClass *color3=[MIMColorClass colorWithRed:255.0f/255.0f Green:182.0f/255.0f Blue:193.0f/255.0f Alpha:1];
MIMColorClass *color4=[MIMColorClass colorWithRed:1.0f/255.0f Green:255.0f/255.0f Blue:255.0f/255.0f Alpha:1];
colorsArray=[NSArray arrayWithObjects:color1,color2,color3,color4, nil];
return colorsArray;
}
I have made a dictionary of colors using the [UIColor colorWithRed: green: blue: alpha: ] function. However, when called and shown on screen, they look nothing like the actual colors, but rather resembling the basic [UIColor yellowColor] functions. Is there a library that I should be including in order to show the actual shades of RGB colors?
For reference, here is the dictionary I made:
colorStorage = [NSDictionary dictionaryWithObjectsAndKeys:
[UIColor colorWithRed:229.0 green:43.0 blue:80.0 alpha:1.0], #"Amaranth",
[UIColor colorWithRed:225 green:191 blue:0 alpha:1.0], #"Amber",
[UIColor colorWithRed:239 green:222 blue:205 alpha:1.0], #"Almond",
[UIColor colorWithRed:205 green:149 blue:117 alpha:1.0], #"Antique brass",
[UIColor colorWithRed:0 green:128 blue:0 alpha:1.0], #"Ao",
[UIColor colorWithRed:141 green:182 blue:0 alpha:1.0], #"Apple green",
[UIColor colorWithRed:251 green:206 blue:177 alpha:1.0], #"Apricot",
[UIColor colorWithRed:135.0 green:169.0 blue:107.0 alpha:1.0], #"Asparagus",
[UIColor colorWithRed:0 green:127 blue:255 alpha:1.0], #"Azure",
[UIColor colorWithRed:225 green:32 blue:82 alpha:1.0], #"Awesome",
[UIColor colorWithRed:178 green:190 blue:181 alpha:1.0], #"Ash grey",
[UIColor colorWithRed:110 green:127 blue:128 alpha:1.0], #"AutoMetalSaurus", nil];
And they are called as such:
for (id key in colorStorage){
UIButton *colorButton = [UIButton buttonWithType:UIButtonTypeCustom];
[colorButton setTitle:key forState:UIControlStateNormal];
//additional code here...
colorButton.backgroundColor = [colorStorage objectForKey:key];
colorButton.tag = i;
[self.colorPicker addSubview:colorButton];
i = i + 1;
}
As stated, always divide RGB values by 255.0. I did this mistake too when first learning.
colorStorage = [NSDictionary dictionaryWithObjectsAndKeys:
[UIColor colorWithRed:229.0/255.0 green:43.0/255.0 blue:80.0/255.0 alpha:1.0], #"Amaranth",
[UIColor colorWithRed:225.0/255.0 green:191.0/255.0 blue:0.0/255.0 alpha:1.0], #"Amber",
[UIColor colorWithRed:239.0/255.0 green:222.0/255.0 blue:205.0/255.0 alpha:1.0], #"Almond",
[UIColor colorWithRed:205.0/255.0 green:149.0/255.0 blue:117.0/255.0 alpha:1.0], #"Antique brass",
[UIColor colorWithRed:0.0/255.0 green:128.0/255.0 blue:0.0/255.0 alpha:1.0], #"Ao",
[UIColor colorWithRed:141.0/255.0 green:182.0/255.0 blue:0.0/255.0 alpha:1.0], #"Apple green",
[UIColor colorWithRed:251.0/255.0 green:206.0/255.0 blue:177.0/255.0 alpha:1.0], #"Apricot",
[UIColor colorWithRed:135.0/255.0 green:169.0/255.0 blue:107.0/255.0 alpha:1.0], #"Asparagus",
[UIColor colorWithRed:0.0/255.0 green:127.0/255.0 blue:255.0/255.0 alpha:1.0], #"Azure",
[UIColor colorWithRed:225.0/255.0 green:32.0/255.0 blue:82.0/255.0 alpha:1.0], #"Awesome",
[UIColor colorWithRed:178.0/255.0 green:190.0/255.0 blue:181.0/255.0 alpha:1.0], #"Ash grey",
[UIColor colorWithRed:110.0/255.0 green:127.0/255.0 blue:128.0/255.0 alpha:1.0], #"AutoMetalSaurus", nil];
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];