1.把UITextField的leftView当做填充位置,实现文字偏移
// 创建一个占位的 view
UIView *leftV = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 21)];
UITextField *pwdTF = [[UITextField alloc] init];
pwdTF.leftView = leftV; // 设置 TF 的 leftView 为 leftV
pwdTF.leftViewMode = UITextFieldViewModeAlways;
[self.view addSubview:pwdTF];
//UITextFieldViewModeNever 不展示
//UITextFieldViewModeWhileEditing 编辑时展示
//UITextFieldViewModeUnlessEditing 编辑时不展示
//UITextFieldViewModeAlways 一直展示
2.继承UITextfield,覆盖父类方法!
#import <UIKit/UIKit.h>
@interface InsetsTextField : UITextField
- (CGRect)textRectForBounds:(CGRect)bounds;
- (CGRect)editingRectForBounds:(CGRect)bounds;
@end
#import "InsetsTextField.h"
@implementation InsetsTextField
//控制文本所在的的位置,左右缩 10
- (CGRect)textRectForBounds:(CGRect)bounds {
return CGRectInset( bounds , 10 , 0 );
}
//控制编辑文本时所在的位置,左右缩 10
- (CGRect)editingRectForBounds:(CGRect)bounds {
return CGRectInset( bounds , 10 , 0 );
}
@end
网友评论