首页 > UItextfield怎么做成下划线样式的?输入的文本在下划线上面?

UItextfield怎么做成下划线样式的?输入的文本在下划线上面?

像这样。


方法不止一种,可以用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)效果图:

【热门文章】
【热门文章】