开发者社区> 问答> 正文

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

screenshot

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

展开
收起
a123456678 2016-07-29 09:31:59 2704 0
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];

      }

    2019-07-17 20:01:30
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载