Change foregroundColor of a UIView - ios

Is there a way to change the foregroundColor of a UIView?
I'm creating an arrow using an UIView (drawRect) like this ">", with a clear background, only two black lines. This is working great. But afterwards I would like to change the color of the ">" from black to red, for example. Would be nice to have an animation in it, like a gradient from black to red, using CAKeyframeAnimation. I can do it for borderColor and backgroundColor but these properties are not the ones that I'm looking for.
I'm changing the borderColor of another UIView using this block of animation. I would like to do the same with the foregroundColor but it's not working in iOS.
CAKeyframeAnimation* colorAnim = [CAKeyframeAnimation animationWithKeyPath:#"borderColor"];
NSArray* colorValues = [NSArray arrayWithObjects:(id)[UIColor blackColor].CGColor,(id)[UIColor redColor].CGColor, nil];
colorAnim.values = colorValues;
colorAnim.calculationMode = kCAAnimationPaced;
colorAnim.duration = 5.0;
colorAnim.repeatCount = 1;
[self.myView.layer addAnimation:colorAnim forKey:#"borderColor"];
I appreciate any help, thanks!

The UIView class does not have a foregroundColor property, so you have to implement it yourself. In your UIView subclass interface, define the property:
#property (nonatomic, strong) UIColor *foregroundColor;
In your subclass implementation, override the setForegroundColor: setter method:
- (void)setForegroundColor:(UIColor *)newForegoundColor
{
_foregroundColor = newForegroundColor;
[self setNeedsDisplay]; // This is need so that drawRect: is called
}
In whichever initialiser you're are using in the subclass, set the foregroundColor property to a default:
self.foregroundColor = [UIColor blackColor];
In your drawRect: implementation, use the foregroundColor property to stroke the chevron you are drawing:
CGContextSetStrokeColorWithColor(context, self.foregroundColor.CGColor);

Related

How to set border color of UIView at runtime [duplicate]

This question already has answers here:
Is it possible to set UIView border properties from interface builder?
(9 answers)
Closed 6 years ago.
I have required to set border color UIView using User Defined Runtime Attributesin identity inspector. By default Color isBlackColor`. If any constraints is available in iOS then tell me.
This will work -
UIView *view = [[UIView alloc] init];
...
//Add a Your Color border
view.layer.borderColor = [UIColor colorWithWhite:1.0f alpha:1.0f].CGColor;
view.layer.borderWidth = 1.0f; //make border 1px thick
you can set border color like,
yourView.layer.borderColor = [UIColor orangeColor].CGColor;
yourView.layer.borderWidth = 1.0; //you have to give width to make border visible
Update according to comment :
you can set runtime attributes for view like below screenshot,
But it will display black border because layer uses cgcolor and you can't get cgcolor reference in interface builder. so you can't set layer color directly as runtime attribute. It's better to set from code as i have mentioned above.
If you hardly want to set color from runtime attribute only then you can try Peter Deweese's answer.
try this code it will help you:
yourViewNAme.layer.borderWidth = 1;
yourViewNAme.layer.borderColor = [UIColor DesiredColour].CGColor;
please try this code, ViewController.h
declare #property (weak, nonatomic) IBOutlet UIView *searchView; In ViewController.m.
viewDidLoad Method:
searchView.layer.cornerRadius = 8.0f;
searchView.layer.borderColor = [UIColor lightGrayColor].CGColor;
searchView.layer.borderWidth = 0.8f;
searchView.layer.shadowColor = [UIColor lightGrayColor].CGColor;
searchView.layer.shadowOpacity = 0.8f;
searchView.layer.shadowRadius = 3.0f;
searchView.layer.shadowOffset =CGSizeMake(2.0f, 2.0f);

How do I make a UISwitch under iOS 7 not take the background colour of the view behind it?

It looks like this whenever off:
While I'd prefer more of a grey background. Do I really have to use a UIImageView?
Here is how I changed the fill color of my iOS7 UISwitch.
First you need to import QuartzCore.
#import <QuartzCore/QuartzCore.h>
Then set the background color and round the UISwitch's corners.
UISwitch *mySwitch = [[UISwitch alloc] initWithFrame:CGRectMake(0.0, 0.0, 51.0, 31.0)];
mySwitch.backgroundColor = [UIColor redColor];
mySwitch.layer.cornerRadius = 16.0; // you must import QuartzCore to do this.
[self addSubview:mySwitch];
This will give you a UISwitch with a custom off (background) color.
Hope this helps someone:)
You can set the setOnTintColor property of your UISwitch to the color you desire.
You can also set this for the switch in Interface Builder. Just set the background colour of the UISwitch to whatever colour you want (white, in the example below), then set a User Defined Runtime Attribute of layer.cornerRadius = 16:
There's no API support for changing the off fill color of a UISwitch.
Adjusting the tintColor will only affect the outline, and adjusting the backgroundColor will affect the whole frame, including the parts outside the rounded bounds.
You either have to place a properly shaped opaque UIView behind it or - easier - use a custom open source implementation, such as MBSwitch, which allows you to set the off fill color.
You can also use an image as background, using the [UIColor colorWithPatternImage];
mySwitch.onTintColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"toggle-bg-on"]];
mySwitch.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"toggle-bg-off"]];
Adding to Barry Wyckoff solution : set tint color also
UISwitch *mySwitch = [[UISwitch alloc] initWithFrame:CGRectMake(0.0, 0.0, 51.0, 31.0)];
mySwitch.backgroundColor = [UIColor redColor];
mySwitch.layer.cornerRadius = 16.0; // you must import QuartzCore to do this.
mySwitch.tintColor = [UIColor redColor];
[self addSubview:mySwitch];

UIControl Lifecycle

Is there a documented Lifecycle for a UIControl somewhere?
Here's why I ask:
Suppose I have a UITextField. I could easily wire up a button that changes the border color like so:
myTextField.layer.borderColor = [[UIColor redColor] CGColor];
Now suppose I have a custom control that's a subclass of UIControl. That same code will not change the border color unless I also issue setNeedsLayout, like so:
[myControl setNeedsLayout];
Is there an event method somewhere that I need to implement to make this work without the setNeedsLayout?
For future generations, here's how I solved the problem on my own.
In my .h file:
#property (nonatomic, strong, setter = setBorderColor:) UIColor *borderColor;
In my .m file:
- (void)setBorderColor:(UIColor *)clr {
borderColor = clr;
myControl.layer.borderColor = borderColor.CGColor;
}
Works like a charm.

CATextLayer in Interface Builder?

I would like to use a CATextLayer to display some partially bolded text in the form of a NSAttributedString - but would still like to use the convenience of IB for positioning.
Is there a way to drop in a CATextLayer with interface builder? Or what is the next best solution?
You could configure a UIView subclass in IB then set its layerClass to CATextLayer in code:
+ (Class)layerClass;
{
return [CATextLayer class];
}
In the view's init method(s) configure your CATextLayer properties.
To access the layer's properties:
CATextLayer *textLayer = (CATextLayer *)self.layer;
textLayer.string = #"Foo";
// etc...

set label border in iphone

How can I set label's border which is dynamically generated (Not from Interface Builder)?
you can do it by
Label.layer.borderColor = [UIColor whiteColor].CGColor;
Label.layer.borderWidth = 4.0;
before this you need to import a framework QuartzCore/QuartzCore.h
Swift version
Set label border
label.layer.borderWidth = 2.0
Set border color
label.layer.borderColor = UIColor.blueColor().CGColor
Use rounded corners
label.layer.cornerRadius = 8
Make background color stay within rounded corners
label.layer.masksToBounds = true
You can also try to subclass your label and override the drawRect: method to draw or a border or whatever you like:
- (void)drawRect:(CGRect)rect
{
[super drawRect:rect];
CGContextRef context = UIGraphicsGetCurrentContext();
[[UIColor blackColor] setStroke];
CGContextStrokeRect(context, self.bounds);
}
I'm not sure you can by default with UILabel. You might want to consider using a read-only (field.editing = NO) UITextField and setting it's borderStyle (which can be done programmatically using a UITextBorderStyle). That may be a little 'heavy' though. Another option may be to sub-class UILabel to draw your border.
Alternatively, and depending on your needs this may be better, use the backing CALayer and draw a border using it's borderColor and borderWidth properties.

Resources