继承UITextfield
重写子类中的- (void)drawRect:(CGRect)rect绘制一个下划线
// 重写drawRect
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [UIColor blackColor].CGColor);
CGContextFillRect(context, CGRectMake(0, CGRectGetHeight(self.frame) - 0.5, CGRectGetWidth(self.frame), 0.5));
}
使用方法
- (void)viewDidLoad {
[super viewDidLoad];
CustomField *textField = [CustomField new];
textField.frame = CGRectMake(100, 200, 100, 30);
textField.backgroundColor = [UIColor yellowColor];
[self.view addSubview:textField];
}
效果图
好了!祝大家代码越来越优秀