我想插入一个UITextField
的文本 。 框架
这可能吗? atom
使用textRectForBounds:
是正确的方法。 我将其包装在子类中,所以您能够简单地使用textEdgeInsets
。 参见SSTextField 。 spa
我可以经过如下方式作到这一点: code
myTextField.layer.sublayerTransform = CATransform3DMakeTranslation(5, 0, 0);
固然,请记住导入QuartzCore并将框架添加到您的项目中。 orm
若是只须要左边距,则能够尝试如下操做: get
UItextField *textField = [[UITextField alloc] initWithFrame:...]; UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, textField.frame.size.height)]; leftView.backgroundColor = textField.backgroundColor; textField.leftView = leftView; textField.leftViewMode = UITextFieldViewModeAlways;
这个对我有用。 但愿对您有所帮助。 it
若是只想更改TOP和LEFT缩进,则 io
//占位符位置 form
- (CGRect)textRectForBounds:(CGRect)bounds { CGRect frame = bounds; frame.origin.y = 3; frame.origin.x = 5; bounds = frame; return CGRectInset( bounds , 0 , 0 ); }
//文字位置 import
- (CGRect)editingRectForBounds:(CGRect)bounds { CGRect frame = bounds; frame.origin.y = 3; frame.origin.x = 5; bounds = frame; return CGRectInset( bounds , 0 , 0 ); }
向UITextField添加填充的一种好方法是子类化UITextField并添加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