UITextField having bottom border only in Objective-C - ios

I am creating UITextField programatically and i am trying to create a textField that has only bottom border as given in the figure. Please help this problem in objective-c programatically ?

First:
Add and import QuartzCore framework.
#import <QuartzCore/QuartzCore.h>
Second:
CALayer *border = [CALayer layer];
CGFloat borderWidth = 2;
border.borderColor = [UIColor grayColor].CGColor;
border.frame = CGRectMake(0, textField.frame.size.height - borderWidth, textField.frame.size.width, textField.frame.size.height);
border.borderWidth = borderWidth;
[textField.layer addSublayer:border];
textField.layer.masksToBounds = YES;
EDIT:
If you have more TextFields, make one common method which takes UITextField and applies border to it like below:
-(void)SetTextFieldBorder :(UITextField *)textField{
CALayer *border = [CALayer layer];
CGFloat borderWidth = 2;
border.borderColor = [UIColor grayColor].CGColor;
border.frame = CGRectMake(0, textField.frame.size.height - borderWidth, textField.frame.size.width, textField.frame.size.height);
border.borderWidth = borderWidth;
[textField.layer addSublayer:border];
textField.layer.masksToBounds = YES;
}
Pass your TextField as follows to set Bottom Border:
[self SetTextFieldBorder:YourTextField];

Or you can add a thin line subview to the textfield that will mimic the border:
UIView *lineView = [[UIView alloc] init];
lineView.translatesAutoresizingMaskIntoConstraints = false;
lineView.backgroundColor = [UIColor grayColor];
[textField addSubview:lineView];
[lineView.heightAnchor constraintEqualToConstant:1];
[lineView.leftAnchor constraintEqualToAnchor:textField.leftAnchor constant:5.0];
[lineView.rightAnchor constraintEqualToAnchor:textField.rightAnchor constant:-5.0];
[lineView.bottomAnchor constraintEqualToAnchor:textField.bottomAnchor constant:0.0];
Swift version:
let lineView = UIView()
lineView.translatesAutoresizingMaskIntoConstraints = false
lineView.backgroundColor = UIColor.grayColor()
textField.addSubview(lineView)
lineView.heightAnchor.constraintEqualToConstant(1)
lineView.leftAnchor.constraintEqualToAnchor(textField.leftAnchor)
lineView.rightAnchor.constraintEqualToAnchor(textField.rightAnchor)
lineView.bottomAnchor.constraintEqualToAnchor(textField.bottomAnchor)

Related

How to add one border line for UIView

I am trying to add border line between 2 UIView, as i researched, the following code can add 4 borders.
CGFloat borderWidth = 5.0f;
self.imgview.frame = CGRectInset(self.imgview. frame, -borderWidth, -borderWidth);
self.imgview. layer.borderColor = [UIColor blueColor].CGColor;
self.imgview. layer.borderWidth = borderWidth;
However, i just need to add one border, any advice? Thanks in advance.
You can add one One more UIView (BorderView) between two UIView (View1 and View2). Give the border view appropriate width and background color.
If you want to set these borders programmatically, you can use the following snippet of code
+(void)SetImageViewBottomBorder :(UIImageView *)imageView{
CALayer *border = [CALayer layer];
CGFloat borderWidth = 1;
border.borderColor = [[self colorWithHexString:#"9B9B9B"] CGColor];
border.frame = CGRectMake(0, imageView.frame.size.height - borderWidth, imageView.frame.size.width, imageView.frame.size.height);
border.borderWidth = borderWidth;
[imageView.layer addSublayer:border];
imageView.userInteractionEnabled = YES;
imageView.layer.masksToBounds = YES;
}
you can replace the UIImageView with UIView and it will be working like a charm.

Add a bottom border in a UIButton with rounded corners

How can I add a right and left border in a UIButton with rounded corners?
This is my button and I want rounded corners in the border too:
And the source code of my button:
- (void)initialise;
{
//add border
[self addBottomBorderWithColor:[UIColor grayColor] andWidth:2];
[self addRightBorderWithColor:[UIColor grayColor] andWidth:2];
//round
self.layer.cornerRadius = 8.0f;
}
- (void)addTopBorderWithColor:(UIColor *)color andWidth:(CGFloat) borderWidth
{
CALayer *border = [CALayer layer];
border.backgroundColor = color.CGColor;
border.frame = CGRectMake(0, 0, self.frame.size.width, borderWidth);
[self.layer addSublayer:border];
}
- (void)addRightBorderWithColor:(UIColor *)color andWidth:(CGFloat) borderWidth
{
CALayer *border = [CALayer layer];
border.backgroundColor = color.CGColor;
border.frame = CGRectMake(self.frame.size.width, 0, borderWidth, self.frame.size.height);
[self.layer addSublayer:border];
}
you can add a single CALayer slightly larger than your button and offsetting it with a negative margin so that top and left border are hidden. Mind that i cannot test it right now, and it may need further adjustments but it should give you the general idea:
-(void)initialise;
{
//add border
[self addBorderWithColor:[UIColor grayColor] andWidth:2];
//round
self.layer.cornerRadius = 8.0f;
}
- (void)addBorderWithColor:(UIColor *)color andWidth:(CGFloat) borderWidth
{
CALayer *border = [CALayer layer];
border.borderColor = color.CGColor;
border.frame = CGRectMake(-borderWidth, -borderWidth, self.frame.size.width+borderWidth, self.frame.size.height+borderWidth);
border.borderWidth = borderWidth;
border.cornerRadius = 8.f;
[self.layer addSublayer:border];
self.layer.masksToBounds = YES;
}

How to add a shadow to CALayer that is masked to custom content

I'm trying to add a shadow to a masked UIView (from a UIImage), but I believe because the view is masked to bounds, masksToBounds = YES the shadow isn't showing.
If this is indeed the case, how then can I accomplish drawing a shadow around a masked image?
-(UIView *)modifyViewWithMask:(UIView *)view
{
view.alpha = 0.5;
UIView *blackView = [[UIView alloc] initWithFrame:view.bounds];
blackView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
blackView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.3];
[view addSubview:blackView];
UIImage* bubbleImage = [UIImage imageNamed:#"Bubble"];
CALayer* mask = [CALayer layer];
mask.contents = (id)[bubbleImage CGImage];
mask.frame = CGRectMake(0, 0, view.frame.size.width, view.frame.size.height);
view.layer.mask = mask;
view.layer.masksToBounds = YES;
view.layer.shadowColor = [UIColor blackColor].CGColor;
view.layer.shadowOffset = CGSizeMake(10, 10);
view.layer.shadowRadius = 10;
view.layer.shadowOpacity = 1;
}

Add just a top border to an UIView with Quartzcore/layer?

Is it possible to add a border just on top of a UIView, if so, how please?
I just Testing Bellow few line of Code and it works very nice, just test it in to your Project. hope you'll get your solution easily.
Why to create new View and adding it into your existing view..? For this task simply create one CALayer and add it into your existing UIView's Layer do as following:-
#import <QuartzCore/QuartzCore.h>
- (void)viewDidLoad
{
CALayer *TopBorder = [CALayer layer];
TopBorder.frame = CGRectMake(0.0f, 0.0f, myview.frame.size.width, 3.0f);
TopBorder.backgroundColor = [UIColor redColor].CGColor;
[myview.layer addSublayer:TopBorder];
[super viewDidLoad];
}
and It's Output is:-
i've find solution for me, here's the tricks :
CGSize mainViewSize = self.view.bounds.size;
CGFloat borderWidth = 1;
UIColor *borderColor = [UIColor colorWithRed:37.0/255 green:38.0/255 blue:39.0/255 alpha:1.0];
UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, mainViewSize.width, borderWidth)];
topView.opaque = YES;
topView.backgroundColor = borderColor;
topView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin;
[self.view addSubview:topView];
GilbertOOI's answer in Swift 2:
let topBorder: CALayer = CALayer()
topBorder.frame = CGRectMake(0.0, 0.0, myView.frame.size.width, 3.0)
topBorder.backgroundColor = UIColor.redColor().CGColor
myView.layer.addSublayer(topBorder)
Here's a UIView category that lets you add a layer-back or view-backed border on any side of the UIView: UIView+Borders
GilbertOOI's answer in Swift 4:
let topBorder: CALayer = CALayer()
topBorder.frame = CGRect(x: 0, y: 0, width: myView.frame.size.width, height: 1)
topBorder.backgroundColor = UIColor.purple.cgColor
myView.layer.addSublayer(topBorder)
I created this simple UIView subclass so that it works in Interface Builder and works with constraints:
https://github.com/natrosoft/NAUIViewWithBorders
Here's my blog post about it:
http://natrosoft.com/?p=55
-- Basically just drop in a UIView in Interface Builder and change its class type to NAUIViewWithBorders.
-- Then in your VC's viewDidLoad do something like:
/* For a top border only ———————————————- */
self.myBorderView.borderColorTop = [UIColor redColor];
self.myBorderView..borderWidthsAll = 1.0f;
/* For borders with different colors and widths ————————— */
self.myBorderView.borderWidths = UIEdgeInsetsMake(2.0, 4.0, 6.0, 8.0);
self.myBorderView.borderColorTop = [UIColor blueColor];
self.myBorderView.borderColorRight = [UIColor redColor];
self.myBorderView.borderColorBottom = [UIColor greenColor];
self.myBorderView.borderColorLeft = [UIColor darkGrayColor];
Here's a direct link to the .m file so you can see the implementation: NAUIViewWithBorders.m
There is a demo project as well.
remus' answer in Obj-C:
CALayer *topBorder = [CALayer new];
topBorder.frame = CGRectMake(0.0, 0.0, self.frame.size.width, 3.0);
topBorder.backgroundColor = [UIColor redColor].CGColor;
[myView.layer addSublayer:topBorder];
Swift5:
We will write a separate method to add borders to this view. To add borders to this view we will create two layers with the desired thickness. We will set the frame of these two layers to the top and bottom of the view. We will set the desired background color of the borders on these layers and add these layers as subLayers to the view.
func addTopBorders() {
let thickness: CGFloat = 1.0
let topBorder = CALayer()
topBorder.frame = CGRect(x: 0.0, y: 0.0, width:
self.down_view_outlet.frame.size.width, height: thickness)
topBorder.backgroundColor = UIColor.white.cgColor
down_view_outlet.layer.addSublayer(topBorder)
}

iOS add bottom border to UILabel with shadow

May i know how do i add bottom border to UILable only with shadow?
No top, left, right border.
this is not working. Here is the code I've tried but it's not working.
CALayer* layer = [titleLabel layer];
layer.frame = CGRectMake(-1, -1, titleLabel.frame.size.width, 1.0f);
layer.borderWidth=1;
[layer setBorderWidth:2.0f];
[layer setBorderColor:[UIColor blackColor].CGColor];
[layer setShadowOffset:CGSizeMake(-3.0, 3.0)];
[layer setShadowRadius:5.0];
[layer setShadowOpacity:5.0];
Test in this way:
UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 50)];
[lbl setText:#"Testo di prova..."];
[lbl setBackgroundColor:[UIColor clearColor]];
[[self view] addSubview:lbl];
[lbl sizeToFit];
CALayer* layer = [lbl layer];
CALayer *bottomBorder = [CALayer layer];
bottomBorder.borderColor = [UIColor darkGrayColor].CGColor;
bottomBorder.borderWidth = 1;
bottomBorder.frame = CGRectMake(-1, layer.frame.size.height-1, layer.frame.size.width, 1);
[bottomBorder setBorderColor:[UIColor blackColor].CGColor];
[layer addSublayer:bottomBorder];
I hope this helps you
Just posting this answers for reference, since the user already got the answer,
Try this code in Swift and let me know how it goes..
func buttomBorder(label: UILabel) -> UILabel {
// For Buttom Border
let frame = label.frame
let bottomLayer = CALayer()
bottomLayer.frame = CGRect(x: 0, y: frame.height - 1, width: frame.width - 2, height: 1)
bottomLayer.backgroundColor = UIColor.lightGray.cgColor
label.layer.addSublayer(bottomLayer)
//For Shadow
label.layer.shadowColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.25).CGColor
label.layer.shadowOffset = CGSizeMake(0.0, 2.0)
label.layer.shadowOpacity = 1.0
label.layer.shadowRadius = 0.0
label.layer.masksToBounds = false
label.layer.cornerRadius = 4.0
return label
}
Function Call:
labelName = buttomBoder(label: labelName)

Resources