How to custom border line on bottom of Label, Button, Text Field or more in Xcode 8 Swift 3

How to custom Border line on bottom of Label, Button, Text Field or more in Xcode 8 Swift 3 


The simplest solution is can use this function . Example:
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
extension UIView {
    
    func addBottomBorderWithColor(color: UIColor, width: CGFloat) {
        let border = CALayer()
        border.backgroundColor = color.cgColor
        border.frame = CGRect(x: 0, y: self.frame.size.height - width, width: frame.size.width, height: width)
        self.layer.addSublayer(border)
    }
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Put that in your function and all calls to addBottomBorderWithColor will work with swift 3
 Look away. Example:
import UIKit

extension UIView {
    func addBottomBorderWithColor(color: UIColor, width: CGFloat) {
        let border = CALayer()
        border.backgroundColor = color.cgColor
        border.frame = CGRect(x: 0, y: self.frame.size.height - width, width: frame.size.width, height: width)
        self.layer.addSublayer(border)
    }
}

class ViewController: UIViewController {
    
    @IBOutlet weak var labelText: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()
        
        labelText.addBottomBorderWithColor(color: UIColor.lightGray, width: 1)
    }
}
and can add more with functions Add to Bottom, Top, Left, or Right. 
Result here:



Look more Functions Border Line:
Add to Top Border with color
extension UIView {
    func addTopBorderWithColor(color: UIColor, width: CGFloat) {
        let border = CALayer()
        border.backgroundColor = color.cgColor
        border.frame = CGRect(x: 0, y: 0, width: frame.size.width, height: width)
        self.layer.addSublayer(border)
    }
}

Add to Bottom border with color
extension UIView {
    func addRightBorderWithColor(color: UIColor, width: CGFloat) {
        let border = CALayer()
        border.backgroundColor = color.cgColor
        border.frame = CGRect(x: frame.size.width - width, y: 0, width: width, height: self.frame.size.height)
        self.layer.addSublayer(border)
    }
}


Add to Left border with color
extension UIView {
    func addLeftBorderWithColor(color: UIColor, width: CGFloat) {
        let border = CALayer()
        border.backgroundColor = color.cgColor
        border.frame = CGRect(x: 0, y: 0, width: width, height: self.frame.size.height)
        self.layer.addSublayer(border)
    }
}


Add to Right border with color
extension UIView {
    func addRightBorderWithColor(color: UIColor, width: CGFloat) {
        let border = CALayer()
        border.backgroundColor = color.cgColor
        border.frame = CGRect(x: frame.size.width - width, y: 0, width: width, height: self.frame.size.height)
        self.layer.addSublayer(border)
    }
}




How to custom border line on bottom of Label, Button, Text Field or more in Xcode 8 Swift 3 How to custom border line on bottom of Label, Button, Text Field or more in Xcode 8 Swift 3 Reviewed by Unknown on 10:52 AM Rating: 5

No comments:

Powered by Blogger.