Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITextView attributedText with Japanese keyboard repeats input

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)

like image 510
Jakub Vodak Avatar asked Jul 15 '15 12:07

Jakub Vodak


People also ask

How do I display multiple text styles in UITextView?

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.

How to control the impact of keyboard display on uitextfields?

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.

What is UITextView used for?

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.

How to hide the keyboard on UITextView’s return key?

// 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.


1 Answers

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.

like image 60
Chen Lim Avatar answered Oct 12 '22 20:10

Chen Lim