How do I change UITextField border from itself? - ios

I have MYTextField inherited from UITextField.
I call [self changeBorderColor] method inside MYTextField that looks like that:
self.layer.masksToBounds = YES;
self.layer.borderColor = (__bridge CGColorRef _Nullable)([UIColor brownColor]);
self.layer.borderWidth = 1;
But nothing happen. Is there way to do that?

instead of self.layer.borderColor = (__bridge CGColorRef _Nullable)([UIColor brownColor]);
write self.layer.borderColor = [UIColor brownColor].CGColor;

By using the layer property you can do like this way,
tf.layer.cornerRadius = 25.0f;
tf.layer.masksToBounds = YES;
tf.layer.borderColor = [UIColor brownColor].CGColor;
tf.layer.borderWidth = 1.5f;

Related

Why CGColorRef does not change the CALayer properties?

I wanted to change view properties using CALayer associated with it.
I have created one function:
-(void)setupViewLayer{
viewLayer1.backgroundColor = (__bridge CGColorRef _Nullable)([UIColor blueColor]);
viewLayer1.borderColor =(__bridge CGColorRef _Nullable)([UIColor redColor]);
viewLayer1.borderWidth = 100.0;
viewLayer1.shadowOpacity = 0.7;
viewLayer1.shadowRadius = 10.0;
}
viewLayer1 is view's layer whose properties I want to change.
But the view does not show the properties.
Is there anything else I need to change? Please help.
Use this code -
viewLayer1.backgroundColor = [UIColor blueColor].CGColor;
viewLayer1.borderColor = [UIColor redColor].CGColor;
viewLayer1.borderWidth = 100.0;
viewLayer1.shadowOpacity = 0.7;
viewLayer1.shadowRadius = 10.0;
Hope this helps!
Set Color by,
viewLayer1.backgroundColor = [UIColor blueColor].CGColor;
viewLayer1.borderColor = [UIColor redColor].CGColor;

iOS Button with round corners and a shadow

I have searched and tried the various solutions but without success. What I would like is a button background with rounded corners and a shadow. I can make one or the other happen but not both at the same time. Any help would be very welcome.
viewDepositButton_.layer.cornerRadius = 5.0;
CAGradientLayer *viewLayer = [CAGradientLayer layer];
[viewLayer setColors:aloColors];
[viewLayer setFrame:viewDepositButton_.bounds];
[viewDepositButton_.layer insertSublayer:viewLayer atIndex:0];
viewDepositButton_.clipsToBounds = YES;
viewDepositButton_.layer.shadowColor = [UIColor colorWithRed:0.46 green:0.46 blue:0.46 alpha:1.0].CGColor;
viewDepositButton_.layer.shadowOpacity = 0.8;
viewDepositButton_.layer.shadowRadius = 8;
viewDepositButton_.layer.shadowOffset = CGSizeMake(8.0f, 8.0f);
viewDepositButton_.clipsToBounds = YES; or viewDepositButton_.layer.masksToBounds = YES; will clip everything outside your layer - including the shadow.
However you have few options:
Put your button under a parent view, which will have the same corner radius and the shadow you want, but will have clipsToBounds = NO. This way you'll have your button as a subview of a view which has the shadow.
Use an image for your button, which has the rounded corners and set your buttons clipsToBounds to NO
Hope this helps
I was able to get your code to work by the following and it looks fine. Make sure you call your methods in proper sequence.
viewDepositButton_.layer.cornerRadius = 5.0;
viewDepositButton_.layer.borderWidth = 1.0;
viewDepositButton_.layer.shadowColor = [UIColor colorWithRed:0.46 green:0.46 blue:0.46 alpha:1.0].CGColor;
viewDepositButton_.layer.shadowRadius = 8;
viewDepositButton_.layer.shadowOpacity = 0.8;
viewDepositButton_.layer.shadowOffset = CGSizeMake(8.0f, 8.0f);
CAGradientLayer *viewLayer = [CAGradientLayer layer];
[viewLayer setColors:aloColors];
[viewLayer setFrame:viewDepositButton_.bounds];
[viewDepositButton_.layer insertSublayer:viewLayer atIndex:0];
[viewDepositButton_ viewLayer];}
Here is my code for setting up a rounded corner button with shadow.
button.layer.cornerRadius = 15;
button.layer.shadowRadius = 2.0f;
button.layer.shadowColor = [UIColor lightGrayColor].CGColor;
button.layer.shadowOffset = CGSizeMake(-1.0f, 3.0f);
button.layer.shadowOpacity = 0.8f;
button.layer.masksToBounds = NO;
Overall, the code is pretty straightforward. Please comment if you have any question, concerns or bugs.

iOS 7 UIButton alignment

I'm trying to make custom badge. For that I make subclass of UIButton and remake - (void)drawRect:(CGRect)rect like this:
- (void)drawRect:(CGRect)rect
{
self.titleLabel.font = [UIFont systemFontOfSize:12];
[self setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
self.userInteractionEnabled = NO;
self.layer.borderWidth = 1.5f;
self.layer.cornerRadius = rect.size.width / 2;
self.layer.shouldRasterize = YES;
self.layer.rasterizationScale = [[UIScreen mainScreen] scale];
self.layer.backgroundColor = [UIColor grayColor].CGColor;
self.layer.borderColor = [UIColor grayColor].CGColor;
}
And everything is good except alignment. It is a little upper than it should be. Looks like this:
What to do?
#property(nonatomic) NSTextAlignment textAlignment;
// default is NSTextAlignmentLeft
It's an attribute of UILabel.
Here is badge I need: https://github.com/jessesquires/JSCustomBadge . Works perfect!

multiple shadowOffset on UIViewController

I've looked on the web but my results mainly consist of "how to do a shadowOffset." I need to apply "Shadow,Border, and CornerRadius" to multiple objects in my views. I just wanted to see if theres a more effienct way? Or if theres a way to keep it more organized. heres some of my code. Keep in mind I have multiple views like this, so the amount of space this code takes gets pretty annoying.
topView.layer.cornerRadius = 3;
topView.layer.masksToBounds = YES;
topView.layer.borderColor= [UIColor lightGrayColor].CGColor;
topView.layer.borderWidth = 0.5f;
bottomView.layer.cornerRadius = 3;
bottomView.layer.masksToBounds = YES;
bottomView.layer.borderColor= [UIColor lightGrayColor].CGColor;
bottomView.layer.borderWidth = 0.5f;
eventName.layer.masksToBounds = NO;
eventName.layer.shadowColor = [UIColor blackColor].CGColor;
eventName.layer.shadowOpacity = 0.5;
eventName.layer.shadowRadius = 2;
//(right,down) also (-right,-down)
eventName.layer.shadowOffset = CGSizeMake(0.0f, 0.8f);
addressLabel.layer.masksToBounds = NO;
addressLabel.layer.shadowColor = [UIColor blackColor].CGColor;
addressLabel.layer.shadowOpacity = 0.5;
addressLabel.layer.shadowRadius = 2;
//(right,down) also (-right,-down)
addressLabel.layer.shadowOffset = CGSizeMake(0.0f, 0.8f);
dateLabel.layer.masksToBounds = NO;
dateLabel.layer.shadowColor = [UIColor blackColor].CGColor;
dateLabel.layer.shadowOpacity = 0.5;
dateLabel.layer.shadowRadius = 2;
//(right,down) also (-right,-down)
dateLabel.layer.shadowOffset = CGSizeMake(0.0f, 0.8f);
typeLabel.layer.masksToBounds = NO;
typeLabel.layer.shadowColor = [UIColor blackColor].CGColor;
typeLabel.layer.shadowOpacity = 0.5;
typeLabel.layer.shadowRadius = 2;
//(right,down) also (-right,-down)
typeLabel.layer.shadowOffset = CGSizeMake(0.0f, 0.8f);
eventCaption.layer.masksToBounds = NO;
eventCaption.layer.shadowColor = [UIColor blackColor].CGColor;
eventCaption.layer.shadowOpacity = 0.5;
eventCaption.layer.shadowRadius = 2;
//(right,down) also (-right,-down)
eventCaption.layer.shadowOffset = CGSizeMake(0.0f, 0.8f);
If most of your shadows are identical, you could choose to loop over an array (or a set, since you don't really care about the order of the items it is applied to) of your views to apply the same shadow ?
That is, I see 2 kind of shadows in the code you pasted here, the lightGray and the black.
You could do something like :
NSArray * blackShadowItems = #[eventName, addressLabel, dateLabel];
for (UIView * view in blackShadowItems) {
view.layer.masksToBounds = NO;
view.layer.shadowColor = [UIColor blackColor].CGColor;
view.layer.shadowOpacity = 0.5;
view.layer.shadowRadius = 2;
//(right,down) also (-right,-down)
view.layer.shadowOffset = CGSizeMake(0.0f, 0.8f);
}
or declare another function :
- (void)setBlackShadow:(UIView *)view {
view.layer.masksToBounds = NO;
view.layer.shadowColor = [UIColor blackColor].CGColor;
view.layer.shadowOpacity = 0.5;
view.layer.shadowRadius = 2;
//(right,down) also (-right,-down)
view.layer.shadowOffset = CGSizeMake(0.0f, 0.8f);
}
You can combine both of these solutions.
Finally, note that if these views are declared in a .xib file, you could declare an IBOutletCollection to regroup the views according to the type of shadow you want to set on it.
This is some quite similar to declaring your NSArray or NSSet yourself.

Add shadow to frame of UITextView add also shadow to text

I have a UITextView where I add some drop shadow to the frame, but when I write, the text got also the same shadow. How to avoid this problem?
My code :
commentary = [[UITextView alloc]initWithFrame:CGRectMake(10, 435, 230, 120)];
commentary.font = STANDARDFONT;
commentary.backgroundColor = BACKGROUND;
commentary.layer.shadowColor = [UIColor blackColor].CGColor;
commentary.layer.shadowOffset = CGSizeMake(2, 2);
commentary.layer.shadowOpacity = 0.8;
commentary.layer.shadowRadius = 2.0;
commentary.layer.borderColor = [UIColor grayColor].CGColor;
commentary.layer.borderWidth = 1.5;
commentary.layer.cornerRadius = 5;
commentary.layer.masksToBounds = NO;
commentary.clipsToBounds = NO;
[self addSubview:commentary];
BACKGROUND and STANDARDFONT is [UICOLOR clearColor].CGColorand [UIFont fontWithName:#"TimesNewRomanPSMT" size:16];
Try setting the layer background color also: commentary.layer.backgroundColor = BACKGROUND.CGColor

Resources