在UITextField的开头创build空格

我想在UITextField的开头留下一些空格,就像这样: 将左边距添加到UITextField

但是我不知道如何用Swift做到这一点。

这是我现在使用的:

 class TextField: UITextField { let padding = UIEdgeInsets(top: 0, left: 5, bottom: 0, right: 5); override func textRectForBounds(bounds: CGRect) -> CGRect { return UIEdgeInsetsInsetRect(bounds, padding) } override func placeholderRectForBounds(bounds: CGRect) -> CGRect { return UIEdgeInsetsInsetRect(bounds, padding) } override func editingRectForBounds(bounds: CGRect) -> CGRect { return UIEdgeInsetsInsetRect(bounds, padding) } } 

对于Swift 3:

 class TextField: UITextField { let padding = UIEdgeInsets(top: 0, left: 5, bottom: 0, right: 5); override func textRect(forBounds bounds: CGRect) -> CGRect { return UIEdgeInsetsInsetRect(bounds, padding) } override func placeholderRect(forBounds bounds: CGRect) -> CGRect { return UIEdgeInsetsInsetRect(bounds, padding) } override func editingRect(forBounds bounds: CGRect) -> CGRect { return UIEdgeInsetsInsetRect(bounds, padding) } } 

我从来没有设置其他填充,但你可以调整。 这个类不处理textfield上的rightView和leftView。 如果你想要正确处理你可以使用类似的东西(例如objc和我只需要正确的View:

 - (CGRect)textRectForBounds:(CGRect)bounds { CGRect paddedRect = UIEdgeInsetsInsetRect(bounds, self.insets); if (self.rightViewMode == UITextFieldViewModeAlways || self.rightViewMode == UITextFieldViewModeUnlessEditing) { return [self adjustRectWithWidthRightView:paddedRect]; } return paddedRect; } - (CGRect)placeholderRectForBounds:(CGRect)bounds { CGRect paddedRect = UIEdgeInsetsInsetRect(bounds, self.insets); if (self.rightViewMode == UITextFieldViewModeAlways || self.rightViewMode == UITextFieldViewModeUnlessEditing) { return [self adjustRectWithWidthRightView:paddedRect]; } return paddedRect; } - (CGRect)editingRectForBounds:(CGRect)bounds { CGRect paddedRect = UIEdgeInsetsInsetRect(bounds, self.insets); if (self.rightViewMode == UITextFieldViewModeAlways || self.rightViewMode == UITextFieldViewModeWhileEditing) { return [self adjustRectWithWidthRightView:paddedRect]; } return paddedRect; } - (CGRect)adjustRectWithWidthRightView:(CGRect)bounds { CGRect paddedRect = bounds; paddedRect.size.width -= CGRectGetWidth(self.rightView.frame); return paddedRect; } 

X,Y,Z是你想要的值

 textField.layer.sublayerTransform = CATransform3DMakeTranslation(x, y, z) 

我想避免UITextField的子类为了实现这一点,所以我能够使用一个扩展如下:

 extension UITextField { func setLeftPaddingPoints(_ amount:CGFloat){ let paddingView = UIView(frame: CGRect(x: 0, y: 0, width: amount, height: self.frame.size.height)) self.leftView = paddingView self.leftViewMode = .always } func setRightPaddingPoints(_ amount:CGFloat) { let paddingView = UIView(frame: CGRect(x: 0, y: 0, width: amount, height: self.frame.size.height)) self.rightView = paddingView self.rightViewMode = .always } } 

然后,当我需要在我的应用程序的任何位置设置文本字段的填充时,我只需执行以下操作:

  textField.setLeftPaddingPoints(10) textField.setRightPaddingPoints(10) 

希望这可以帮助!

神秘的是,这个问题已经在我的情况下解决了….让我告诉你的代码。 无论如何,我正在使用Xcode 7 + iOS 9 + iPAD Retina。

现在这里是间距的代码。 不过,我通过硬代码制作占位符。

 import UIKit class LoginClass: UIViewController { @IBOutlet weak var userNameTxtFldOutlet: UITextField! @IBOutlet weak var passwordTxtFldOutlet: UITextField! override func viewDidLoad() { super.viewDidLoad() //Making a padding object of view for first textField let paddingForFirst = UIView(frame: CGRectMake(0, 0, 15, self.userNameTxtFldOutlet.frame.size.height)) //Adding the padding to the second textField userNameTxtFldOutlet.leftView = paddingForFirst userNameTxtFldOutlet.leftViewMode = UITextFieldViewMode .Always //Making a padding object of view for second textField let paddingForSecond = UIView(frame: CGRectMake(0, 0, 15, self.passwordTxtFldOutlet.frame.height)) //Adding the padding to the second textField passwordTxtFldOutlet.leftView = paddingForSecond passwordTxtFldOutlet.leftViewMode = UITextFieldViewMode .Always self.userNameTxtFldOutlet.delegate = self self.passwordTxtFldOutlet.delegate = self userNameTxtFldOutlet.attributedPlaceholder = NSAttributedString(string: "Restaurant Name", attributes: [NSForegroundColorAttributeName: UIColor(red: 51/255, green: 57/255, blue: 67/255, alpha: 1)]) passwordTxtFldOutlet.attributedPlaceholder = NSAttributedString(string: "Password", attributes: [NSForegroundColorAttributeName: UIColor(red: 51/255, green: 57/255, blue: 67/255, alpha: 1)]) self.navigationController?.navigationBarHidden = true override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } } 

完全不用担心。 让我解释一下Hack。 我刚刚在占位符开始之前添加了几个空格

我刚刚添加/放置一个UIView对象强制文本的左侧。 所以现在打字会在视图之后开始。

谢谢

希望这有助于…

简单的SWIFT 3解决scheme – 添加代码viewDidLoad:

 let indentView = UIView(frame: CGRect(x: 0, y: 0, width: 10, height: 20)) textField.leftView = indentView textField.leftViewMode = .always 

不需要可笑的长码

在Swift 3中为UITextField创build填充视图

 func txtPaddingVw(txt:UITextField) { let paddingView = UIView(frame: CGRect(x: 0, y: 0, width: 5, height: 5)) txt.leftViewMode = .always txt.leftView = paddingView } 

子类化UITextField是要走的路。 打开一个操场并添加以下代码:

 class MyTextField : UITextField { var leftTextMargin : CGFloat = 0.0 override func textRectForBounds(bounds: CGRect) -> CGRect { var newBounds = bounds newBounds.origin.x += leftTextMargin return newBounds } override func editingRectForBounds(bounds: CGRect) -> CGRect { var newBounds = bounds newBounds.origin.x += leftTextMargin return newBounds } } let tf = MyTextField(frame: CGRect(x: 0, y: 0, width: 100, height: 44)) tf.text = "HELLO" tf.leftTextMargin = 25 tf.setNeedsLayout() tf.layoutIfNeeded() 

创build具有所需填充空间的UIView,并将其添加到textfield.leftView成员,并将textfield.leftViewMode成员设置为UITextFieldViewMode.Always

 // For example if you have textfield named title @IBOutlet weak var title: UITextField! // Create UIView let paddingView : UIView = UIView(frame: CGRectMake(0, 0, 5, 20)) //Change your required space instaed of 5. title.leftView = paddingView title.leftViewMode = UITextFieldViewMode.Always 

在Swift 3.你可以使用自定义的UITextField和在其构造函数中设置的缩进。 在控制器中不需要额外的声明。

 class CustomTextField : UITextField { private let indentView = UIView(frame: CGRect(x: 0, y:0, width: 10, height: 10)) required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) self.leftView = indentView self.leftViewMode = .always } } 

ScareCrow的答案在Swift 3中

 let padding = UIEdgeInsets(top: 0, left: 5, bottom: 0, right: 5); override func textRect(forBounds bounds: CGRect) -> CGRect { return UIEdgeInsetsInsetRect(bounds, padding) } override func placeholderRect(forBounds bounds: CGRect) -> CGRect { return UIEdgeInsetsInsetRect(bounds, padding) } override func editingRect(forBounds bounds: CGRect) -> CGRect { return UIEdgeInsetsInsetRect(bounds, padding) } 

把这个代码放在你的viewDidLoad()

 textField.delegate = self let paddingView = UIView(frame: CGRect(x: 0, y: 0, width: 20, height: self.textField.frame.height)) textField.leftView = paddingView textField.leftViewMode = UITextFieldViewMode.always 

它适用于我:)