ios 在 xcode 中使用 swift 设置文本字段的填充
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31814300/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
set the padding of a text field using swift in xcode
提问by sekaisan
How to create some space at the beginning of a text field using swift? I looked at the post
如何使用swift在文本字段的开头创建一些空间?我看了帖子
I don't understand what the subclass is doing and how do I use that class to padding.
我不明白子类在做什么以及如何使用该类进行填充。
Thank you very much
非常感谢
回答by Klevison
https://stackoverflow.com/a/27066764/846780
https://stackoverflow.com/a/27066764/846780
This answer is very clear,
这个答案很明确
If you subclass
UITextField
you can overridetextRectForBounds
,editingRectForBounds
andplaceholderRectForBounds
. These methods just allow you to add a rect (frame) to your textfield's label.newBounds
method create the rect (frame) that will be added to textfield's label.Finally your padding is:
let padding = UIEdgeInsets(top: 0, left: 5, bottom: 0, right: 5);
Now you have a custom UITextField that has a custom padding.
If you create your new subClass like this
class MyTextField: UITextField
for example, you only need to change the class of the UITextField that you've added into IB file.
如果你是子类,
UITextField
你可以覆盖textRectForBounds
,editingRectForBounds
和placeholderRectForBounds
。这些方法只允许您向文本字段的标签添加一个矩形(框架)。newBounds
方法创建将添加到文本字段标签的矩形(框架)。最后你的填充是:
let padding = UIEdgeInsets(top: 0, left: 5, bottom: 0, right: 5);
现在你有一个自定义的 UITextField,它有一个自定义的填充。
class MyTextField: UITextField
例如,如果您像这样创建新的子类,则只需更改已添加到 IB 文件中的 UITextField 的类。
回答by Jorge Casariego
Complementing the answer of klevison-matiasthis is the code I use when I want to add a padding to my TextField in Swift 3
补充klevison-matias的答案,这是我想在 Swift 3 中向 TextField 添加填充时使用的代码
//UITextField : override textRect, editingRect
class LeftPaddedTextField: UITextField {
override func textRect(forBounds bounds: CGRect) -> CGRect {
return CGRect(x: bounds.origin.x + 10, y: bounds.origin.y, width: bounds.width, height: bounds.height)
}
override func editingRect(forBounds bounds: CGRect) -> CGRect {
return CGRect(x: bounds.origin.x + 10, y: bounds.origin.y, width: bounds.width, height: bounds.height)
}
}
Then in my TextField I use in this way:
然后在我的 TextField 中,我以这种方式使用:
let emailTextField: LeftPaddedTextField = {
let textField = LeftPaddedTextField()
textField.placeholder = "Enter email"
textField.layer.borderColor = UIColor.lightGray.cgColor
textField.layer.borderWidth = 1
textField.keyboardType = .emailAddress
return textField
}()
let passwordTextField: LeftPaddedTextField = {
let textField = LeftPaddedTextField()
textField.placeholder = "Enter password"
textField.layer.borderColor = UIColor.lightGray.cgColor
textField.layer.borderWidth = 1
textField.isSecureTextEntry = true
return textField
}()
回答by Avijit Nagare
Create TextField outlet and use following code. Say "nameTextField" has to add padding.
创建 TextField 插座并使用以下代码。说“nameTextField”必须添加填充。
let paddingView = UIView(frame: CGRectMake(x: 0, y: 0, width: 30, height: 30))
nameTextField.leftView = paddingView
nameTextField.leftViewMode = .always
nameTextField.placeholder = "Enter Name"
You can add Image in paddingView.
您可以在 paddingView 中添加图像。
回答by nickgzzjr
Based on @Jorge Casariego's answer I came up with this solution.
根据@Jorge Casariego 的回答,我想出了这个解决方案。
You can set PaddedTextField
class and the desired padding through the Interface Builder!
您可以PaddedTextField
通过 Interface Builder设置类和所需的填充!
class PaddedTextField: UITextField {
@IBInspectable var padding: CGFloat = 0
override func textRect(forBounds bounds: CGRect) -> CGRect {
return CGRect(x: bounds.origin.x + padding, y: bounds.origin.y, width: bounds.width - padding * 2, height: bounds.height)
}
override func editingRect(forBounds bounds: CGRect) -> CGRect {
return CGRect(x: bounds.origin.x + padding, y: bounds.origin.y, width: bounds.width - padding * 2, height: bounds.height)
}
}
回答by MrG
full code sample: make sure your TextField points to RoundedTextField via Story board
完整代码示例:确保您的 TextField 通过故事板指向 RoundedTextField
import UIKit
class RoundedTextField: UITextField {
override func awakeFromNib() {
// add rounded corner
self.layer.cornerRadius = 15.0
self.clipsToBounds = false
super.awakeFromNib()
}
// For the padding from the left
override func textRect(forBounds bounds: CGRect) -> CGRect {
return CGRect(x: bounds.origin.x + 15.0, y: bounds.origin.y, width: bounds.width, height: bounds.height)
}
override func editingRect(forBounds bounds: CGRect) -> CGRect {
return CGRect(x: bounds.origin.x + 15.0, y: bounds.origin.y, width: bounds.width, height: bounds.height)
}
}