UITextView attribittedText with repetition of input in the Japanese keyboard

I need to use two different fonts in a text view, so I set the Text attribute to textViewDidChange. But for the Japanese keyboard, the input character is re-entered.

It works for english keyboard. It also works for the Japanese keyboard when you use plain text instead of the Text attribute.

My code is:

- (void)viewDidLoad { [super viewDidLoad]; UITextView *textView = [[UITextView alloc] initWithFrame:self.view.frame]; textView.delegate = self; [self.view addSubview:textView]; } - (void)textViewDidChange:(UITextView *)textView { NSLog(@"TOTAL: %@", textView.text); textView.attributedText = [[NSMutableAttributedString alloc] initWithString: textView.text]; } - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text { NSLog(@"ADDED: %@", text); return YES; } 

Output:

 2015-07-15 13:51:10.156 japKeyTest[32163:5765000] ADDED: a 2015-07-15 13:51:10.167 japKeyTest[32163:5765000] TOTAL: あ2015-07-15 13:51:11.376 japKeyTest[32163:5765000] ADDED: a 2015-07-15 13:51:11.378 japKeyTest[32163:5765000] TOTAL: あああ2015-07-15 13:51:12.054 japKeyTest[32163:5765000] ADDED: a 2015-07-15 13:51:12.055 japKeyTest[32163:5765000] TOTAL: ああああああ 

Expected:

 2015-07-15 13:51:10.156 japKeyTest[32163:5765000] ADDED: a 2015-07-15 13:51:10.167 japKeyTest[32163:5765000] TOTAL: あ2015-07-15 13:51:11.376 japKeyTest[32163:5765000] ADDED: a 2015-07-15 13:51:11.378 japKeyTest[32163:5765000] TOTAL: ああ2015-07-15 13:51:12.054 japKeyTest[32163:5765000] ADDED: a 2015-07-15 13:51:12.055 japKeyTest[32163:5765000] TOTAL: あああ 

Any idea how to enter attribute text with a Japanese keyboard and get a normal result? (no extra characters)

+7
ios objective-c uitextview keyboard
source share
2 answers

This answer helped me understand: Check if noTextRange is checked in the UITextView. This means that the user is in the middle of entering a multi-step character. Hold while editing the Text attribute until they are executed.

+2
source share

My experience shows that an error (and I believe that it is a UIKit error) occurs at any time when the -attributedText property of a UITextField or UITextView changes during - textView(Field)DidChange time (just like in your example).

In my case, I was able to change the code so that the update did not happen during this time (updating in my case was not required at all, so it was easy).

If you need to update the text attribute at this time, all I can recommend is a file with an error from Apple and wait (forever or longer).

Note that you can also see this error, even if you only update the -text property of these objects - because UIKit will sometimes (always?) Go ahead and update the -attributedText property for you when you update -text . (you can see this by subclassing these objects and overriding -setAttributedText: and setting a breakpoint.

Good luck to you

Edit: I have only seen this error with Japanese keyboards so far, but as I gain more experience working with other applications, I would not be surprised to see more problems.

0
source share

All Articles