Uitextfield the limit of character length when inputting Chinese input method

Source: Internet
Author: User

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 The length limit of the string is implemented as follows:#define KMAXLENGTH 20
-(BOOL)TextField: (Uitextfield*)TextFieldShouldchangecharactersinrange: (Nsrange)RangeReplacementstring: (NSString*)String{

NSString*Tobestring=[TextField.TextStringbyreplacingcharactersinrange:RangeWithstring:String];

IfTobestring.Length>Kmaxlength&&Rangelength!=1       textfield.text = [tobestring Span style= "color: #c0c0c0;" >substringtoindex:kmaxlength       return NO;

        returnyes;

/span>   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.   and, to be deadly, this delegate method does not respond, the process of selecting candidate characters, there is no way to re-correct the character length statistics.   looked up some methods online, a blogger called Onyx wrote an article: how to correctly count input words in English and pinyin in iOS Uitextview/uitextfield   seems to be the same problem as me, But when I moved his code into my class, the results were not good, I didn't get the results I wanted, and his approach seemed a little bit more complicated, and I didn't study how the problem was. He was introduced to this post because his article later gave me some inspiration.   I didn't find it at first  -(BOOL)TextField: (uitextfield *)TextField shouldchangecharactersinrange: (nsrange )Rangereplacementstring: (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. He told me that I could sign up for this observer when I consulted a friend on Weibo.uitextinputcurrentinputmodedidchangenotification It can be listened to in 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 notification at init time:[[nsnotificationcenter Defaultcenter]addobserver: self selector: @selector (textfilededitchanged:)
                                    name:@ "Uitextfieldtextdidchangenotification"
                                  object:mytextfield <2> Implement monitoring methods:-(voidTextfilededitchanged: (Nsnotification*)Obj{
Uitextfield*TextField= (Uitextfield*)Obj.Object

NSString*Tobestring=TextField.Text
NSString*Lang=[[UitextinputmodeCurrentinputmode]Primarylanguage];Keyboard Input Mode
If([LangIsequaltostring:@ "Zh-hans"]){Simplified Chinese input, including simplified pinyin, fitness wubi, simplified handwriting
Uitextrange*Selectedrange=[TextFieldMarkedtextrange];
Get highlighted parts
Uitextposition*Position=[TextFieldPositionfromposition:Selectedrange.StartOffset0];
Word count and limit the words you have entered without highlighting the selected words
If!Position{
IfTobestring.Length>Kmaxlength){
TextField.Text=[TobestringSubstringtoindex: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];
       "
   "

/span> <3> write off the listening method in Dealloc, remember!

-(void)dealloc{

[[nsnotificationcenterdefaultcenter]removeobserver:Self
Name:@ "Uitextfieldtextdidchangenotification"
object:_albumnametextfield];
}

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.