I'm stuck with a problem here in UIColor.
I'll be getting color names from webservices like
Red, Blue, Magenta ....... and so.
how can i set the backgroundColor of a view using this.
i can only find class methods with these color names ([UIColor redColor], [UIColor blueColor], [UIColor magentaColor]) but i'm not able to write a code which programatically calls these methods cause the color names i get are dynamic.
PLease Help.....
Thank you.
Something like this should work:
NSString *colorFromWeb = #"Red"; // for example
NSString *selectorName = [NSString stringWithFormat:#"%#Color", [colorFromWeb lowercaseString]];
SEL selector = NSSelectorFromString(selectorName);
if ([UIColor respondsToSelector:selector])
UIColor *color = [UIColor performSelector:selector]; // Equivalent to [UIColor redColor];
Here you can Follow Two Approach.
1).Here you can make the call to the color Method by passing the Name of these coming Colors.
As Scott explain in his Answer.
NSString *colorFromWeb = #"Red"; // for example
NSString *selectorName = [NSString stringWithFormat:#"%#Color", [colorFromWeb lowercaseString]];
SEL selector = NSSelectorFromString(selectorName);
if ([UIColor respondsToSelector:selector])
UIColor *color = [UIColor performSelector:selector]; // Equivalent to [UIColor redColor];
2).In Above way you may face some trouble suppose you got some color name form Your WebService which is not exist in UIColor Class then in that case you can not get that desirable color or might be your app could crashed etc.
Here I would Suggest you you should ask the RGB float Values From your Webservice Provider.In this way You can easily pass These RGB and can make Color with these RGB
Here is Some Demo.
[UIColor colorWithRed:redRGB green:greenRGB blue:blueRGB alpha:1.0];
You just need to obtain redRGB,greenRGBand blueRGB from WebService.
In this You can' face any Crash.
I hope It may clears To you.
The Key Value Coding mechanism allows you to interact with class's properties using string representations of the property names.
NSString* colorString = #"redColor";
UIColor* color = [UIColor valueForKey:colorString];
Related
I want to change the background color of my UIimageview. I am aware of how to do it like this
ColorBox.backgroundColor = [UIColor blueColor];
However, my program requires that the backgroundColor be set to a Hex color value. How do i set to a Hex color instead of a UIimage text color?
I have found a link which can answer you.
Please visit http://arstechnica.com/apple/guides/2009/02/iphone-development-accessing-uicolor-components.ars
Assuming you have the individual hex value for red, green and blue. This should work.
int red = [[NSString stringWithFormat:#"%d", redHex] intValue];
int green = [[NSString stringWithFormat:#"%d", greenHex] intValue];
int blue = [[NSString stringWithFormat:#"%d", blueHex] intValue];
[UIColor colorWithRed:red/255.0 green:green/255.0 blue:blue/255.0 alpha:1]
Or use this to parse the string:
How can I convert RGB hex string into UIColor in objective-c?
I'm trying to assign blue text like this, exactly like this
I'm using my own text field.
In hex the color is #336699
I need to access my text color to this, I would have liked to use a UIColor but there doesn't seem to be one.
UIColor needs it's values in RGB/255.0f. You can find here a converter. In your case, your color is R:51, G:102, B:153.
So the code to get your UIColor is then:
UIColor *myColor = [UIColor colorWithRed:51.0f/255.0f green:102.0f/255.0f blue:153.0f/255.0f alpha:1.0f];
I wrote a category for UIColor to convert hex-style colors to UIColors
+ (UIColor *)colorWithHex:(UInt32)col {
unsigned char r, g, b;
b = col & 0xFF;
g = (col >> 8) & 0xFF;
r = (col >> 16) & 0xFF;
return [UIColor colorWithRed:(double)r/255.0f green:(double)g/255.0f blue:(double)b/255.0f alpha:1];
}
UIColor *newColor = [UIColor colorWithHex:0x336699];
I found a blog about this, and in there someone had made a comment where they'd written some code to print out the exact values used to the log. This is the exact specification for the Slate Blue color that Apple uses:
[UIColor colorWithRed:0.22f green:0.33f blue:0.53f alpha:1.0f]
Here's a category:
#interface UIColor (mxcl)
+ (UIColor *)slateBlueColor;
#end
#implementation UIColor (mxcl)
+ (UIColor *)slateBlueColor { return [UIColor colorWithRed:0.22f green:0.33f blue:0.53f alpha:1.0f]; }
#end
better use the bicolor converter
http://www.touch-code-magazine.com/web-color-to-uicolor-convertor/
I'm trying to create a simple custom UIView wich contain a string drawn with a single font, but where the first character is slightly larger.
I thought this would be easily implemented with two UILabel:s placed next to eachother.
I use NSString sizeWithFont to measure my string to be able to lay it out correctly.
But I noticed that the font baseline in the returned rectangle varies with +/- 1 pixel depending on the font size I set.
Here is my code:
NSString* ctxt = [text substringToIndex:1];
NSString* ttxt = [text substringFromIndex:1];
CGSize sz = [ctxt sizeWithFont: cfont ];
clbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, sz.width, sz.height)];
clbl.text = ctxt;
clbl.font = cfont;
clbl.backgroundColor = [UIColor clearColor];
[contentView addSubview:clbl];
CGSize sz2 = [ttxt sizeWithFont: tfont];
tlbl = [[UILabel alloc] initWithFrame:CGRectMake(sz.width, (sz.height - sz2.height), sz2.width, sz2.height)];
tlbl.text = ttxt;
tlbl.font = tfont;
tlbl.backgroundColor = [UIColor clearColor];
[contentView addSubview:tlbl];
If I use 12.0 and 14.0 as sizes, it works fine.
But if I instead use 13.0 and 15.0, then the first character is 1 pixel too high.
Is this a known problem?
Any suggestions how to work around it?
Creating a UIWebView with a CSS and HTML page seems way overkill for this. and more work to handle dynamic strings. Is that what I'm expected to do?
Found the answer...
Ofcourse, I also have to check the descender value on the font, and compensate for that in the layout.
New rect for the second label is:
CGRectMake(sz.width, (sz.height - sz2.height) + floor(cfont.descender - tfont.descender), sz2.width, sz2.height)
floor() is to make sure it snaps to pixel position, or the font will look blurry
I'm trying to compare the text colour in a UIlabel with a UIColor but the result is always false.
The following code produces the result:
color equal 1, 0
I expect both a and b to be equal to 1. Is there another way to do this compare?
bool a,b;
UIColor *myColor1, *myColor2;
myColor1 = [UIColor redColor];
mainViewController.timerLabel.textColor = [UIColor redColor];
myColor2 = [UIColor colorWithCGColor:mainViewController.timerLabel.textColor.CGColor];
a = [[UIColor redColor] isEqual:myColor1];
b = [[UIColor redColor] isEqual:myColor2];
NSLog(#"color equal %i, %i",a,b);
UIColor does not define isEqual, isEqual is inherited from NSObject. Thus isEqual is comparing the addresses of the colors and will fail.
CGColor has a comparison function CGColorEqualToColor():
CGColor *c = myColor.CGColor;
Then the CGColor colors can be compared:
bool colorsEqual = CGColorEqualToColor(myColor1.CGColor, myColor2.CGColor);
Or get the individual components of the two colors and compare then individually using
- (BOOL)getRed:(CGFloat *)red green:(CGFloat *)green blue:(CGFloat *)blue alpha:(CGFloat *)alpha
Is it possible to add a shadow to the text in a UITextField?
As of 3.2, you can use the CALayer shadow properties.
_textField.layer.shadowOpacity = 1.0;
_textField.layer.shadowRadius = 0.0;
_textField.layer.shadowColor = [UIColor blackColor].CGColor;
_textField.layer.shadowOffset = CGSizeMake(0.0, -1.0);
I have a slightly different problem - I want a blurred shadow on a UILabel. Luckily, the solution to this turned out to be number (2) from Tyler
Here's my code :
- (void) drawTextInRect:(CGRect)rect {
CGSize myShadowOffset = CGSizeMake(4, -4);
CGFloat myColorValues[] = {0, 0, 0, .8};
CGContextRef myContext = UIGraphicsGetCurrentContext();
CGContextSaveGState(myContext);
CGColorSpaceRef myColorSpace = CGColorSpaceCreateDeviceRGB();
CGColorRef myColor = CGColorCreate(myColorSpace, myColorValues);
CGContextSetShadowWithColor (myContext, myShadowOffset, 5, myColor);
[super drawTextInRect:rect];
CGColorRelease(myColor);
CGColorSpaceRelease(myColorSpace);
CGContextRestoreGState(myContext);
}
This is in a class that extends from UILabel and draws the text with a shadow down and to the right 4px, the shadow is grey at 80% opacity and is sightly blurred.
I think that Tyler's solution number 2 is a little better for performance than Tyler's number 1 - you're only dealing with one UILabel in the view and, assuming that you're not redrawing every frame, it's not a hit in rendering performance over a normal UILabel.
PS This code borrowed heavily from the Quartz 2D documentation
I don't think you get built-in support for text shadows here, the way you do with UILabel.
Two ideas:
(1) [Moderately tricky to code.] Add a second UITextField behind the original, at a very small offset (maybe by (0.2,0.8)? ). You can listen to every text change key-by-key by implementing the textField:shouldChangeCharactersInRange:replacementString: method in the UITextFieldDelegate protocol. Using that, you can update the lower text simultaneously. You could also make the lower text (the shadow text) gray, and even slightly blurry using the fact that fractionally-offset text rects appear blurry. Added: Oh yea, don't forget to set the top text field's background color to [UIColor clearColor] if you go with this idea.
(2) [Even more fun to code.] Subclass UITextField and override the drawRect: method. I haven't done this before, so I'll mention up front that this depends on this being the designated drawing method, and it may turn out that you have to override another drawing function, such as drawTextInRect:, which is specific to UITextField. Now set up the drawing context to draw shadows via the CGContextSetShadow functions, and call [super drawRect:rect];. Hopefully that works -- in case the original UITextField code clears the drawing context's shadow parameters, that idea is hosed, and you'll have to write the whole drawing code yourself, which I anti-recommend because of all the extras that come with UITextFields like copy-and-paste and kanji input in Japanese.
Although the method of applying the shadow directly to the UITextView will work, it's the wrong way to do this. By adding the shadow directly with a clear background color, all subviews will get the shadow, even the cursor.
The approach that should be used is with NSAttributedString.
NSMutableAttributedString* attString = [[NSMutableAttributedString alloc] initWithString:textView.text];
NSRange range = NSMakeRange(0, [attString length]);
[attString addAttribute:NSFontAttributeName value:textView.font range:range];
[attString addAttribute:NSForegroundColorAttributeName value:textView.textColor range:range];
NSShadow* shadow = [[NSShadow alloc] init];
shadow.shadowColor = [UIColor whiteColor];
shadow.shadowOffset = CGSizeMake(0.0f, 1.0f);
[attString addAttribute:NSShadowAttributeName value:shadow range:range];
textView.attributedText = attString;
However textView.attributedText is for iOS6. If you must support lower versions, you could use the following approach. (Dont forget to add #import <QuartzCore/QuartzCore.h>)
CALayer *textLayer = (CALayer *)[textView.layer.sublayers objectAtIndex:0];
textLayer.shadowColor = [UIColor whiteColor].CGColor;
textLayer.shadowOffset = CGSizeMake(0.0f, 1.0f);
textLayer.shadowOpacity = 1.0f;
textLayer.shadowRadius = 0.0f;