UITextField的相关设置

iOS设置TextField的placeholder的颜色,位置,字体,光标颜色

  • 如果只是单纯的想修改一下占位文字的的颜色字体

    • 可以设置attributedPlaceholder属性

      UITextField *textField = [UITextField new];
      NSString *placeholderString = @"占位文字";
      NSMutableAttributedString *placeholder = [[NSMutableAttributedString alloc] initWithString: placeholderString];
      //占位文字字体颜色
      [placeholder addAttribute:NSForegroundColorAttributeName
                      value:[UIColor greenColor]
                      range:NSMakeRange(0, placeholderString.length)];
      //占位文字字体大小                 
      [placeholder addAttribute:NSFontAttributeName
                      value:[UIFont boldSystemFontOfSize:15]
                      range:NSMakeRange(0, placeholderString.length)];
      textField.attributedPlaceholder = placeholder;
      
    • 通过KVC访问内部变量直接访问

      textField.placeholder = @"请输入你的QQ号"; 
      [textField setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor"]; 
      [textField setValue:[UIFont boldSystemFontOfSize:16] forKeyPath:@"_placeholderLabel.font"];
      
    • 可以通过以上两种方式直接改变textField的占位文字的颜色和字体

  • 改变占位文字的位置,光标的位置,文本显示的位置,这个需要我们自定义一个textField,来重写它里面的一些方法,具体的可以根据自己的需要来定制

    • 自定义textField的.h文件

      #import <UIKit/UIKit.h>
      #import "UIColor+YS.h"
      @interface LYTextField : UITextField
      
      @end
      
    • 自定义textField的.m文件

      #import "LYTextField.h"
      
      @implementation LYTextField
      
      //通过代码创建
      - (instancetype)initWithFrame:(CGRect)frame {
      
          self = [super initWithFrame:frame];
      
          if (self) {
          
              [self setUpUI];
          }
          
          return self;
      }
      
      //布局子控件
      - (void)setUpUI{
          
          //设置border
          self.layer.masksToBounds = YES;
          [self.layer setCornerRadius:14];
          self.layer.borderWidth = 1;
          [self.layer setBorderColor:HEX(@"#D8D8D8").CGColor];
          
          //字体大小
          self.font = [UIFont fontWithName:@"PingFangSC-Regular" size:12];
          //字体颜色
          self.textColor = kAppColor33;
          //光标颜色
          self.tintColor= self.textColor;
          
          //占位符的颜色和大小
          [self setValue:kAppColor99 forKeyPath:@"_placeholderLabel.textColor"];
          [self setValue:[UIFont fontWithName:@"PingFangSC-Regular" size:12] forKeyPath:@"_placeholderLabel.font"];
          
      }
      
      //控制placeHolder的位置 上边界4 左14
      -(CGRect)placeholderRectForBounds:(CGRect)bounds{
          
          CGRect inset = CGRectMake(bounds.origin.x + 14, bounds.origin.y + 4, bounds.size.width -14, bounds.size.height - 5);
          
          return inset;
      }
          
      //控制显示文本的位置
      -(CGRect)textRectForBounds:(CGRect)bounds{
          
          CGRect inset = CGRectMake(bounds.origin.x + 14, bounds.origin.y + 4, bounds.size.width - 14, bounds.size.height - 5);
          return inset;
      }
      
      //控制编辑文本的位置(光标显示的位置)
      -(CGRect)editingRectForBounds:(CGRect)bounds {
          
          CGRect inset = CGRectMake(bounds.origin.x + 14, bounds.origin.y + 4, bounds.size.width -14, bounds.size.height - 5);
          return inset;
      }
      
      
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容