IOStextfield limits the length of input characters and filters emojis,

Source: Internet
Author: User

IOStextfield limits the length of input characters and filters emojis,

// Add a modification box

-(Void) creatSaveView {

UILabel * lab = [[UILabel alloc] init];

If (self. searchType = 1 ){

Lab. frame = CGRectMake (14, 8 * KWIDTH + 64, IPHONE_WIDTH-100, 16 * KWIDTH );

} Else {

Lab. frame = CGRectMake (14, 8 * KWIDTH, IPHONE_WIDTH-100, 16 * KWIDTH );

}

 

Lab. text = @ "remark name ";

Lab. font = [UIFont systemFontOfSize: 14];

Lab. textColor = kColor (137,137,137 );

[Self. view addSubview: lab];

UIView * view = [[UIView alloc] init];

If (self. searchType = 1 ){

View. frame = CGRectMake (0, 32 * KWIDTH + 64, IPHONE_WIDTH, 40 * KWIDTH );

} Else {

View. frame = CGRectMake (0, 32 * KWIDTH, IPHONE_WIDTH, 40 * KWIDTH );

}

 

UIView * lineViewlight3 = [[UIView alloc] initWithFrame: CGRectMake (0.5, IPHONE_WIDTH,)];

LineViewlight3.backgroundColor = kColor (200,200,200 );

LineViewlight3.alpha = 0.8;

[View addSubview: lineViewlight3];

UIView * lineViewlight4 = [[UIView alloc] initWithFrame: CGRectMake (0.5 * KWIDTH-0.5, IPHONE_WIDTH,)];

LineViewlight4.backgroundColor = kColor (200,200,200 );

LineViewlight4.alpha = 0.7;

[View addSubview: lineViewlight4];

View. backgroundColor = [UIColor whiteColor];

_ NameField = [[UITextField alloc] initWithFrame: CGRectMake (10 * KWIDTH, 0, IPHONE_WIDTH-15 * KWIDTH, 40 * KWIDTH)];

_ NameField. text = self. NameField;

_ NameField. borderStyle = UITextBorderStyleNone;

_ NameField. clearButtonMode = UITextFieldViewModeAlways;

[[Nsicationcenter center defacenter center] addObserver: selfselector: @ selector (textFiledEditChanged :)

Name: @ "UITextFieldTextDidChangeNotification"

Object: _ nameField];

[View addSubview: _ nameField];

[Self. view addSubview: view];

}

 

 

-(Void) textFiledEditChanged :( NSNotification *) obj {

UITextField * textField = (UITextField *) obj. object;

// Filter out the system expression

// UITextView * textView;

//

// Nsange textRange = [textView selectedRange];

// [TextView setText: [self disable_emoji: [textView text];

// [TextView setSelectedRange: textRange];

NSString * toBeString = textField. text;

DSLog (@ "----- toBeString 1 ---- % @", toBeString );

BOOL isEmoj = [self stringContainsEmoji: toBeString];

NSString * _ showStr;

ToBeString = [self disable_emoji: toBeString];

// TextField. text = toBeString;

DSLog (@ "----- toBeString 2 ----- % @", toBeString );

 

NSString * lang = [[UITextInputMode currentInputMode] primaryLanguage]; // keyboard input mode

If ([lang isw.tostring: @ "zh-Hans"]) {// Simplified Chinese input, including simplified Chinese pinyin, five strokes for fitness, and simplified handwriting

UITextRange * selectedRange = [textField markedTextRange];

// Obtain the highlighted part

UITextPosition * position = [textFieldpositionFromPosition: selectedRange. start offset: 0];

// If no selected words are highlighted, the words that have been entered are counted and restricted.

If (! Position ){

If (toBeString. length> kHZMaxLength ){

TextField. text = [toBeString substringToIndex: kHZMaxLength];

_ ShowStr = [toBeString substringToIndex: kHZMaxLength];

}

}

// If a highlighted string exists, no text statistics and restrictions are imposed.

Else {

}

}

// Statistical restrictions can be directly imposed on Chinese input methods, regardless of other languages

Else {

If (toBeString. length> kEGMaxLength ){

TextField. text = [toBeString substringToIndex: kEGMaxLength];

_ ShowStr = [toBeString substringToIndex: kEGMaxLength];

}

}

DSLog (@ "_ showStr % @", _ showStr );

If (isEmoj ){

If ([_ showStr length]) {

TextField. text = _ showStr;

 

} Else {

TextField. text = toBeString;

}

}

}

 

// Determine whether the NSString contains emoji

-(BOOL) stringContainsEmoji :( NSString *) string

{

_ Block BOOL returnValue = NO;

[String enumerateSubstringsInRange: NSMakeRange (0, [string length]) options: NSStringEnumerationByComposedCharacterSequences usingBlock: ^ (NSString * substring, nsange substringRange, nsange enclosingRange, BOOL * stop ){

Const unichar hs = [substring characterAtIndex: 0];

// Surrogate pair

If (0xd800 ){

If (0xd800 <= hs & hs <= 0 xdbff ){

If (substring. length> 1 ){

Const unichar ls = [substring characterAtIndex: 1];

Const int uc = (hs-0xd800) * 0x400) + (ls-0xdc00) + 0x10000;

If (0x1d000 <= uc & uc <= 0x1f77f ){

ReturnValue = YES;

}

}

} Else if (substring. length> 1 ){

Const unichar ls = [substring characterAtIndex: 1];

If (ls = 0x20e3 ){

ReturnValue = YES;

}

} Else {

// Non surrogate

If (0x2100 <= hs & hs <= 0x27ff ){

ReturnValue = YES;

} Else if (0x2B05 <= hs & hs <= 0x2b07 ){

ReturnValue = YES;

} Else if (0x2934 <= hs & hs <= 0x2935 ){

ReturnValue = YES;

} Else if (0x3297 <= hs & hs <= 0x3299 ){

ReturnValue = YES;

} Else if (hs = 0xa9 | hs = 0xae | hs = 0x303d | hs = 0x3030 | hs = 0x2b55 | hs = 0x2b1c | hs = 0x2b1b | hs = 0x2b50) {

ReturnValue = YES;

}

}

}

}];

Return returnValue;

}

# Pragma Mark --- filter emoticon

 

-(NSString *) disable_emoji :( NSString *) text

{

NSRegularExpression * regex = [NSRegularExpressionregularExpressionWithPattern: @ "[^ \ u0020-\ u007E \ u00A0-\ u00BE \ u2E80-\ uA4CF \ uF900-\ uFAFF \ uFE30-\ uFE4F \ \ uFF00-\ uFFEF \ u0080-\ u009F \ u2000-\ u201f \ r \ n] "options: NSRegularExpressionCaseInsensitive error: nil];

NSString * modifiedString = [regex stringByReplacingMatchesInString: text

Options: 0

Range: NSMakeRange (0, [text length])

WithTemplate: @ ""];

Return modifiedString;

}

Related Article

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.