缩进UITextField中的文本

我的问题和标题一样简单,但是还有一点:

我有一个UITextField,我已经设置了背景图片。 问题在于文字如此紧密地贴着它的边缘,这也是图像的边缘。 我希望我的文本字段看起来像苹果的,在背景图像和文本开始位置之间有一些水平的空间。

这是我没有做任何子类的最快捷的方法:

UIView *spacerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 10)]; [textfield setLeftViewMode:UITextFieldViewModeAlways]; [textfield setLeftView:spacerView]; 

在Swift中:

 let spacerView = UIView(frame:CGRect(x:0, y:0, width:10, height:10)) textField.leftViewMode = UITextFieldViewMode.Always textField.leftView = spacerView 

你必须inheritance和重写textRectForBounds:和editingRectForBounds :. 这是一个UITextfield子类,具有自定义背景和垂直和水平填充:

 @interface MyUITextField : UITextField @property (nonatomic, assign) float verticalPadding; @property (nonatomic, assign) float horizontalPadding; @end #import "MyUITextField.h" @implementation MyUITextField @synthesize horizontalPadding, verticalPadding; - (CGRect)textRectForBounds:(CGRect)bounds { return CGRectMake(bounds.origin.x + horizontalPadding, bounds.origin.y + verticalPadding, bounds.size.width - horizontalPadding*2, bounds.size.height - verticalPadding*2); } - (CGRect)editingRectForBounds:(CGRect)bounds { return [self textRectForBounds:bounds]; } @end 

用法:

 UIImage *bg = [UIImage imageNamed:@"textfield.png"]; CGRect rect = CGRectMake(0, 0, bg.size.width, bg.size.height); MyUITextField *textfield = [[[MyUITextField alloc] initWithFrame:rect] autorelease]; textfield.verticalPadding = 10; textfield.horizontalPadding = 10; [textfield setBackground:bg]; [textfield setBorderStyle:UITextBorderStyleNone]; [self.view addSubview:textfield]; 

将填充添加到UITextField的好方法是inheritanceUITextField,重写矩形方法并添加edgeInsets属性。 然后你可以设置edgeInsets和UITextField将相应的绘制。 这也将正确使用自定义leftView或rightView设置。

OSTextField.h

 #import <UIKit/UIKit.h> @interface OSTextField : UITextField @property (nonatomic, assign) UIEdgeInsets edgeInsets; @end 

OSTextField.m

 #import "OSTextField.h" @implementation OSTextField - (id)initWithFrame:(CGRect)frame{ self = [super initWithFrame:frame]; if (self) { self.edgeInsets = UIEdgeInsetsMake(0, 0, 0, 0); } return self; } -(id)initWithCoder:(NSCoder *)aDecoder{ self = [super initWithCoder:aDecoder]; if(self){ self.edgeInsets = UIEdgeInsetsMake(0, 0, 0, 0); } return self; } - (CGRect)textRectForBounds:(CGRect)bounds { return [super textRectForBounds:UIEdgeInsetsInsetRect(bounds, self.edgeInsets)]; } - (CGRect)editingRectForBounds:(CGRect)bounds { return [super editingRectForBounds:UIEdgeInsetsInsetRect(bounds, self.edgeInsets)]; } @end 

我把它变成了一个可以用在Storyboard中的漂亮的小扩展:

 public extension UITextField { @IBInspectable public var leftSpacer:CGFloat { get { if let l = leftView { return l.frame.size.width } else { return 0 } } set { leftViewMode = .Always leftView = UIView(frame: CGRect(x: 0, y: 0, width: newValue, height: frame.size.height)) } } } 

我build议将您的UITextField转换为UITextView并设置contentInset 。 例如要缩进10个空格:

 textview.contentInset=UIEdgeInsetsMake(0, 10, 0, 0); 

我的2美分:

 class PaddedTextField : UITextField { var insets = UIEdgeInsets.zero var verticalPadding:CGFloat = 0 var horizontalPadding:CGFloat = 0 override func textRect(forBounds bounds: CGRect) -> CGRect { return CGRect(x: bounds.origin.x + insets.left, y: bounds.origin.y + insets.top, width: bounds.size.width - (insets.left + insets.right), height: bounds.size.height - (insets.top + insets.bottom)); } override func editingRect(forBounds bounds: CGRect) -> CGRect { return textRect(forBounds: bounds) } } 

有时textField的leftView是Apple的放大镜图像。 如果是这样,你可以像这样设置缩进:

  UIView *leftView = textField.leftView; if (leftView && [leftView isKindOfClass:[UIImageView class]]) { leftView.contentMode = UIViewContentModeCenter; leftView.width = 20.0f; //the space you want indented. } 

如果你不想创build@ IBOutlet的可以简单的子类(在Swift中回答):

 class PaddedTextField: UITextField { override func awakeFromNib() { super.awakeFromNib() let spacerView = UIView(frame:CGRect(x: 0, y: 0, width: 10, height: 10)) leftViewMode = .always leftView = spacerView } }