美文网首页
UITextField 的简单用法

UITextField 的简单用法

作者: 有梦想的程序员 | 来源:发表于2016-06-13 17:27 被阅读30次
    UITextField *_tfInput = [[UITextField alloc]init];
    _tfInput.tintColor = [UIColor whiteColor];
    _tfInput.clearButtonMode=UITextFieldViewModeWhileEditing;
    _tfInput.textColor = [UIColor whiteColor];
    [_tfInput setKeyboardType:UIKeyboardTypeDefault];
    //设置Placeholder 颜色 内容 字体大小
     _tfInput.attributedPlaceholder = [[NSAttributedString alloc]initWithString:@"输入约跑主题" attributes:@{NSForegroundColorAttributeName:[�UIColor whiteColor],NSFontAttributeName:[UIFont systemFontOfSize:14.f]}];
    [self.contentView addSubview:_tfInput];
    

    补充:更改UITextField右边按钮的图片

    MBTextField 继承 UITextField

    @implementation MBTextField
    
    - (instancetype)init
    {
        self = [super init];
        if (self) {
    //        [self setBackgroundColor:[UIColor redColor]];
            [self setTextColor:[UISystemColor getSystemWhite]];
            [self setTintColor:[UIColor whiteColor]];
            [self setKeyboardType:UIKeyboardTypeNumberPad];
            [self setClearButtonMode:UITextFieldViewModeWhileEditing];        
        }
        return self;
    }
    
    - (void)layoutSubviews{
        [super layoutSubviews];
        [self setClearButtonColor];
    }
    
    - (UIButton *)getClearButton{
        for(UIView *v in self.subviews)
        {
            if([v isKindOfClass:[UIButton class]])
            {
                UIButton *clearButton = (UIButton *) v;
                return clearButton;
            }
        }
        return nil;
    }
    
    - (void)setClearButtonColor{
        UIButton *clearButton = [self getClearButton];
        if(clearButton){
            [clearButton setImage:[UIImage imageNamed:@"login_del"] forState:UIControlStateNormal];
        }
    }
    

    相关文章

      网友评论

          本文标题:UITextField 的简单用法

          本文链接:https://www.haomeiwen.com/subject/dbvydttx.html