繁体   English   中英

右侧的UITextField边框仅在swift中

[英]UITextField border for right side only in swift

在我的快速项目中,我只想为我的文本字段添加右侧边框,因为在单个tableviewcell中有两个文本字段,因此,我想将它们显示为第一个文本字段的右侧边框分隔。

任何建议,谢谢!

我已经将这种方法用于文本字段

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 

下面是上面代码的结果

上面代码的结果

尝试这个。

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];

看起来像这样

选择是你的,

方法1:创建IBDesignable并将其分配给类属性中的文本字段,

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
    }


}

方式2: @Dixit Akabari解释

方法3:您只需将右视图添加为文本图像作为边框图像

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM