How to set alpha of uiclearcolor? - objective-c

I am currently doing this:
UIColor *myColor = [UIColor clearColor];
This is great but i would like to specify a certain alpha of "myColor". How would i do so?

If you have an existing color, you can return a new one with a specified alpha, like this:
- (void)setBackgroundColor:(UIColor *)color
{
self.backgroundColor = [color colorWithAlphaComponent:0.3f];
}

[UIColor clearColor] is, how should I put it?, clear!
It is a convenience class method returning a UIColor with alpha at zero.
If you want a color with a fractional amount of transparency:
+ (UIColor *)colorWithRed:(CGFloat)red green:(CGFloat)green blue:(CGFloat)blue alpha:(CGFloat)alpha
Or one of the other UIColor class methods.

You can use colorWithWhite:alpha like so:
[UIColor colorWithWhite:1.0 alpha:0.5];
Or if you want a specific color:
[UIColor colorWithRed:1.0 green:1.0 blue:0.3 alpha:0.72];
Check out the docs.

For Swift you can use:
UIColor.black.withAlphaComponent(0.5)
Or RGB Colors:
UIColor(red: 0/255.0 , green: 0/255.0 , blue: 0/255.0 , alpha: 0.5)

To answer that Question
UIColor *myColor = [[UIColor clearColor] colorWithAlphaComponent:0.3f];
I assume that you have "clearColor" as a valid UIColor;
#refer James O'Brien Solution;

If you prefer a simple and yet powerful solution in Swift then checkout HandyUIKit. Install it in your project using Carthage – then your life becomes easier:
import HandyUIKit
// creates a new UIColor object with the given value set
myColor.change(.alpha, to: 0.2)
There is also an option to apply a relative change:
// create a new UIColor object with alpha increased by 0.2
myColor.change(.alpha, by: 0.2)
I hope it helps!

Related

Issue with using RGB colors in IOS

I want to use RGB color to set set background color of UIButton.
I have lots of UIButtons so I decided to make a custom class.But when I try to use colorWithRed I am getting error.
First Code:
[self.layer setBackgroundColor:[UIColor colorWithRed:60/255.0f green:146/255.0f blue:180/255.0f alpha:1.0f]];
Error
Second Code
self.layer.backgroundColor = [UIColor colorWithRed:60/255.0f green:146/255.0f blue:180/255.0f alpha:1.0f];
Error
try to use this code
self.layer.backgroundColor = [UIColor colorWithRed:60/255.0f green:146/255.0f blue:180/255.0f alpha:1.0f].CGColor;
You need to convert UIColor to CGColor which can be done by above code.
Hope this will solve your problem
layer backgroundcolor is of cgcolorref type, so cant use uicolor. we need to use cgcolor or convert uicolor to cgcolor and use it.
example:
[[UIColor whiteColor] CGColor];
or
[UIColor whiteColor].CGColor;

Objective-C drawRect Custom RGB Fill Color

I'm trying to fill a circle in Objective-C with a custom RGB color. Here is my drawRect method:
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
UIColor *color = [UIColor colorWithRed:10.0 green:131.0 blue:254.0 alpha:1.0];
CGContextSetStrokeColorWithColor(context, [color CGColor]);
CGContextSetLineWidth(context, 1.0);
CGContextAddEllipseInRect(context, [ball getRect]);
CGContextStrokePath(context);
}
Whenever I specify anything in place of [color CGColor] like greenColor it works fine, but with the above code nothing gets rendered (an invisible object). I know it is moving around the page because my update method is NSLog'ing a string for every update.
So, I want to fill and get the outer line of the circle to be the specified RGB color above. What am I doing wrong?
Thanks
[UIColor colorWithRed:green:blue:alpha] accept values between 0.0 to 1.0 (Apple Docs Reference) try changing the code like this:
UIColor *color = [UIColor colorWithRed:10.0/255.0 green:131.0/255.0 blue:254.0/255.0 alpha:1.0];
The value for each parameter in this method call:
[UIColor colorWithRed:10.0 green:131.0 blue:254.0 alpha:1.0];
should be between 0.0 and 1.0.

TableView backgroundColor not changing

I have a grouped tableView, and I am trying to change the default background to custom color. I have looked all over, and the closest thing to working is this:
- (void)viewDidLoad {
UIColor *backgroundColor = [UIColor colorWithRed:181 green:293 blue:223 alpha:0];
self.tableView.backgroundView = [[UIView alloc]initWithFrame:self.tableView.bounds];
self.tableView.backgroundView.backgroundColor = backgroundColor;
}
This code changes the background to white, but I can't get it to change to the custom color. Can someone help me out?
You are creating the color incorrectly. The RGBA values need to be in the range 0.0 - 1.0. UIColor will treat anything over 1.0 as 1.0. So your color is being set to white since all three RGB values will be treated as 1.0. Also note that an alpha of 0 means totally transparent. You want 1.0 to mean totally visible.
- (void)viewDidLoad {
UIColor *backgroundColor = [UIColor colorWithRed:181/255.0 green:293/255.0 blue:223/255.0 alpha:1.0];
self.tableView.backgroundView = [[UIView alloc]initWithFrame:self.tableView.bounds];
self.tableView.backgroundView.backgroundColor = backgroundColor;
}
Please note that your green value is 293. That needs to be changed to something from 0 to 255.
Your RGBA values should be 0.0 - 1.0.
Make sure alpha value should not be 0.0, to see the colour effect
UIColor *backgroundColor = [UIColor colorWithRed:0.7 green:1.0 blue:0.85 alpha:1.0];
self.tableView.backgroundView = [[UIView alloc]initWithFrame:self.tableView.bounds];
self.tableView.backgroundView.backgroundColor = backgroundColor;

-[UICIColor colorSpaceName]: unrecognized selector sent to instance

I am creating several classes for theme-ing support in my iOS 5 app. My themes are stored in plist and I load them up in a Theme object, which I use in my app to initialize various controls. I store the colors as strings in my theme and then I use this code to convert them to UIColor:
UIColor* color = [UIColor colorWithCIColor:[CIColor colorWithString:#"0.5 0.5 0.5 1.0"]];
This works fine for most controls, however when I try to set the tint color of the navigation bar as such:
//navigation bar
[self.navigationController.navigationBar setTintColor:color];
I get this exception:
-[UICIColor colorSpaceName]: unrecognized selector sent to instance
When I initialize the color without using CIColor e.g. like this:
UIColor* color = [UIColor colorWithRed:0.5 green:0.5 blue:0.5 alpha:1.0];
[self.navigationController.navigationBar setTintColor:color];
All works great.
Any clues what is causing this? I could not find much info about UICIColor, but I am guessing since UIColor is only a wrapper on top of CGColor or CIColor there are implementation differences.
See Strange crash when I try to access to uibutton's titleLabel property (xcode 4.5 and IOS sdk 6.0)
I found a workaround : before using my colorWithCIColor, I made a copy of it with :
newcolor = [UIColor colorWithCGColor:newcolor.CGColor];
and it solves the crash. Strange, anyway
I had similar issues with
[UIColor colorWithCIColor:[CIColor colorWithString:color]];
Although I looked for an elegant fix for this, in the end I settled with a solution that stopped the problem and enabled my app to continue exactly as before.
My color string was in the same format as yours:
"0.5 0.7 0.2 0.75"
I found the easiest way to fix it was just to do the following:
NSArray * colorParts = [color componentsSeparatedByString: #" "];
CGFloat red = [[colorParts objectAtIndex:0] floatValue];
CGFloat green = [[colorParts objectAtIndex:1] floatValue];
CGFloat blue = [[colorParts objectAtIndex:2] floatValue];
CGFloat alpha = [[colorParts objectAtIndex:3] floatValue];
UIColor * newColor = [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
Manually splitting out each value and then putting then into the colorWithRed: code.
It means you can keep your color string but get rid of the problematic colorWithString code which is causing all the crashes.
Hope this helps
UIColor define in iOS 2.0 ,because of [UIColor colorWithCIColor] convert to iOS5.0, I think that apple convert error, you can using below code:
CIColor *ci_ = [CIColor colorWithString:colorString];
UIColor *color = [UIColor colorWithRed:ci_.red green:ci_.green blue:ci_.blue alpha:ci_.alpha];
// UIColor *color = [UIColor colorWithCIColor:[CIColor colorWithString:colorString]];

How do I define constant values of UIColor?

I want to do something like this, but I cannot get a cooperative syntax.
static const UIColor *colorNavbar = [UIColor colorWithRed: 197.0/255.0 green: 169.0/255.0 blue: 140.0/255.0 alpha: 1.0];
I suppose that I could define macros, but they are ugly.
I like to use categories to extend classes with new methods for this sort of thing. Here's an excerpt of code I just wrote today:
#implementation UIColor (Extensions)
+ (UIColor *)colorWithHueDegrees:(CGFloat)hue saturation:(CGFloat)saturation brightness:(CGFloat)brightness {
return [UIColor colorWithHue:(hue/360) saturation:saturation brightness:brightness alpha:1.0];
}
+ (UIColor *)aquaColor {
return [UIColor colorWithHueDegrees:210 saturation:1.0 brightness:1.0];
}
+ (UIColor *)paleYellowColor {
return [UIColor colorWithHueDegrees:60 saturation:0.2 brightness:1.0];
}
#end
Now in code I can do things like:
self.view.backgroundColor = highlight? [UIColor paleYellowColor] : [UIColor whitecolor];
and my own defined colors fit right in alongside the system-defined ones.
(Incidentally, I am starting to think more in terms of HSB than RGB as I pay more attention to colors.)
UPDATE regarding precomputing the value: My hunch is that it's not worth it. But if you really wanted, you could memoize the values with static variables:
+ (UIColor *)paleYellowColor {
static UIColor *color = nil;
if (!color) color = [UIColor colorWithHueDegrees:60 saturation:0.2 brightness:1.0];
return color;
}
You could make a macro do do the memoizing, too.
I usually make a category of UIColor for each project:
#interface UIColor (ProjectName)
+(UIColor *) colorForSomeTable;
+(UIColor *) colorForSomeControl;
+(UIColor *) colorForSomeText;
#end
With the constants in the implementation:
#implementation UIColor (ProjectName)
+(UIColor *) colorForSomeTable { return [UIColor colorWithRed:...]; }
#end
I also do the same for UIFont and UIImage as needed.
To expand on jasoncrawford's answer (I'd put this in as a comment, but you can't format code in the comments) if you want to precompute the values (or do it only once).
+ (UIColor *)paleYellowColor
{
static UIColor* paleYellow = nil;
if (paleYellow == nil)
{
paleYellow = [UIColor colorWithHueDegrees:60 saturation:0.2 brightness:1.0];
}
return paleYellow;
}
The reason your original idea doesn't work is because the compiler can only use initialisers outside of functions, not normal code. You could have achieved something like what you wanted with the initialize methosd e.g.
static UIColor* colorNavBar = nil;
+(void) initialize
{
if (colorNavBar != nil)
{
colorNavBar = ....
}
}
NB the const qualifier on your original definition is redundant since UIColor is immutable anyway.
You can 'define' a similar CONSTANT like this:
#define FAV_COLOR [UIColor colorWithRed:24/255.0f green:89/255.0f blue:36/255.0f alpha:0.9]
and call it by name like you are used to with constants: FAV_COLOR
Hope that helps.
You can do this:
#define backgroundColorApp [UIColor colorWithRed: 197.0/255.0 green: 169.0/255.0 blue: 140.0/255.0 alpha: 1.0]
In Swift, define an extension:
extension UIColor {
class func XXXWhiteColor() -> UIColor {
return UIColor(red: 256, green: 256, blue: 256, alpha: 1.0)
}
class func XXXGreenColor() -> UIColor {
return UIColor(red: 73/255.0, green: 212/255.0, blue: 86/255.0, alpha: 1.0)
}
}
Use like:
label.background = UIColor.XXXWhiteColor()
#define textColorApp [UIColor colorWithRed: 197.0/255.0 green: 169.0/255.0 blue: 140.0/255.0 alpha: 1.0]
myLabel.textColor=textColorApp;
Just define below macro in your constant file and pass only RGB value and use anywhere you want
#define RGBCOLOR(r,g,b)[UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1]
To use :
[lblCount setTextColor:RGBCOLOR(236, 43, 92)];
I feel it's also worth mentioning another awesome feature that is rarely talked about: Color Literals. Not only are they easier to read, but they are WAY easier to edit. In Swift,
let color: UIColor = #colorLiteral(red: 0.9607843137, green: 0.4784313725, blue: 0.3215686275, alpha:
When pasted into Xcode, this syntax creates a simple color box.
Click here to see an example.
Once you see the box, you can then double click on it to edit it easily. Similarly, you can switch between various IB options, including RGB Sliders, if you have a specific list of color hex values from your designer.