自定义浮动文本字段 - 快速
当我开始输入标签时,我想创建一个自定义文本字段,应浮出水面。我知道有一些图书馆可以做到这一点,但我想自己做。我找到了一些有关此博客的博客,但通常是在ViewController中进行的,但是我会在许多页面中使用此格式,因此我认为我应该创建一个自定义文本字段并在需要的地方使用它。我尝试在自定义文本字段中使用代码,但没有发生。
有在ViewController中起作用的代码:
import UIKit
class ViewController: UIViewController, UITextFieldDelegate {
@IBOutlet weak var textField: UITextField!
@IBOutlet weak var textLabel: UILabel!
@IBOutlet weak var textLabelTop: NSLayoutConstraint!
@IBOutlet weak var textLabelLeading: NSLayoutConstraint!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
textField.delegate = self
// Create a padding view for padding on left
let indentView = UIView(frame: CGRect(x: 0, y: 10, width: 10, height: 20))
textField.leftView = indentView
textField.leftViewMode = .always
// Create a padding view for padding on right
textField.rightView = UIView(frame: CGRect(x: 0, y: 10, width: 15, height: textField.frame.height))
}
func textFieldDidBeginEditing(_ textField: UITextField) {
floatTitle()
performAnimation(transform: CGAffineTransform(scaleX: 1, y: 1))
}
// This is where we adjust constraint and the label will float to the top
func floatTitle() {
textLabel.font = textLabel.font?.withSize(8)
textLabelTop.constant = 7
textLabelLeading.constant = 7
}
// By adding a little animation
private func performAnimation(transform: CGAffineTransform) {
UIView.animate(withDuration: 0.5, delay: 0, options: .curveEaseOut, animations: {
self.textLabel.transform = transform
self.view.layoutIfNeeded()
}, completion: nil)
}
}
如何实现此代码到自定义文本字段?
I want to create a custom text field when I started to input label should float to top-left. I know there is some libraries to make that but I want to do it myself. I found some blog about that but these are generally make that in viewController but I'll use this format in many pages so I think I should create a custom text field and use this where I need. I tried to use codes in my custom text field but nothings happen.
There is codes which working in viewController :
import UIKit
class ViewController: UIViewController, UITextFieldDelegate {
@IBOutlet weak var textField: UITextField!
@IBOutlet weak var textLabel: UILabel!
@IBOutlet weak var textLabelTop: NSLayoutConstraint!
@IBOutlet weak var textLabelLeading: NSLayoutConstraint!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
textField.delegate = self
// Create a padding view for padding on left
let indentView = UIView(frame: CGRect(x: 0, y: 10, width: 10, height: 20))
textField.leftView = indentView
textField.leftViewMode = .always
// Create a padding view for padding on right
textField.rightView = UIView(frame: CGRect(x: 0, y: 10, width: 15, height: textField.frame.height))
}
func textFieldDidBeginEditing(_ textField: UITextField) {
floatTitle()
performAnimation(transform: CGAffineTransform(scaleX: 1, y: 1))
}
// This is where we adjust constraint and the label will float to the top
func floatTitle() {
textLabel.font = textLabel.font?.withSize(8)
textLabelTop.constant = 7
textLabelLeading.constant = 7
}
// By adding a little animation
private func performAnimation(transform: CGAffineTransform) {
UIView.animate(withDuration: 0.5, delay: 0, options: .curveEaseOut, animations: {
self.textLabel.transform = transform
self.view.layoutIfNeeded()
}, completion: nil)
}
}
How can I implement this codes to custom text field ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我找到了中等博客,并将其用于我的项目。
https://medium.com/sprinthub/creating-a-custom-floation-label-style-style-text-field-in-swift-in-swift-f9f6aeaf39fe
I found that medium blog and use that for my project.
https://medium.com/sprinthub/creating-a-custom-floating-label-style-text-field-in-swift-f9f6aeaf39fe