IOS-how to remove the UITextField long-pressed gesture menu and input content restrictions

Source: Internet
Author: User

IOS-how to remove the UITextField long-pressed gesture menu and input content restrictions

If this is used in the project today, you can use it directly when you do not know it.

First, rewrite the UITextFiled subclass. copy the following method under the initialization method to disable the long-pressed menu.

// Disable copy paste from the long-pressed menu of textField.

-(BOOL) can1_maction :( SEL) action withSender :( id) sender

{

If ([UIMenuControllersharedMenuController]) {

[UIMenuControllersharedMenuController]. menuVisible = NO;

}

Return NO;

}

During development, we sometimes perform some operations on UITextFiled to explain how to enter only Chinese characters, or just numbers and 2 Chinese methods.


I directly use macro definition:

# Define kAlphaNum @ "abcdefghijklmnopqrstuvwxyz" // This type indicates that only Chinese characters are input and other numbers or symbols are not allowed.

# Define kEnglishNum @ "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" // only numbers and English letters can be entered.


Run the delegate method of UITextField. Remember textFiled. delegate = self;


The following code is directly used:

-(BOOL) textField :( UITextField *) textField shouldChangeCharactersInRange :( nsange) range replacementString :( NSString *) string

{

UITextField * nameField = (UITextField *) [selfviewWithTag: 200];

UITextField * cardField = (UITextField *) [selfviewWithTag: 201];


If (textField = nameField ){

// Only Chinese characters can be entered

NSCharacterSet * cs;

Cs = [[NSCharacterSetcharacterSetWithCharactersInString: kAlphaNum] invertedSet];

NSString * filtered = [[stringcomponentsSeparatedByCharactersInSet: cs] componentsJoinedByString: @ ""];

BOOL basic = [string isEqualToString: filtered];

NSString * text = [textField. textstringByReplacingCharactersInRange: range withString: string];

If (_ delegate & [_ delegaterespondsToSelector: @ selector (passName :)]) {

[_ DelegatepassName: text];

}

Return basic;

}


If (textField = cardField ){


// Only English and numbers are allowed.

NSCharacterSet * cs;

Cs = [[NSCharacterSetcharacterSetWithCharactersInString: kEnglishNum] invertedSet];

NSString * filtered = [[stringcomponentsSeparatedByCharactersInSet: cs] componentsJoinedByString: @ ""];

BOOL basic = [string isEqualToString: filtered];

NSString * text = [textField. textstringByReplacingCharactersInRange: range withString: string];

If (_ delegate & [_ delegaterespondsToSelector: @ selector (passCard :)]) {

[_ DelegatepassCard: text];

}

Return basic;

}

Return YES;


}


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.