I need to use two different fonts in text view, so I set attributed Text in textViewDidChange. But for Japanese keyboard the input character is entered repeatedly.
It works for English keyboard. It also works for Japanese keyboard when you use normal text instead of attributedText.
My code:
- (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 attributed text with Japanese keyboard and get normal result? (without extra characters)
UITextView supports the display of text using custom style information and also supports text editing. You typically use a text view to display multiple lines of text, such as when displaying the body of a large text document. This class supports multiple text styles through use of the attributedText property.
We shall utilise the Delegate feature so the view, as the delegate to the UITextFields and UITextViews, can control the impact of the keyboard display on the positions of UITextFields and UITextViews.
UITextView supports the display of text using custom style information and also supports text editing. You typically use a text view to display multiple lines of text, such as when displaying the body of a large text document.
// Resign the first responder from textField to close the keyboard. To hide the keyboard when the user presses the return key on UITextView ‘s keyboard. We can add the below code in the textView function. You should first make your ViewController class implement the UITextViewDelegate delegate protocol.
This answer helped me figure it out: Check if markedTextRange on the UITextView is not nil. This means that the user is in the middle of inputting a multistage character. Hold off on editing the attributedText until they are done.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With