[Turn]ios Uitextfield use comprehensive analysis

Source: Internet
Author: User

001
Initialize TextField and set location and size
002
Uitextfield *text = [[Uitextfield alloc]initwithframe:cgrectmake (20, 20, 130, 30)];
003

004
Set border style to show border style only if set
10T
Text.borderstyle = Uitextborderstyleroundedrect;
006
typedef enum {
007
Uitextborderstylenone,
008
Uitextborderstyleline,
009
Uitextborderstylebezel,
010
Uitextborderstyleroundedrect
011
} Uitextborderstyle;
012
 
013
Sets the background color of the input box, which is set to white if you use a custom background picture border is ignored
014
Text.backgroundcolor = [Uicolor Whitecolor];
015

016
Set background
017
Text.background = [UIImage imagenamed:@ "Dd.png"];
018

019
Set background
020
Text.disabledbackground = [UIImage imagenamed:@ "Cc.png"];
021
When the input box does not have content, the watermark prompt content is password
022
Text.placeholder = @ "password";
023

024
Set font style and size for input box contents
025
Text.font = [Uifont fontwithname:@ "Arial" size:20.0f];
026

12V
Set Font Color
028
Text.textcolor = [Uicolor Redcolor];
029

030
Whether there is a fork in the input box, when it appears, to delete the contents of the input box at once
031
Text.clearbuttonmode = Uitextfieldviewmodealways;
032

033
typedef enum {
034
Uitextfieldviewmodenever, the heavy does not appear
035
Uitextfieldviewmodewhileediting, appearing when editing
036
Uitextfieldviewmodeunlessediting, except for the editor's appearance.
037
Uitextfieldviewmodealways always appears.
038
} Uitextfieldviewmode;
039

040
Enter the text from the beginning of the box
041
Text.text = @ "The beginning of the text in the input box";
042

043
Every character you enter becomes a point. Password input
044
Text.securetextentry = YES;
045

046
Whether to correct
047
Text.autocorrectiontype = Uitextautocorrectiontypeno;
048

049
typedef enum {
050
Uitextautocorrectiontypedefault, default
051
Uitextautocorrectiontypeno, no automatic error correction
052
Uitextautocorrectiontypeyes, automatic error correction
053
} Uitextautocorrectiontype;
054

25w
Edit it again and empty it.
056
text.clearsonbeginediting = YES;
15V

058
Content Alignment
059
Text.textalignment = Uitextalignmentleft;
060

061
The vertical alignment of content Uitextfield inherits from Uicontrol, which has an attribute in this class contentverticalalignment
062
Text.contentverticalalignment = Uicontrolcontentverticalalignmentcenter;
063

064
When set to Yes, the text automatically shrinks to fit the text window size. The default is to keep the original size and let long text scroll
065
Textfied.adjustsfontsizetofitwidth = YES;
066

067
Set the minimum font size for auto-shrink display
068
Text.minimumfontsize = 20;
069

070
Set the keyboard style
071
Text.keyboardtype = Uikeyboardtypenumberpad;
072

073
typedef enum {
074
Uikeyboardtypedefault, default keyboard, all characters supported
075
Uikeyboardtypeasciicapable, default keyboard that supports ASCII
076
Uikeyboardtypenumbersandpunctuation, standard telephone keypad, supports +*# characters
077
Uikeyboardtypeurl, URL keyboard, support for. com buttons only support URL characters
078
Uikeyboardtypenumberpad, Digital keypad
079
Uikeyboardtypephonepad, telephone keypad
080
Uikeyboardtypenamephonepad, telephone keypad, also support input person name
081
Uikeyboardtypeemailaddress, keyboard for entering e-mail addresses
082
Uikeyboardtypedecimalpad, numeric keypad with numbers and decimal points
083
Uikeyboardtypetwitter, optimized keyboard for easy input @, #字符
084
Uikeyboardtypealphabet = uikeyboardtypeasciicapable,
085
} Uikeyboardtype;
086

18V
Whether the first letter is capitalized
088
Text.autocapitalizationtype = Uitextautocapitalizationtypenone;
089

090
typedef enum {
091
Uitextautocapitalizationtypenone, not automatically capitalized
092
Uitextautocapitalizationtypewords, capitalize the first letter of the word
093
Uitextautocapitalizationtypesentences, capitalize the first letter of the sentence
094
Uitextautocapitalizationtypeallcharacters, all letters are capitalized.
095
} Uitextautocapitalizationtype;
096

097
Return key becomes what key
098
Text.returnkeytype =uireturnkeydone;
099

100
typedef enum {
101
Uireturnkeydefault, default Gray button, marked with return
102
Uireturnkeygo, the blue button labeled Go
103
Uireturnkeygoogle, a blue button labeled Google, search by language
104
Uireturnkeyjoin, blue button labeled Join
105
Uireturnkeynext, the blue button labeled next.
106
Uireturnkeyroute, a blue button labeled route
107
Uireturnkeysearch, the blue button labeled Search
108
Uireturnkeysend, the blue button labeled Send
109
Uireturnkeyyahoo, the blue button labeled Yahoo!
110
Uireturnkeyyahoo, the blue button labeled Yahoo!
111
Uireturnkeyemergencycall, emergency call button
112
} Uireturnkeytype;
113

114
Keyboard appearance
115
Textview.keyboardappearance=uikeyboardappearancedefault;
116
typedef enum {
117
Uikeyboardappearancedefault, default appearance, light grey
118
Uikeyboardappearancealert, dark grey graphite
119

120
} Uireturnkeytype;
121

122

123
Setting up proxies for implementing protocols
124
Text.delegate = self;
125

126
Add the TextField to the view
127
[Self.window Addsubview:text];
128

129
The rightmost plus picture is similar to the left of the following code
130
Uiimageview *image=[[uiimageview alloc] initwithimage:[uiimage imagenamed:@ "Right.png"];
131
Text.rightview=image;
132
Text.rightviewmode = Uitextfieldviewmodealways;
133

134
typedef enum {
135
Uitextfieldviewmodenever,
136
Uitextfieldviewmodewhileediting,
137
Uitextfieldviewmodeunlessediting,
138
Uitextfieldviewmodealways
139
} Uitextfieldviewmode;
140

141

142
Press RETURN key to receive Becomefirstresponder
143

144
class to adopt the Uitextfielddelegate protocol
145

146
Text.delegate = self; Declare the text of the agent is me, I will go to the way to get the keyboard down the method in uitextfielddelegate so we have to use uitextfielddelegate this protocol
147

148
-(BOOL) Textfieldshouldreturn: (Uitextfield *) TextField
149
{
150
[Text Resignfirstresponder]; The main is [receiver Resignfirstresponder] where the call to the receiver corresponding to the keyboard down
151
return YES;
152
}
153

154

155
overriding Paint behavior
156
In addition to the style options of the Uitextfield object, you can also customize the Uitextfield object and add many different rewrite methods to it to change the display behavior of the text field. All of these methods return a CGRECT structure that has a boundary range for each part of the text field. The following methods can be overridden.
157

158
–textrectforbounds://rewrite to reset text area
159
–drawtextinrect://Change the Emoji property. When overridden, calling super can be drawn by default drawing properties, so you don't have to call super if you completely override the drawing function.
160
–placeholderrectforbounds://Override to reset placeholder area
161
–drawplaceholderinrect://Override to change the draw placeholder property. When overridden, calling super can be drawn by default drawing properties, so you don't have to call super if you completely override the drawing function.
162
–borderrectforbounds://Rewrite to reset the edge area
163
–editingrectforbounds://Rewrite to reset the editing area
164
–clearbuttonrectforbounds://Rewrite to reset the Clearbutton position, changing the size may cause the picture of the button to be distorted
165
–leftviewrectforbounds:
166
–rightviewrectforbounds:
167

168
Delegate method
169

170
-(BOOL) textfieldshouldbeginediting: (Uitextfield *) textfield{
171
  
172
Returns a bool value that specifies whether the sequential text field begins editing
173
  
174
return YES;
175
}
176

177
-(void) textfielddidbeginediting: (Uitextfield *) textfield{
178
Triggered when editing is started, the text field becomes first responder
179
}
180

181
-(BOOL) textfieldshouldendediting: (Uitextfield *) textfield{
182
Returns a bool value that specifies whether the text field is allowed to end editing, and when the edit is finished, the text field will yield first responder
183
To prevent the text field from disappearing when the user finishes editing, you can return no
184
This is useful for programs where text fields must always remain active, such as instant messaging
185
 
186
return NO;
187
}
188

189
-(BOOL) TextField: (uitextfield*) TextField Shouldchangecharactersinrange: (nsrange) range replacementstring: ( NSString *) string{
190
This method is called when the user uses the AutoCorrect feature to modify the input text to the recommended text.
191
This is especially useful for applications that want to join the Undo option
192
You can track the last modification made within a field, or you can log all edits for auditing purposes.
193
To prevent text from being changed, you can return no
194
There is a Nsrange object in the parameter of this method, which indicates the position of the changed text, and the suggested text is in it.
195

196
return YES;
197
}
198

199
-(BOOL) Textfieldshouldclear: (Uitextfield *) textfield{
200

201
Returns a bool value indicating whether content is allowed to be purged based on user request
202
Can be set under certain conditions to allow purging of content
203

204
return YES;
205
}
206

207
-(BOOL) Textfieldshouldreturn: (Uitextfield *) textfield{
208

209
Returns a bool value that indicates whether the edit is allowed to end when the ENTER key is pressed
210
If you allow the Resignfirstresponder method to be called, this causes the end of the edit, and the keyboard will be closed [TextField Resignfirstresponder];
211
Check out the meaning of the word resign to understand this method.
212
return YES;
213
}
214

215

216

217
Notice
218
Uitextfield is derived from Uicontrol, so the notification system in the Uicontrol class can also be used in a text field. In addition to the standard events of the Uicontrol class, you can also use the following Uitextfield class-specific events
219

220
Uitextfieldtextdidbegineditingnotification
221
Uitextfieldtextdidchangenotification
222
Uitextfieldtextdidendeditingnotification
223
triggered when the text field exits edit mode. The object property of the notification stores the final text.
224

225
Because text fields use the keyboard to enter text, action notifications are also sent when the following events occur
226

227
Uikeyboardwillshownotification//keyboard display before sending
228
Uikeyboarddidshownotification//keyboard display after send
229
Uikeyboardwillhidenotification//keyboard hidden before sending
230
Uikeyboarddidhidenotification//keyboard hidden after sending
231

232
1. Text: Sets the default text for the text box.
233
2, Placeholder: Can be displayed in the text box gray words, to prompt the user should be in this text box to enter what content. When you enter data in this text box, the gray words for the hint will automatically disappear.
234
3, Background:
235
4, Disabled: If this item is selected, the user will not be able to change the text box contents.
236
5. Next, there are three buttons to set the alignment.
237
6, Border style: Select the border style.
238
7, Clear button: This is a drop-down menu, you can choose to clear when the button appears, the so-called Clear button is a text box to the right of the small X, you can have the following options:
239
7.1 Never appears: Never appears
240
7.2 appears while editing: appears when editing
241
7.3 appears unless editing:
242
7.4 is always visible:
243
8, clear when editing begins: If this item is selected, the previous contents of the text box will be erased when you start editing the text box. For example, you now enter the text box a "what", then to edit the text box B, if you come back to edit the text box A, then the "what" will be immediately cleared.
244
9. Text color: Sets the colors of the text in the text box.
245
10. Font: Sets the font and font size of the text.
246
11, min Font Size: Sets the minimum font that the text box can display (but I don't feel like it)
247
12. Adjust to Fit: Specifies whether the text in the text box also shrinks when the size of the text box decreases. Select it to make all text visible, even if the text is long. However, this option is used with the Min font size, and the text shrinks, not less than the set min font size.
248
The next sections are used to set how the keyboard is displayed.
249
13, Captitalization: Set uppercase. There are four options in the drop-down menu:
250
13.1 None: Uppercase not set
251
13.2 Words: Capitalize the first letter of each word, where the word refers to a string separated by a space
252
13.3 sentances: The first letter of each sentence is capitalized, and the sentence here is a string separated by a period plus a space
253
13.4 All characters: so uppercase letters
254
14, Correction: Check spelling, default is yes.
255
15, Keyboard: Select the keyboard type, such as full numbers, letters and numbers.
256
16, Appearance:
257
17, Return key: Select the Return key, you can choose Search, return, done, and so on.
258
18, Auto-enable Return key: If selected, the keyboard's return key is valid only if at least one character is entered in the text box.
259
19. Secure: When your text box is used as a password input box, you can select this option, at which point the characters are displayed as asterisks.
260

261

262
1.Alignment Horizontal Horizontal alignment
263
2.Alignment Vertical Vertical Alignment
264
3. Returns a bool value if the input box is Selected (checked) Enabled (available) highlighted (highlighted)
265

266
Restrict input of only specific characters
267

268
(BOOL) TextField: (Uitextfield *) TextField Shouldchangecharactersinrange: (nsrange) Range replacementstring: (nsstring *) string{
269
Nscharacterset *cs;
270
CS = [[Nscharacterset charactersetwithcharactersinstring:numbers]invertedset];
271

272
NSString *filtered = [[string componentsseparatedbycharactersinset:cs]componentsjoinedbystring:@ "]; Separate groups by CS, array separated by @ ""
273

274
BOOL canchange = [string isequaltostring:filtered];
275

276
return canchange;
277
}
278

279
The numbers above is a macro that can be defined at the top of the file:
280
#define NUMBERS @ "0123456789n" (this representative can enter numbers and line breaks, please note that this n, if you do not write this, the Done button will not be triggered, if used in Searchbar, will not trigger the search event, because you do not have to restrict the input n , so miserable that I found it in the project. )
281
So, if you want to restrict the input of English and numbers, you can define this as:
282
#define Kalphanum @ "Abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789″.
283
Of course, you can also make a hint before the above method return, such as prompting the user to enter only numbers. If you feel the need.
284

285
Restrict the input of only a certain length of characters
286

287
-(BOOL) TextField: (Uitextfield *) TextField Shouldchangecharactersinrange: (nsrange) range replacementstring: ( NSString *) string;
288
{//string is the character entered at this time TextField is the input box that is being entered returns Yes to change the value of the input box no opposite
289
 
290
if ([string isequaltostring:@ "n"])//press passing to change
291
{
292
return YES;
293
}
294

295
NSString * tobestring = [Textfield.text stringbyreplacingcharactersinrange:range withstring:string]; Get the contents of the input box
296

297
if (Self.mytextfield = = TextField)//Determine if we want to limit the input box
298
{
299
if ([tobestring length] > 20) {//Popup warning if input box content is greater than 20
300
Textfield.text = [tobestring substringtoindex:20];
301
Uialertview *alert = [[[Uialertview alloc] Initwithtitle:nil message:@ "The maximum number of words cannot be entered" Delegate:nil cancelbuttontitle:@ " OK "otherbuttontitles:nil, nil] autorelease];
302
[Alert show];
303
return NO;
304
}
305
}
306
return YES;
307
}

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.