Uitextfield limits the input length, and uitextview limits the input length.

Source: Internet
Author: User

---------------- Uitextfield limits the input length ------------

.

Class to inherit uitextfielddelegate.

.

Uitextfield * content = [uitextfield
Alloc] initwithframe:
Cgrectmake (X, Y, width, height)];

Content. textcolor = [uicolor
Graycolor];

Content. font = [uifont
Systemfontofsize: 17];

Content. Delegate =
Self; // specify your delegate

.

# Pragma mark-

# Pragma mark uitextfielddelegate

-(Bool) textfield :( uitextfield *) textfield shouldchangecharactersinrange :( nsange) range replacementstring :( nsstring
*) String

{


If (range. Location>
20)

{

Uialertview * alertview = [uialertview
Alloc] initwithtitle: Nil
Message: @ "length cannot exceed 20"
Delegate: Nil
Cancelbuttontitle: @ "close"
Otherbuttontitles: Nil];

[Alertview
Show];

[Alertview
Release];

Return
No;

}


Return yes;

}

---------------- Uitextview limits the input length ------------

.

<Uitextviewdelegate>

.

Inputtextview. Delegate = self;
// Specify the delegate

.

# Pragma mark-

# Pragma mark textview

-(Bool) textview :( uitextview *) textview shouldchangetextinrange :( nsange) range replacementtext :( nsstring *) text {

If ([text length] = 0 ){

Return yes;

}

If ([textview. Text length] + range. length> = 140 ){

Return no;

}

Return yes;

}

-(Void) textviewdidchange :( uitextview *) textview {

// [Nsstring stringwithformat: @ "% d", 140-[textview. Text length];
// The length that can be entered.

}

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.