自定义UITextField,支持placeholderFont、placeholderColor、offset、isEnableDecimalLimit
- 使用方法
self.textField = [[NBTextField alloc]initWithFrame:CGRectMake(0, 0, 400, 60)];
[self.view addSubview:self.textField];
self.textField.placeholder = @"请输入内容...";
self.textField.offset = CGPointMake(20, 10);
self.textField.placeholderColor = UIColor.lightGrayColor;
self.textField.placeholderFont = [UIFont systemFontOfSize:20.0];
UIView *leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 20, 20)];
leftView.backgroundColor = UIColor.orangeColor;
self.textField.leftView = leftView;
self.textField.leftViewMode = UITextFieldViewModeAlways;
self.textField.center = self.view.center;
self.textField.backgroundColor = [UIColor colorWithHue:0.0 saturation:0.0 brightness:0.0 alpha:0.02];
- 源码分享
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface NBTextField : UITextField
/// 占位字体
@property (nonatomic, strong) UIFont *placeholderFont;
/// 占位字颜色
@property (nonatomic, strong) UIColor *placeholderColor;
/// 内边距偏移量
@property (nonatomic, assign) CGPoint offset;
/// 是否开启输入小数点后,仅支持两位数
@property (nonatomic, assign) BOOL isEnableDecimalLimit;
@end
NS_ASSUME_NONNULL_END
#import "NBTextField.h"
#import <objc/runtime.h>
@interface NBTextField() <UITextFieldDelegate>
@property (nonatomic, strong) UILabel *placeholderLabel;
@end
@implementation NBTextField
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
self.delegate = self;
self.placeholderColor = UIColor.lightGrayColor;
self.placeholderFont = [UIFont systemFontOfSize:17.0];
}
return self;
}
- (UILabel *)placeholderLabel {
if (!_placeholderLabel) {
Ivar ivar = class_getInstanceVariable(UITextField.class, "_placeholderLabel");
_placeholderLabel = object_getIvar(self, ivar);
}
return _placeholderLabel;
}
- (void)setPlaceholderFont:(UIFont *)placeholderFont {
self.placeholderLabel.font = placeholderFont;
}
- (void)setPlaceholderColor:(UIColor *)placeholderColor {
self.placeholderLabel.textColor = placeholderColor;
}
- (CGRect)rectForBounds:(CGRect)bounds {
CGRect rect = bounds;
CGFloat leftViewWidth = 0.0f;
if (self.leftView) {
leftViewWidth = self.leftView.bounds.size.width;
}
// 调整X坐标和宽度
rect.origin.x += leftViewWidth + self.offset.x;
rect.size.width -= leftViewWidth + fabs(self.offset.x);
if (self.offset.x < 0) {
rect.size.width = MIN(rect.size.width, bounds.size.width - leftViewWidth);
}
// 调整Y坐标
rect.origin.y += self.offset.y;
if (self.offset.y < 0) {
rect.origin.y = MIN(rect.origin.y, bounds.origin.y);
}
return rect;
}
- (CGRect)placeholderRectForBounds:(CGRect)bounds {
return [self rectForBounds:bounds];
}
- (CGRect)textRectForBounds:(CGRect)bounds {
return [self rectForBounds:bounds];
}
- (CGRect)editingRectForBounds:(CGRect)bounds {
return [self rectForBounds:bounds];
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if (!self.isEnableDecimalLimit) {
return YES;
}
NSString *currentText = [textField.text stringByReplacingCharactersInRange:range withString:string] ? : @"";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", @"^$|^0$|^0\\.[0-9]{0,2}$|^[1-9][0-9]*\\.?[0-9]{0,2}$"];
BOOL isValid = [predicate evaluateWithObject:currentText];
return isValid;
}
@end