UITextField光标在无输入的情况下默认居中

效果图如下:

效果图
#import "ZRTradeTextField.h"

@interface ZRTradeTextField : UITextField

@end

#import "ZRTradeTextField.h"

@implementation ZRTradeTextField

- (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;
    }
}


你可能感兴趣的:(UITextField光标在无输入的情况下默认居中)