IOS UITextfield设置右对齐后 开头空格无法右对齐的解决方式

 
  

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string

{

    if (range.location == textField.text.length && [string isEqualToString:@" "]) {

        // ignore replacement string and add your own

        textField.text = [textField.text stringByAppendingString:@"\u00a0"];

        return NO;

    }

    // for all other cases, proceed with replacement

    return YES;

}

//nameField(UITextfield) 最后去文本数据的时候执行下面的操作

nameField.text = [nameField.text stringByReplacingOccurrencesOfString:@"\u00a0" withString:@" "];

你可能感兴趣的:(IOS)