iOS UITextfield怎么做成下划线样式的?

继承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];
}

效果图


WX20200512-155002@2x.png

好了!祝大家代码越来越优秀

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。