Ios:uitextfield the limit of character length when inputting Chinese input method

Source: Internet
Author: User
Tags wubi

Title of the problem, is a let me crazy half the day of the problem, or to do a record, there are similar problems with classmates can refer to, but not necessarily. Specific problems need to be analyzed. I met the demand is this: there is an input box, input box input text, text word limit of 20 words. I used Uitextfield as my input box control, and in the delegate method:-(BOOL) TextField: (Uitextfield *) TextField Shouldchangecharactersinrange: ( Nsrange) Range replacementstring: (NSString *) string that implements the length limit for strings, implemented as follows: #define KMAXLENGTH 20
-(BOOL) TextField: (Uitextfield *) TextField Shouldchangecharactersinrange: (nsrange) range replacementstring: ( NSString *) string{

NSString * tobestring = [Textfield.text stringbyreplacingcharactersinrange:range withstring:string];

if (Tobestring.length > Kmaxlength && range.length!=1) {
Textfield.text = [tobestring substringtoindex:kmaxlength];
return NO;

}
return YES;
The result of this implementation is that there is no problem with statistics for pure characters, and when the input character exceeds the limit, the input box intercepts the maximum limit length of the string. However, when using the Pinyin IME, the last parameter in the delegate method string accepts the input letter instead of the selected kanji, causing the result when you want to enter the text "I'm Programming", enter the Pinyin "Wozaibiancheng", Each letter entered into the delegate method, the statistical character length is the length of the letter, in fact, the character has not exceeded the limit length, but the length of the letter is more than the result can not continue input.  Moreover, the fatal is that this delegate method does not respond, the process of selecting candidate characters, there is no way to re-correct the character length statistics. I didn't find it at first-(BOOL) TextField: (Uitextfield *) TextField Shouldchangecharactersinrange: (nsrange) Range Replacementstring: (NSString *) String This delegate method did not respond to the final pinyin to the Chinese character process, when I found this problem, the problem is almost solved. 1, because before to a friend on Weibo, he told me that can register the observer uitextinputcurrentinputmodedidchangenotification in its listening to get Chinese. When I first started to understand what he meant, I thought I was going to get the final selected kanji, and the pinyin in the middle would not be obtained. But in fact, this listener, more than the delegation method above the last step, that is, from pinyin to Chinese process. So the code of implementation is as follows. <1> register at INIT notification: [[Nsnotificationcenter Defaultcenter]addobserver:selfselector: @selector ( Textfilededitchanged:)
name:@ "Uitextfieldtextdidchangenotification"
Object:mytextfield]; <2> implementation of the Monitoring method:-(void) textfilededitchanged: (nsnotification *) obj{
Uitextfield *textfield = (Uitextfield *) Obj.object;

NSString *tobestring = Textfield.text;
NSString *lang = [[Uitextinputmode currentinputmode] primarylanguage]; Keyboard Input Mode
if ([lang isequaltostring:@ "Zh-hans"]) {//Simplified Chinese input, including simplified pinyin, fitness wubi, simplified handwriting
Uitextrange *selectedrange = [TextField markedtextrange];
Get highlighted parts
Uitextposition *position = [TextFieldpositionFromPosition:selectedRange.start offset:0];
Word count and limit the words you have entered without highlighting the selected words
if (!position) {
if (Tobestring.length > Kmaxlength) {
Textfield.text = [tobestring substringtoindex:kmaxlength];
}
}
There is a highlighted string, the text is temporarily not counted and limited
else{

}
}
Direct statistical restrictions other than Chinese input method, regardless of other languages
else{
if (Tobestring.length > Kmaxlength) {
Textfield.text = [tobestring substringtoindex:kmaxlength];
}
}
} <3> write off the listening method in Dealloc, remember! -(void) dealloc{
[[Nsnotificationcenter Defaultcenter]removeobserver:self
name:@ "Uitextfieldtextdidchangenotification"
Object:_albumnametextfield];
}

2.

[_expertsignature addtarget:self Action: @selector (textfielddidchangeediting) forControlEvents: Uicontroleventeditingchanged];

-(void) textfielddidchangeediting {

NSString *tobestring = Self.expertSignature.text;

NSString *lang = [[Uitextinputmode currentinputmode] primarylanguage];

if ([lang isequaltostring:@ "Zh-hans"]) {//Simplified Chinese input, including simplified pinyin, fitness wubi, simplified handwriting

Uitextrange *selectedrange = [Self.expertsignature markedtextrange];

Uitextposition *position = [self.expertsignature positionFromPosition:selectedRange.start offset:0];

if (!position) {//non-highlighted

if (Tobestring.length > Kmaxlength) {

[Toolutil showhud:@] You can enter up to 22 words "duration:2";

Self.expertSignature.text = [tobestring substringtoindex:kmaxlength];

}

}

}else{//Chinese Input Method

if (Tobestring.length > Kmaxlength) {

[Toolutil showhud:@] You can enter up to 22 words "duration:2";

Self.expertSignature.text = [tobestring substringtoindex:kmaxlength];

}

}

}

Ios:uitextfield the limit of character length when inputting Chinese input method

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.