1.把UITextField的leftView當做填充位置,實現(xiàn)文字偏移
// 創(chuàng)建一個占位的 view
UIView *leftV = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 21)];
UITextField *pwdTF = [[UITextField alloc] init];
pwdTF.leftView = leftV; // 設(shè)置 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
轉(zhuǎn)載自:https://blog.csdn.net/coslay/article/details/42266663