gpt4 book ai didi

ios - 如何禁用 uitextField.rightView 的编辑功能。

转载 作者:行者123 更新时间:2023-11-28 15:35:37 29 4
gpt4 key购买 nike

所以我在我的应用程序中实现了一个 uitextfield 并将其右 View 设置为一个按钮来触发某些东西。

但是,每当我按下 uitextfield 的 rightView 时,它也会触发应用程序开始编辑(显示键盘)。

如何在按下 uitextField 的 rightView 时禁用编辑?

谢谢。

最佳答案

使用这个自定义子类:

@IBDesignable class TJTextField: UITextField {

fileprivate var ImgIcon: UIImageView?

@IBInspectable var errorEntry: Bool = false {
didSet {
self.setNeedsDisplay()
}
}
@IBInspectable var leftTextPedding: Int = 0 {
didSet {
self.setNeedsDisplay()
}
}
@IBInspectable var lineColor: UIColor = UIColor.black {
didSet {
self.setNeedsDisplay()
}
}
@IBInspectable var placeHolerColor: UIColor = UIColor(red: 199.0/255.0, green: 199.0/255.0, blue: 205.0/255.0, alpha: 1.0) {
didSet {
self.setNeedsDisplay()
}
}
@IBInspectable var errorColor: UIColor = UIColor.red {
didSet {
self.setNeedsDisplay()
}
}

@IBInspectable var imageWidth: Int = 0 {
didSet {
self.setNeedsDisplay()
}
}
@IBInspectable var txtImage : UIImage? {
didSet {
self.setNeedsDisplay()
}
}
override func textRect(forBounds bounds: CGRect) -> CGRect {
return self.newBounds(bounds)
}
override func editingRect(forBounds bounds: CGRect) -> CGRect {
return self.newBounds(bounds)
}

fileprivate func newBounds(_ bounds: CGRect) -> CGRect {

var newBounds = bounds
newBounds.origin.x += CGFloat(leftTextPedding) + CGFloat(imageWidth)
return newBounds
}

var errorMessage: String?

override func awakeFromNib() {
super.awakeFromNib()
//setting left image
if (txtImage != nil)
{
let btnLeft = UIButton()

btnLeft.frame = CGRect(x: 0, y: 0, width: CGFloat(imageWidth), height: self.frame.height)
btnLeft.setImage(txtImage, for: .normal)
self.leftViewMode = UITextFieldViewMode.always
self.leftView = btnLeft
btnLeft.addTarget(self, action: #selector(TJTextField.btnClicked), for: .touchUpInside)
}
}
func btnClicked()
{
if !self.isEditing
{
self.resignFirstResponder()
}

}
override func draw(_ rect: CGRect)
{
let height = self.bounds.height

// get the current drawing context
let context = UIGraphicsGetCurrentContext()

// set the line color and width
if errorEntry {
context?.setStrokeColor(errorColor.cgColor)
context?.setLineWidth(1.5)
} else {
context?.setStrokeColor(lineColor.cgColor)
context?.setLineWidth(0.5)
}

// start a new Path
context?.beginPath()

context!.move(to: CGPoint(x: self.bounds.origin.x, y: height - 0.5))
context!.addLine(to: CGPoint(x: self.bounds.size.width, y: height - 0.5))
// close and stroke (draw) it
context?.closePath()
context?.strokePath()

//Setting custom placeholder color
if let strPlaceHolder: String = self.placeholder
{
self.attributedPlaceholder = NSAttributedString(string:strPlaceHolder,
attributes:[NSForegroundColorAttributeName:placeHolerColor])
}
}
override func leftViewRect(forBounds bounds: CGRect) -> CGRect
{
return CGRect(x: 0, y: 0, width: CGFloat(imageWidth), height: self.frame.height)
}
}

关于ios - 如何禁用 uitextField.rightView 的编辑功能。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44326631/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com