Uitextfield limit the length of text Support Chinese Association

Source: Internet
Author: User

Uitextfield+text_constraints.h



@interface Uitextfield (text_constraints)//take into account the length of the text in some places is a range eg:2-10, so use Nsrange@property (nonatomic, assign) Nsrange textlengthrange;-(BOOL) Shouldchangeinrange: (nsrange) Range replacestring: (NSString *) string; @end

  

Uitextfield+text_constraints.m#import "Uitextfield+text_constraints.h" #import <objc/runtime.h>@ Implementation Uitextfield (text_constraints) #pragma mark-textlengthrange/** * Add notification of text changes, enter pinyin to select Chinese characters will be notified * Delegate Shou Ldchangecharactersinrange method does not call */-(void) addtextchangenotification{when selecting Chinese characters [[Nsnotificationcenter Defaultcenter]    Removeobserver:self name:uitextfieldtextdidchangenotification Object:nil]; [[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (textfieldtextdidchangenotif:) Name: Uitextfieldtextdidchangenotification Object:nil];}    -(void) Textfieldtextdidchangenotif: (nsnotification *) notif{Uitextfield *textfield = notif.object;    Nsuinteger MaxNumber = self.textLengthRange.location + self.textLengthRange.length; if (Textfield.markedtextrange = = nil) {if (TextField.text.length > MaxNumber) {textfield.text = [te        Xtfield.text Substringtoindex:maxnumber]; }}}-(void) Settextlengthrange: (Nsrange) Textlengthrange{[self addtextchangenotification]; Objc_setassociatedobject (Self, @selector (textlengthrange), [Nsvalue Valuewithrange:textlengthrange], OBJC_ association_retain_nonatomic);}    -(Nsrange) textlengthrange{nsvalue *value = Objc_getassociatedobject (self, @selector (textlengthrange));    if (value) {return value.rangevalue; } return Nsmakerange (0, Nsuintegermax);} #pragma mark-textfield replace//(BOOL) TextField: (Uitextfield *) TextField Shouldchangecharactersinrange: (Nsrange) Range replacementstring: (NSString *) string, using-(BOOL) Shouldchangeinrange: (nsrange) Range replacestring: (NSString *) string{if (range.location >= (self.textLengthRange.location + self.textLengthRange.length) && (self.    Markedtextrange = = Nil && Range.length = = 0)) {return NO; } return YES; @end

  

Uitextfield limit the length of text Support Chinese Association

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.