Add bottom line to view in SwiftUI / Swift / Objective-C / Xamarin

前端 未结 22 2072
自闭症患者
自闭症患者 2020-11-27 09:19

I would like to keep the border at the bottom part only in UITextField. But I don\'t know how we can keep it on the bottom side.

Can you please advise m

相关标签:
22条回答
  • 2020-11-27 09:59

    For View : (Most Recommended)

    It works for all type of UIView subclass (view, textfiled, label, etc...) using UIView extension

    It is more simple and convenient. But the only condition is the view must contain an auto layout.

    extension UIView {
        enum Line_Position {
            case top
            case bottom
        }
    
        func addLine(position : Line_Position, color: UIColor, height: Double) {
            let lineView = UIView()
            lineView.backgroundColor = color
            lineView.translatesAutoresizingMaskIntoConstraints = false // This is important!
            self.addSubview(lineView)
    
            let metrics = ["width" : NSNumber(value: height)]
            let views = ["lineView" : lineView]
            self.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[lineView]|", options:NSLayoutConstraint.FormatOptions(rawValue: 0), metrics:metrics, views:views))
    
            switch position {
            case .top:
                self.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[lineView(width)]", options:NSLayoutConstraint.FormatOptions(rawValue: 0), metrics:metrics, views:views))
                break
            case .bottom:
                self.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:[lineView(width)]|", options:NSLayoutConstraint.FormatOptions(rawValue: 0), metrics:metrics, views:views))
                break
            }
        }
    }
    

    How to use?

    // UILabel
    self.lblDescription.addLine(position: .bottom, color: UIColor.blue, height: 1.0)
    


    and

    // UITextField
    self.txtArea.addLine(position: .bottom, color: UIColor.red, height: 1.0)
    

    0 讨论(0)
  • 2020-11-27 10:02

    SwiftUI

    in SwiftUI, there is a View called Divider that is perfectly matched for this. You can add it below any view by embedding them into a simple VStack:

    VStack {
        Text("This could be any View")
        Divider()
    }
    
    0 讨论(0)
  • 2020-11-27 10:04

    Objective C

            [txt.layer setBackgroundColor: [[UIColor whiteColor] CGColor]];
            [txt.layer setBorderColor: [[UIColor grayColor] CGColor]];
            [txt.layer setBorderWidth: 0.0];
            [txt.layer setCornerRadius:12.0f];
            [txt.layer setMasksToBounds:NO];
            [txt.layer setShadowRadius:2.0f];
            txt.layer.shadowColor = [[UIColor blackColor] CGColor];
            txt.layer.shadowOffset = CGSizeMake(1.0f, 1.0f);
            txt.layer.shadowOpacity = 1.0f;
            txt.layer.shadowRadius = 1.0f;
    

    Swift

            txt.layer.backgroundColor = UIColor.white.cgColor
            txt.layer.borderColor = UIColor.gray.cgColor
            txt.layer.borderWidth = 0.0
            txt.layer.cornerRadius = 5
            txt.layer.masksToBounds = false
            txt.layer.shadowRadius = 2.0
            txt.layer.shadowColor = UIColor.black.cgColor
            txt.layer.shadowOffset = CGSize.init(width: 1.0, height: 1.0)
            txt.layer.shadowOpacity = 1.0
            txt.layer.shadowRadius = 1.0
    
    0 讨论(0)
  • 2020-11-27 10:06

    You can create a subclass of UITextField as shown below:

    class TextField : UITextField {
    
        override var tintColor: UIColor! {
    
            didSet {
                setNeedsDisplay()
            }
        }
    
        override func draw(_ rect: CGRect) {
    
            let startingPoint   = CGPoint(x: rect.minX, y: rect.maxY)
            let endingPoint     = CGPoint(x: rect.maxX, y: rect.maxY)
    
            let path = UIBezierPath()
    
            path.move(to: startingPoint)
            path.addLine(to: endingPoint)
            path.lineWidth = 2.0
    
            tintColor.setStroke()
    
            path.stroke()
        }
    }
    
    0 讨论(0)
提交回复
热议问题