UITextFiled的输入框改成一条下划线

在一些程序的界面中,它们的编辑框是一条线,而UITextFiled本身并没有这种style,所有需要我们自己设置.方法还是挺多的

第一种 ,

(1).我们可以声明一个类继承与UITextFiled

(2).需要重写父类的- (void)drawRect:(CGRect)rect方法

[objc] view plain copy
 
  1. <span style="font-size:18px;">- (void)drawRect:(CGRect)rect {  
  2.     // Drawing code  
  3.     CGContextRef context = UIGraphicsGetCurrentContext();  
  4.     CGContextSetFillColorWithColor(context, [UIColor blackColor].CGColor);  
  5.     CGContextFillRect(context, CGRectMake(0, CGRectGetHeight(self.frame) - 0.5, CGRectGetWidth(self.frame), 0.5));  
  6. }</span>  
(3)下面再创建uite对象的时候用我们自己声明的这个类来创建,编辑框就会是一条线了

第二种,

我们可以创建一个UIView对象,将它的高设为1,贴到UITextFiled上就可以了(view的frame要根据自己的TextField的from来确定)

UIView *underline = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 1)];

    underline.backgroundColor = [UIColor lightGrayColor];

    [self addSubview:underline];

    [underline release];

 
 

 
 
原文地址:https://www.cnblogs.com/xujiahui/p/6554824.html