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

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];

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];

Color Variables in Objective C

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;

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];

Why can't I create a border

Here is my code which should work but does not.
self.square =[[UIView alloc]init];
self.square.center = CGPointMake(location.x, location.y);
//_square.backgroundColor = [UIColor colorWithRed:12.0/255.0 green:185.0/255.0 blue:249.0/255.0 alpha:1];
[_square.layer setBorderColor: (__bridge CGColorRef)[UIColor colorWithRed:12.0/255.0 green:185.0/255.0 blue:249.0/255.0 alpha:1]];
_square.alpha=1.0;
[_square.layer setBorderWidth:2.0];
[previewView addSubview:_square];
The thing is, if I give the view a background color then the view is visible but if I dont, the view is not. What I am trying to do is get a square outline. So I figured if I get a view with no background color and a border, it would work.
don't typecast the UIColor to CGColor
try below code
[_square.layer setBorderColor: [UIColor colorWithRed:12.0/255.0 green:185.0/255.0 blue:249.0/255.0 alpha:1].CGColor];
By default UIView is transparent. You can achieve what you want, by setting different color for your view's background and border. Then adjust it as you wish.
You can do like this:
_square.backgroundColor = [UIColor whiteColor];
[_square.layer setBorderColor:[UIColor redColor].CGColor];

How To use Fixed set of colors for the Graph in MIMChart Library? suggestions are appreciable

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;
}

Resources