UITextField border for right side only in swift - ios

In my swift project, I want to add right side border only for my textfield, because, I have two textfields in single tableviewcell, so, I want to show them as separated with border of right side for first textfield.
Any suggestions, thanks!!

I have used this method for textfield
let border = CALayer()
let height = CGFloat(2.0)
border.borderColor = UIColor.darkGray.cgColor
border.frame = CGRect(x: searchField.frame.size.width - height, y: 0, width: searchField.frame.size.width, height: searchField.frame.size.height)
border.borderWidth = height
searchField.layer.addSublayer(border)
searchField.layer.masksToBounds = true
Below is the outcome of the code above

Try This.
CGFloat width = 5;
UIView *bottomBorder = [[UIView alloc] initWithFrame:CGRectMake(self.textField1.frame.size.width - width,0,width,_textField1.frame.size.height)];
[bottomBorder setBackgroundColor:[UIColor redColor]];
[_textField1 addSubview:bottomBorder];

Choice is yours,
Way 1: Create IBDesignable & assign it to textfield in class attribute,
import UIKit
#IBDesignable
class IBDesignableTextField: UITextField {
// Only override draw() if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
override func draw(_ rect: CGRect) {
let border = CALayer()
let height = CGFloat(2.0)
border.borderColor = UIColor.darkGray.cgColor
border.frame = CGRect(x: self.frame.size.width - height, y: 0, width: self.frame.size.width, height: self.frame.size.height)
border.borderWidth = height
self.layer.addSublayer(border)
self.layer.masksToBounds = true
}
}
Way 2: #Dixit Akabari explained
Way 3: You can simply add right view to textfield as a your border image

Related

Creating two textfields with only bottom border swift

I'm trying to create two UITextFields that both only have the bottom border, I got some code from another StackOverflow and it worked. However, when I copy and pasted the code again for my second TextField it only shows one TextField with the bottom border.
Here is the code:
let border = CALayer()
let width = CGFloat(2.0)
border.borderColor = UIColor.gray.cgColor
border.frame = CGRect(x: 0, y: emailText.frame.size.height -
width, width: emailText.frame.size.width, height:
emailText.frame.size.height)
border.frame = CGRect(x: 0, y: passwordText.frame.size.height
- width, width: passwordText.frame.size.width, height:
passwordText.frame.size.height)
border.borderWidth = width
passwordText.layer.addSublayer(border)
passwordText.layer.masksToBounds = true
border.borderWidth = width
emailText.layer.addSublayer(border)
emailText.layer.masksToBounds = true
Thanks for the help!
CALayers are instance-based, that means if you added the layer to a view, you cannot add it to a new one without removing it from the first view. Create two instances of CALayers or better yet, make an extension that produces the desired result.
Create separate border object for email textfield and username textfield.
let emailborder = CALayer()
let passwordborder = CALayer()
let width = CGFloat(2.0)
emailborder.borderColor = UIColor.gray.cgColor
emailborder.frame = CGRect(x: 0, y: emailText.frame.size.height -
width, width: emailText.frame.size.width, height:
emailText.frame.size.height)
passwordborder.frame = CGRect(x: 0, y: passwordText.frame.size.height
- width, width: passwordText.frame.size.width, height:
passwordText.frame.size.height)
passwordborder.borderWidth = width
passwordText.layer.addSublayer(passwordborder)
passwordText.layer.masksToBounds = true
emailborder.borderWidth = width
emailText.layer.addSublayer(emailborder)
emailText.layer.masksToBounds = true
You cant use the same view for both textfield. Use a copy of border for the second one. Easiest way to make this work is to refine a variable as border2 and set it up and sublayer it. Don't forget to remove your duplicate frame assign for boarder.
let border = CALayer()
let width = CGFloat(2.0)
border.borderColor = UIColor.gray.cgColor
border.frame = CGRect(x: 0, y: emailText.frame.size.height - width, width: emailText.frame.size.width, height: emailText.frame.size.height)
let border2 = CALayer() border2.borderColor = border.borderColor border2.frame = border.frame;
border.borderWidth = width
passwordText.layer.addSublayer(border)
passwordText.layer.masksToBounds = true
border.borderWidth = width
emailText.layer.addSublayer(border2)
emailText.layer.masksToBounds = true
The best you could without writing redundant code is that you create a function, pass the textfields you want to border as parameters and keep the styling code in there like so:
func setBorder(tf: UITextField) {
let border = CALayer()
let width = CGFloat(2.0)
border.borderColor = UIColor.gray.cgColor
border.frame = CGRect(x: 0, y: tf.frame.size.height - width,
width: tf.frame.size.width, height:
tf.frame.size.height)
border.borderWidth = width
tf.layer.addSublayer(border)
tf.layer.masksToBounds = true
}
Call this function in viewDidLoad:
setBorder(tf: emailText)
setBorder(tf: passwordText)
Hope this helps!

Swift textfields without border

I am new to swift. Your help will be really appreciated.
I have two textfields in my application. How would I create same UI as given in the pic below.
I want to create textfields with only one below border as given in the screenshot.
https://www.dropbox.com/s/wlizis5zybsvnfz/File%202017-04-04%2C%201%2052%2024%20PM.jpeg?dl=0
#IBOutlet var textField: UITextField! {
didSet {
let border = CALayer()
let width: CGFloat = 1 // this manipulates the border's width
border.borderColor = UIColor.darkGray.cgColor
border.frame = CGRect(x: 0, y: textField.frame.size.height - width,
width: textField.frame.size.width, height: textField.frame.size.height)
border.borderWidth = width
textField.layer.addSublayer(border)
textField.layer.masksToBounds = true
}
}
Create a subclass of UITextField so you can reuse this component across multiple views without have to re implement the drawing code. Expose various properties via #IBDesignable and #IBInspectable and you can have control over color and thickness in the story board. Also - implement a "redraw" on by overriding layoutSubviews so the border will adjust if you are using auto layout and there is an orientation or perhaps constraint based animation. That all said - effectively your subclass could look like this:
import UIKit
class Field: UITextField {
private let border = CAShapeLayer()
#IBInspectable var color: UIColor = UIColor.blue {
didSet {
border.strokeColor = color.cgColor
}
}
#IBInspectable var thickness: CGFloat = 1.0 {
didSet {
border.lineWidth = thickness
}
}
override func draw(_ rect: CGRect) {
self.borderStyle = .none
let from = CGPoint(x: 0, y: rect.height)
let here = CGPoint(x: rect.width, y: rect.height)
let path = borderPath(start: from, end: here).cgPath
border.path = path
border.strokeColor = color.cgColor
border.lineWidth = thickness
border.fillColor = nil
layer.addSublayer(border)
}
override func layoutSubviews() {
super.layoutSubviews()
let from = CGPoint(x: 0, y: bounds.height)
let here = CGPoint(x: bounds.width, y: bounds.height)
border.path = borderPath(start: from, end: here).cgPath
}
private func borderPath(start: CGPoint, end: CGPoint) -> UIBezierPath {
let path = UIBezierPath()
path.move(to: start)
path.addLine(to: end)
return path
}
}
Then when you add a text field view to your story board - update the class in the Identity Inspector to use this subclass, Field - and then in the attributes inspector, you can set color and thickness.
Add border at Bottom in UITextField call below function:
func setTextFieldBorder(_ dimension: CGRect) -> CALayer {
let border = CALayer()
let width = CGFloat(2.0)
border.borderColor = UIColor.darkGray.cgColor
border.frame = CGRect(x: 0, y: dimension.size.height - width, width: dimension.size.width, height: dimension.size.height)
border.borderWidth = width
return border
}
How to set UITextField border in textField below sample code for that:
txtDemo.layer.addSublayer(setTextFieldBorder(txtDemo.frame))
txtDemo.layer.masksToBounds = true
Where txtDemo is IBOutlet of UITextField.

Add a bottom border to UITableView in swift?

I would like to add a bottom border to my table view in swift, which will act as a separator. I tried the answers to some other questions, but most of them included CGRectMake, which is now outdated. Any thoughts?
Here is the code I am using, which doesn't produce an error message, but doesn't work:
let border = CALayer()
let width = CGFloat(2.0)
border.borderColor = UIColor.darkGray.cgColor
border.frame = CGRect(x: 0, y: tableView.frame.size.height - width, width: tableView.frame.size.width, height: tableView.frame.size.height)
border.borderWidth = width
tableView.layer.addSublayer(border)
tableView.layer.masksToBounds = true
Thank you!

Underline UITextField does not work in StackView

I'm having multiple textfields for sign-up page and I have used stack view in it. When I try to assign underline colour to my UITextField it does not work. But if I do the same w/o StackView then it works fine.
extension UITextField {
func underlined() {
let border = CALayer()
let width = CGFloat(1.0)
border.borderColor = themeColor.cgColor
border.frame = CGRect(x: 0, y: self.frame.size.height - width, width: self.frame.size.width, height: self.frame.size.height)
border.borderWidth = width
self.layer.addSublayer(border)
self.layer.masksToBounds = true
}
}
nameTextField.underlined()
ageTextField.underlined()

How to create underlined UITextField

iOS noob here
When I add a default UITextField to the ViewController, it looks like this:
My designer would like me to make the UITextField look like this (i.e. background is transparent, and just has an underline:
How do I do this? Should be asking my designer for an image to set as background for the UITextField. What should the image look like? Should the image be = the blue background + the underline (minus the number i guess)
Is that right?
UPDATE:
Based on direction below (from #Ashish Kakkad), added this code in Swift to ViewDidLoad:
var bottomLine = CALayer()
bottomLine.frame = CGRectMake(0.0, view.frame.height - 1, view.frame.width, 1.0)
bottomLine.backgroundColor = UIColor.whiteColor().CGColor
tf_editPassword.borderStyle = UITextBorderStyle.None
tf_editPassword.layer.addSublayer(bottomLine)
This almost works, but the problem is that all side borders disappear, I want just the bottom line to stay.
Try this code :
Obj-C
CALayer *bottomLine = [CALayer layer];
bottomLine.frame = CGRectMake(0.0f, self.frame.size.height - 1, self.frame.size.width, 1.0f);
bottomLine.backgroundColor = [UIColor whiteColor].CGColor;
[myTextField setBorderStyle:UITextBorderStyleNone];
[myTextField.layer addSublayer:bottomLine];
Swift
var bottomLine = CALayer()
bottomLine.frame = CGRectMake(0.0, view.frame.height - 1, view.frame.width, 1.0)
bottomLine.backgroundColor = UIColor.whiteColor().CGColor
myTextField.borderStyle = UITextBorderStyle.None
myTextField.layer.addSublayer(bottomLine)
Hope it helps
If you are using the auto-layout then try to do this code inside the viewDidLayoutSubviews method.
#Ashish Kakkad has posted the right code but the reason it might not be working for #user1406716 is because when there is no text in the textfield, the height and width of the frame are 0 so maybe you can try the following :
Obj-C :
CALayer *bottomLine = [CALayer layer];
bottomLine.frame = CGRectMake(0.0f, 75 - 1, 300, 1.0f);
bottomLine.backgroundColor = [UIColor whiteColor].CGColor;
[myTextField setBorderStyle:UITextBorderStyleNone];
[myTextField.layer addSublayer:bottomLine];
Swift :
var bottomLine = CALayer()
bottomLine.frame = CGRectMake(0.0, 75 - 1, 300, 1.0)
bottomLine.backgroundColor = UIColor.whiteColor().CGColor
myTextField.borderStyle = UITextBorderStyle.None
myTextField.layer.addSublayer(bottomLine)
This will ensure that the line is visible even when there is no text in the textfield.
Use this Extension for UITextField
extension UITextField {
func setUnderLine() {
let border = CALayer()
let width = CGFloat(0.5)
border.borderColor = UIColor.darkGray.cgColor
border.frame = CGRect(x: 0, y: self.frame.size.height - width, width: self.frame.size.width - 10, height: self.frame.size.height)
border.borderWidth = width
self.layer.addSublayer(border)
self.layer.masksToBounds = true
}
}
Call the above function in ViewDidLoad() and display the textfield with an underline.
myTextField.setUnderLine()
To update this with Swift 3:
let bottomLine = CALayer()
bottomLine.frame = CGRect(origin: CGPoint(x: 0, y:first.frame.height - 1), size: CGSize(width: first.frame.width, height: 1))
bottomLine.backgroundColor = UIColor.white.cgColor
first.borderStyle = UITextBorderStyle.none
first.layer.addSublayer(bottomLine)
Delete borders and add underline - Swift 5:
extension UITextField {
func addUnderLine () {
let bottomLine = CALayer()
bottomLine.frame = CGRect(x: 0.0, y: self.bounds.height + 3, width: self.bounds.width, height: 1.5)
bottomLine.backgroundColor = UIColor.lightGray.cgColor
self.borderStyle = UITextField.BorderStyle.none
self.layer.addSublayer(bottomLine)
}
}

Resources