1 个回答

方法不止一种,可以用category,可以直接骗用户,可以...这里说一种继承,步骤如下:
1)继承UITextfield
2)重写子类中的- (void)drawRect:(CGRect)rect 如下

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

3)TestCode 如下:

#import "CustomField.h"
@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    CustomField *textField = [CustomField new];
    textField.frame = CGRectMake(0, 200, 100, 30);
    textField.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:textField];
}

4)效果图:
图片描述

评论 赞赏
    撰写回答

    登录后参与交流、获取后续更新提醒