Uitextfield, uitextview, uilabel, keyboard, uifont

Source: Internet
Author: User

1.Uitextview is also called a smart tag.

(1 ).-[UITextView setContentToHTMLString:]You can set HTML content, but this is a private API function.

Uitextview is preferred for displaying large text segments, not uilabel.

(2) Set inputaccessoryview and add a toolbar to close the keyboard.

{
Uitoolbar * inputaccessoryview = [[uitoolbar alloc] initwithframe: cgrectmake (0, 0,320, 30)];
[Inputaccessoryview setbarstyle: uibarstyleblack];
Uibarbuttonitem * spacebutton = [[uibarbuttonitem alloc] initwithbarbuttonsystemitem: uibarbuttonsystemitemflexiblespace target: Self action: Nil] autorelease];
Uibarbuttonitem * donebutton = [[[uibarbuttonitem alloc] initwithtitle: @ "complete input" style: uibarbuttonitemstyledone target: Self action: @ selector (dismisskeyboard)] autorelease];
Nsarray * buttonsarray = [nsarray arraywithobjects: spacebutton, donebutton, nil];
[Inputaccessoryview setitems: buttonsarray];
[Textview setinputaccessoryview: inputaccessoryview];
}
-(Void) dismisskeyboard
{
[Textview resignfirstresponder];

}

(3) The most perfect multi-line input with underlines

Http://115.com/file/dpko1yxw#LinedTextView.zip

(4). Restrict the input length

Static const int kmaxtextlength = 280;

# Pragma mark-
# Pragma mark uitextviewdelegate

-(Bool) textview :( uitextview *) textview shouldchangetextinrange :( nsange) range replacementtext :( nsstring *) Text
{
Nsstringencoding ENC = cfstringconvertencodingtonsstringencoding (kcfstringencodinggb_18030_2000 );
Int length = [textview. Text lengthofbytesusingencoding: ENC];
Int addlength = [text lengthofbytesusingencoding: ENC];

If (length> = kmaxtextlength & addlength> 0 | length + addlength> kmaxtextlength)
{
Return no;
}

Return yes;
}

-(Void) textviewdidchange :( uitextview *) textview
{
Nsstringencoding ENC = cfstringconvertencodingtonsstringencoding (kcfstringencodinggb_18030_2000 );
Int length = [textview. Text lengthofbytesusingencoding: ENC];
Icountlabel. Text = [nsstring stringwithformat: @ "% d", length];
}

(5) obtain the cursor position in uitextview

Http://blog.csdn.net/liyanbo1984/article/details/5807095

2. uitextfield implements uitextinput protocol, uitextinput protocol implements uikeyinput protocol, and uikeyinput protocol implements uitextinputtraits protocol, which provides seven attributes: Automatic Text uppercase style, etc.

(1) Some uitextfield attributes:

Placeholder: placeholder prompt. The default value is 70% gray. uitextview does not have the placeholder attribute.

Borderstyle: border style of the input box;

Clearbuttonmode: uitextfieldviewmodewhileediting displays a circular cross number on the right of the input box to clear the content. It will be overwritten by rightview.

Inputview: Replace the standard keyboard

Uireturnkeytype returnkeytype: // default is uireturnkeydefault (see note under uireturnkeytype Enum)

Autocapitalizationtype = uitextautocapitalizationtypenone; // remove the upper-case letter. autocapitalizationtype is uitextautocapitalizationtypesentences by default.

Contentverticalignment = uicontrolcontentverticalalignmentcenter; // text Center

(2) leftview and rightview examples

textField.leftViewMode = UITextFieldViewModeAlways;   textField.rightViewMode = UITextFieldViewModeAlways;UILabel* label1=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 30, 20)]; label1.text=@"left"; label1.textAlignment=UITextAlignmentLeft; textField.leftView=label1; [label1 release]; label1=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 40, 20)]; label1.text=@"right"; label1.textAlignment=UITextAlignmentRight; textField.rightView=label1;[label1 release];

More uitextfield attributes

Http://blog.csdn.net/jinglijun/article/details/7058632

Http://www.uplook.cn/index-Index-show-view17944.html

Http://unmi.cc/uilable-uitextfield-padding-insets

Comprehensive parsing of uitextfield in IOS

Http://my.oschina.net/u/936286/blog/131010

5. uitextfielddelegate

-(Bool) textfield :( uitextfield *) textfield shouldchangecharactersinrange :( nsange) range replacementstring :( nsstring *) string; used to filter input content.

This is a verbatim operation. This method is called every time you click the keyboard.

You can use the following syntax to filter:

Nsstring * filtered = [String componentsseparatedbycharactersinset: characterset] componentsjoinedbystring: @ ""];
Bool basictest = [String isequaltostring: Filtered];

Nspredicate ........

Uitextviewdelegate-(void) textviewdidchange :( uitextview *) textview is triggered only when uitextview is in isfirstresponder.

6. General Method to Solve the Problem of keyboard occlusion during uitextfiled Input

Http://blog.csdn.net/favormm/article/details/6799942

When the iPhone is portrait, the keyboard height is 216, And the pop-up time is 0.3 s.

IPad height 264 352 318 406 54 Chinese high

Ios5 dynamically obtains the keyboard height of 216,252. The uikeyboardwillchangeframenotification notification does not occur as expected. Pay attention to the following points:

A. When switching between English and Chinese input methods, the iPhone will not generate uikeyboardwillchangeframenotification and uikeyboarddidchangeframenotification notifications, but the iPad will;
B. changeframe notification occurs before show or hide notification;
C. There will be hide notification in the split keyboard on the iPad, and show notification will be available only when the keyboard is merged. The keyboard is hidden during split;

Reference http://xcodev.com/wordpress? P = 410

-(Void) keyboarddidshow: (nsnotification *) notif
{
Nsdictionary * userinfo = [notif userinfo];
Nsvalue * value = [userinfo objectforkey: uikeyboardframeenduserinfokey];
Float keyboardheight = [value cgrectvalue]. Size. height; // The height is 216 keyboardheight = 252;

// Value = [userinfo objectforkey: uikeyboardanimationdurationuserinfokey];
// Nstimeinterval animationduration;
// [Value getvalue: & animationduration];
}


9. In info. plist, add localizations: Chinese and set the function layer that appears in the long-press input box to Chinese (copy and so on ).

10. uilabel

(1) // shadow

Label. shadowcolor = [uicolor colorwithwhite: 0.0f ALPHA: 0.75f];
Label. shadowoffset = cgsizemake (0.0f,-2.0f );

Another stupid method: defines two uilabel overlays. The underlying uilabel text is the shadow translucent effect. The uilabel on the upper layer and the uilabel on the lower layer are offset by one pixel and the background is transparent.

// Single row, too long omitted

Label _. linebreakmode = uilinebreakmodetailtruncation;
Label _. numberoflines = 1;

(2) Texts of multiple colors can be displayed in the same uilabel.

Three20
Http://github.com/facebook/three20/

Fontlabel
Http://github.com/zynga/FontLabel/

Http://www.cocoachina.com/bbs/simple? T14541.html

13. Custom placeholder

Inherit uitextfield and override drawplaceholderinrect:

-(Void) drawplaceholderinrect :( cgrect) rect

{
// Set color and font size of placeholder text
[Color (0xc4, 0xc4, 0xc4) setfill];
[[Self placeholder] drawinrect: rect withfont: [utility fontwithsize: 12];
}

16. Switch the Input Method

The uitextinputmode class is a new class that is available only after 4.2 and is used to obtain the current text input mode. That is, when the user inputs the text, the user determines which keyboard is used.
This value is obtained when the input mode is changed as follows:
Registration notification:
[[Nsicationcenter center defacenter center] addobserver: Self selector: @ selector (keyboardchangemode :) name: uitextinputcurrentinputmodedidchangenotification object: Nil];
-(Void) keyboardchangemode :( nsnotification *) Notification
{
Nslog (@ "keyboardchangemode: % @", [[uitextinputmode currentinputmode] primarylanguage]);
}

En-US // english
Zh-Hans // Simplified Chinese
Zh-HANT // traditional Chinese

You can also use nslog (@ "currentinputmode: % @", [uitextinputmode currentinputmode] primarylanguage]) If you do not register a notification.

Http://blog.sina.com.cn/s/blog_69081e060100v7ad.html

17. Use the textfield created by the class starting with the added initwithframe.... The displayed uitextfield cannot respond to Keyboard Events. Therefore, the constructors should be encapsulated using static methods.

18.
Nsarray * familynames = [uifont familynames];
For (nsstring * familyname in familynames)
{
Printf ("Family: % s \ n", [familyname utf8string]);
Nsarray * fontnames = [uifont fontnamesforfamilyname: familyname];
For (nsstring * fontname in fontnames)
{
Printf ("\ tfont: % s \ n", [fontname utf8string]);
}
}

Http://www.cocoachina.com/bbs/simple? T70231.html

Http://www.cnblogs.com/tracy-e/archive/2010/11/04/1869250.html

19. uitextfield switch password in plaintext format

Ipasswordtextfield. securetextentry = yes;
Uibutton * eyebutton = [uibutton buttonwithorigin: cgpointmake (0, 0) normalimage: [uiimage imagenamed: @ "eye.png"] identifier: Nil selectedimage: Nil];
[Eyebutton addtarget: Self action: @ selector (showorhidepassword) forcontrolevents: uicontroleventtouchupinside];
Ipasswordtextfield. rightview = eyebutton;
Ipasswordtextfield. rightviewmode = uitextfieldviewmodealways;
Ipasswordtextfield. clearbuttonmode = uitextfieldviewmodenever;

-(Void) showorhidepassword
{
[Ipasswordtextfield resignfirstresponder]; // if you do not lose the focus first, the setting fails.

Bool issecuretextentry = ipasswordtextfield. issecuretextentry;
[Ipasswordtextfield setsecuretextentry :! Issecuretextentry];
}

20. // drawing and positioning overrides
Rewrite the following methods to meet some requirements.
-(Cgrect) borderrectforbounds :( cgrect) bounds;
-(Cgrect) textrectforbounds :( cgrect) bounds;
-(Cgrect) placeholderrectforbounds :( cgrect) bounds;
-(Cgrect) editingrectforbounds :( cgrect) bounds;
-(Cgrect) clearbuttonrectforbounds :( cgrect) bounds;
-(Cgrect) leftviewrectforbounds :( cgrect) bounds;
-(Cgrect) rightviewrectforbounds :( cgrect) bounds;

-(Void) drawtextinrect :( cgrect) rect;
-(Void) drawplaceholderinrect :( cgrect) rect;

21. You can create a false invisible uitextfield, and then

[self.view addSubView:textField];[textField becomeFirstResponser];

22.

 

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.