self.testTextField.leftView = [[UIView alloc] initWithFrame:CGRectMake(, , , )];
self.testTextField.leftViewMode = UITextFieldViewModeAlways;
这两机代码可以是textField的光标向右偏移40;
============================
项目中需要在UITextField在删除时候的做些事情,本身又没有方法。所以需要自己写
KeyInPutTextField.h
#import <UIKit/UIKit.h> @protocol KeyInPutTextFieldDelegate<NSObject> @optional
- (void)deleteBackward; @end @interface KeyInPutTextField : UITextField @property (nonatomic, assign)id<KeyInPutTextFieldDelegate>keyInputDelegate; @end
KeyInPutTextField.m
#import "KeyInPutTextField.h" @implementation KeyInPutTextField - (void)deleteBackward
{
[super deleteBackward];
if ((_keyInputDelegate &&[_keyInputDelegate respondsToSelector:@selector(deleteBackward)]))
{
[_keyInputDelegate deleteBackward];
}
} @end
调用
#import "ViewController.h"
#import "KeyInPutTextField.h"
@interface ViewController ()<UITextFieldDelegate, KeyInPutTextFieldDelegate> @property (nonatomic, strong) KeyInPutTextField *testTextField; @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
self.testTextField = [[KeyInPutTextField alloc]initWithFrame:CGRectMake(, , , )];
self.testTextField.borderStyle = UITextBorderStyleRoundedRect;
self.testTextField.textColor = [UIColor redColor];
self.testTextField.delegate = self;
self.testTextField.keyInputDelegate = self;
[self.view addSubview:self.testTextField];
[self.testTextField becomeFirstResponder]; self.testTextField.rightViewMode = UITextFieldViewModeAlways; self.testTextField.leftView = [[UIView alloc] initWithFrame:CGRectMake(, , , )];
self.testTextField.leftViewMode = UITextFieldViewModeAlways;
} - (void)deleteBackward
{
NSLog(@"aa");
}