UITextField內(nèi)容縮進

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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容