Right aligned UITextField spacebar does not advance cursor in iOS 7

前端 未结 14 1728
执笔经年
执笔经年 2020-12-07 23:04

In my iPad app, I noticed different behavior between iOS 6 and iOS 7 with UITextFields.

I create the UITextField as follows:

UIButton *theButton = (U         


        
14条回答
  •  误落风尘
    2020-12-07 23:24

    Fix right aligned text space removing by replacing space with non-breaking space

    - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
    {
        if (textField.textAlignment == NSTextAlignmentRight) {
            NSString *text = [textField.text stringByReplacingCharactersInRange:range withString:string];
            textField.text = [text stringByReplacingOccurrencesOfString:@" " withString:@"\u00a0"];
    
            UITextPosition *startPos = [textField positionFromPosition:textField.beginningOfDocument offset:range.location + string.length];
            UITextRange *textRange = [textField textRangeFromPosition:startPos toPosition:startPos];
            textField.selectedTextRange = textRange;
    
            return NO;
        }
    
        return YES;
    }
    

    And vice versa

    - (void)textFieldDidEndEditing:(UITextField *)textField
    {
        // Replacing non-breaking spaces with spaces and remove obsolete data
        NSString *textString = [[textField.text stringByReplacingOccurrencesOfString:@"\u00a0" withString:@" "] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
        textField.text = textString;
    }
    

提交回复
热议问题