gpt4 book ai didi

ios - 在 Swift 4 的左/右 UITextField 中添加图标或图像

转载 作者:搜寻专家 更新时间:2023-10-31 22:04:23 25 4
gpt4 key购买 nike

我已经在 UITextField 中设置了左边的图标。当我设置文本时,它位于左侧图标上方。我想在 UITextField 中的图标后设置文本。我使用了以下代码。

    let imageView = UIImageView(image: UIImage(named: strImgname))
imageView.frame = CGRect(x: 0, y: 0, width: imageView.image!.size.width , height: imageView.image!.size.height)
let paddingView: UIView = UIView.init(frame: CGRect(x: 0, y: 0, width: 50, height: 30))
paddingView.addSubview(imageView)
txtField.leftViewMode = .always
txtField.leftView = paddingView

最佳答案

您可以使用 @IBDesignable制作可设计的 UITextField具有这些甚至更多功能,并在整个项目中使用它。

DesignableTextField 在图标位于 UITextField 时具有委托(delegate)方法被点击:

import Foundation
import UIKit

protocol DesignableTextFieldDelegate: UITextFieldDelegate {
func textFieldIconClicked(btn:UIButton)
}

@IBDesignable
class DesignableTextField: UITextField {

//Delegate when image/icon is tapped.
private var myDelegate: DesignableTextFieldDelegate? {
get { return delegate as? DesignableTextFieldDelegate }
}

@objc func buttonClicked(btn: UIButton){
self.myDelegate?.textFieldIconClicked(btn: btn)
}

//Padding images on left
override func leftViewRect(forBounds bounds: CGRect) -> CGRect {
var textRect = super.leftViewRect(forBounds: bounds)
textRect.origin.x += padding
return textRect
}

//Padding images on Right
override func rightViewRect(forBounds bounds: CGRect) -> CGRect {
var textRect = super.rightViewRect(forBounds: bounds)
textRect.origin.x -= padding
return textRect
}

@IBInspectable var padding: CGFloat = 0
@IBInspectable var leadingImage: UIImage? { didSet { updateView() }}
@IBInspectable var color: UIColor = UIColor.lightGray { didSet { updateView() }}
@IBInspectable var imageColor: UIColor = UIColor.init(hex: "3EB2FF") { didSet { updateView() }}
@IBInspectable var rtl: Bool = false { didSet { updateView() }}

func updateView() {
rightViewMode = UITextFieldViewMode.never
rightView = nil
leftViewMode = UITextFieldViewMode.never
leftView = nil

if let image = leadingImage {
let button = UIButton(type: .custom)
button.frame = CGRect(x: 0, y: 0, width: 20, height: 20)

let tintedImage = image.withRenderingMode(.alwaysTemplate)
button.setImage(tintedImage, for: .normal)
button.tintColor = imageColor

button.setTitleColor(UIColor.clear, for: .normal)
button.addTarget(self, action: #selector(buttonClicked(btn:)), for: UIControlEvents.touchDown)
button.isUserInteractionEnabled = true

if rtl {
rightViewMode = UITextFieldViewMode.always
rightView = button
} else {
leftViewMode = UITextFieldViewMode.always
leftView = button
}
}

// Placeholder text color
attributedPlaceholder = NSAttributedString(string: placeholder != nil ? placeholder! : "", attributes:[NSAttributedStringKey.foregroundColor: color])
}
}

现在在 Storyboard 中的 Designable 如下:

enter image description here注意: Rtl当设置为 Off 时图标将移动到 UITextField 的左侧

在所需的 ViewController 中符合委托(delegate)。

class MyViewController: UIViewController, DesignableTextFieldDelegate {
@IBOutlet weak var txtFieldSomeSearch: DesignableTextField!
txtFieldSomeSearch.delegate = self // can be done in storyboard as well
... // other codes
func textFieldIconClicked(btn: UIButton) {
print("MyViewController : textFieldIconClicked")
}
... // other codes
}

最终输出:

enter image description here

关于ios - 在 Swift 4 的左/右 UITextField 中添加图标或图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51841320/

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