iOS--textfield的拓展

岳城
2023-12-01

1.一句话改变textField的PlaceHolder颜色和字体大小

textField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:placeholder attributes:@{NSForegroundColorAttributeName: [UIColor blueColor],NSFontAttributeName:[UIFont systemFontOfSize:20]}];

2.控制输入长度

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSMutableString* str = [NSMutableString stringWithString:textField.text];

    //判断是输入内容,还是del键删除内容。
    if ([string length] > 0) {
        [str insertString:string atIndex:range.location];
    }
    else{
        [str replaceCharactersInRange:range withString:@""];
    }

    if ([textField isEqual:_verifyCodeField]) {
        if ([str length] > verifyCodeLength) {
            return NO;
        }
    }

    return YES;
}
 类似资料: