Issue with using RGB colors in IOS - objective-c

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;

Related

Objective-C: What is the actual color of a textfields placeholder text?

I've tried messing with the individual values of red, green, blue. But can't seem to get the color right.
e.g
[UIColor colorWithRed:0.80 green:0.80 blue:0.80 alpha:1.0];
Thanks
EDIT
Thanks to Satish A for pushing me in the right direction.
The UIColor of UITextField.placeholder that I got was
[UIColor colorWithRed:196.0/255.0 green:193.0/255.0 blue:110.0/255.0 alpha:1.0]
try following:
override drawPlaceholderInRect:(CGRect)rect as such to manually render the placeholder text:
- (void) drawPlaceholderInRect:(CGRect)rect {
[[UIColor blueColor] setFill];
[[self placeholder] drawInRect:rect withFont:[UIFont systemFontOfSize:16]];
}
OR
[self.placeholder drawInRect:rect withFont:self.font lineBreakMode:UILineBreakModeTailTruncation alignment:self.textAlignment];

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.

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

NSImage +imageWithPatternColor: and NSGradient - Cocoa/Mac

I am drawing on a custom view an NSGradient like this:
- (void)drawRect: (NSRect)dirtyRect
{
NSGradient* g = [[NSGradient alloc] initWithStartingColor: _color endingColor: [NSColor clearColor]];
[g drawInRect: [self bounds] angle: 90];
}
If _color is a normal color, for example [NSColor blueColor], everything is fine. The problem is that _color comes from a pattern image (which in this case is mostly grey with some fancy pixels on it), and when that happens the program keeps logging this error:
*** -[NSGradient initWithColors:atLocations:colorSpace:]: the color NSPatternColorSpace CGImageSource=0x400367d60"
)> cannot be converted into color space Generic RGB colorspace
_color = [NSColor colorWithPatternImage: [NSImage imageNamed: #"mainBG.png"]]
The image is completely opaque and is a png file.
Any ideas? perhaps I should change the file type? I don't know...
EDIT:
If I define _color like this:
_color = [[NSColor colorWithPatternImage: [NSImage imageNamed: #"mainBG.tiff"]] colorUsingColorSpace: [NSColorSpace genericRGBColorSpace]]
then no gradient is displayed. Nothing. Just as if I didn't have the drawRect: method. Any ideas?
An NSGradient doesn't work with a pattern as one of the colors. That's what the exception is telling you. It will only work with an NSColor that can be converted to an RGB color. If you want to make your pattern fade to clear, you'll have to do it another way.

How to set alpha of uiclearcolor?

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!