想要实现的效果:
实现代码如下
#import "STTextField.h"
@interface STTextField : UITextField
@end
#import "STTextField.h"
@implementation STTextField
- (CGRect)placeholderRectForBounds:(CGRect)bounds {
CGRect inset = CGRectMake(bounds.origin.x, bounds.origin.y, bounds.size.width, bounds.size.height);
return inset;
}
- (CGRect)textRectForBounds:(CGRect)bounds {
CGRect inset = CGRectMake(bounds.origin.x, bounds.origin.y, bounds.size.width, bounds.size.height);
return inset;
}
- (CGRect)editingRectForBounds:(CGRect)bounds {
if (self.text.length > 0) {
return [super editingRectForBounds:bounds];
} else {
// 可通过默认文案来调整inset.origin.x保证光标在两个字之间
CGRect inset = CGRectMake(bounds.origin.x + bounds.size.width / 2, bounds.origin.y, bounds.size.width - bounds.size.width / 2, bounds.size.height);
return inset;
}
}