textField

// - textField 设置背景图

//    textFied.borderStyle = UITextBorderStyleRoundedRect; 不可以设置这个 否则不会显示背景图

UIImage * textIcon = [UIImageimageNamed:@"chat_sender_bg_on"];

    textIcon =   [textIcon stretchableImageWithLeftCapWidth:image.size.width *0.1topCapHeight:image.size.height *0.1];

    [textField setBackground:textIcon];


// -   textfield 设置字数控制

[textFied addTarget:selfaction:@selector(onValueChanged:)forControlEvents:UIControlEventEditingChanged];

-(void)onValueChanged:(UITextField *)textFidld{

    if (textFidld.text.length >10) {

        

        textFidld.text = [textFidld.textsubstringWithRange:NSMakeRange(0,10)];

        

    }

}




#pragma mark - touchesBegan
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];// - 只要是当前view的子控件或者是子控件的子控件都可以退出键盘

}



textField 设置 palaceHolder的颜色

- (BOOL)becomeFirstResponder
{
    // 修改占位文字颜色
    [self setValue:self.textColor forKeyPath:PlacerholderColorKeyPath];
    return [super becomeFirstResponder];
}

/** * 当前文本框失去焦点时就会调用 (光标不在它身上时调用) */
- (BOOL)resignFirstResponder
{
    // 修改占位文字颜色
    [self setValue:[UIColor grayColor] forKeyPath:PlacerholderColorKeyPath];
    return [super resignFirstResponder];
}

你可能感兴趣的:(textField)