1 UILabel
2
3 UILabel继承了UIView,它可以设置UIView所支持的属性。
4
5 UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake(50.0, 20.0, 200.0, 50.0)]; //设置Label的位置和大小
6 //设置显示文字
7 label1.text = @"用户名";
8 //设置字体:粗体,正常的是 SystemFontOfSize
9 label1.font = [UIFont boldSystemFontOfSize:20];
10 //设置文字颜色
11 label1.textColor = [UIColor orangeColor];
12 //设置文字位置
13 label1.textAlignment = UITextAlignmentRight;
14 label2.textAlignment = UITextAlignmentCenter;
15 //设置字体大小适应label宽度
16 label4.adjustsFontSizeToFitWidth = YES;
17 //设置label的行数
18 label5.numberOfLines = 2;
19 UIlabel.backgroudColor=[UIColor clearColor]; //可以去掉背景色
20
21 //设置高亮
22 label6.highlighted = YES;
23 label6.highlightedTextColor = [UIColor orangeColor];
24 //设置阴影
25 label7.shadowColor = [UIColor redColor];
26 label7.shadowOffset = CGSizeMake(1.0,1.0);
27 //设置是否能与用户进行交互
28 label7.userInteractionEnabled = YES;
29 //设置label中的文字是否可变,默认值是YES
30 label3.enabled = NO;
31 //设置文字过长时的显示格式
32 label3.lineBreakMode = UILineBreakModeMiddleTruncation;//截去中间
33 // typedef enum {
34 // UILineBreakModeWordWrap = 0,
35 // UILineBreakModeCharacterWrap,
36 // UILineBreakModeClip,//截去多余部分
37 // UILineBreakModeHeadTruncation,//截去头部
38 // UILineBreakModeTailTruncation,//截去尾部
39 // UILineBreakModeMiddleTruncation,//截去中间
40 // } UILineBreakMode;
41
42 //如果adjustsFontSizeToFitWidth属性设置为YES,这个属性就来控制文本基线的行为
43 label4.baselineAdjustment = UIBaselineAdjustmentNone;
44 // typedef enum {
45 // UIBaselineAdjustmentAlignBaselines,
46 // UIBaselineAdjustmentAlignCenters,
47 // UIBaselineAdjustmentNone,
48 // } UIBaselineAdjustment;
49
50
51 有时需要设置UILabel中文本的行数,其属性值默认为1,用于设置该UILabel只能显示一行文本。
52
53 oldPasswordLabel.numberOfLines = 2;
54
55
56 UITextField
57
58 //初始化textfield并设置位置及大小
59 UITextField *text = [[UITextField alloc]initWithFrame:CGRectMake(20, 20, 130, 30)];
60
61 //当输入框没有内容时,水印提示 ,提示内容为“用户名”
62 //显示灰色字体,作为提示信息
63 text.placeholder = @"用户名";
64
65 //设置边框样式,只有设置了才会显示边框样式
66 text.borderStyle = UITextBorderStyleRoundedRect;
67 typedef enum {
68 UITextBorderStyleNone,
69 UITextBorderStyleLine,
70 UITextBorderStyleBezel,
71 UITextBorderStyleRoundedRect
72 } UITextBorderStyle;
73
74 //设置键盘的样式
75 text.keyboardType = UIKeyboardTypeNumberPad;
76 typedef enum {
77 UIKeyboardTypeDefault, //默认键盘,支持所有字符
78 UIKeyboardTypeASCIICapable, //支持ASCII的默认键盘
79 UIKeyboardTypeNumbersAndPunctuation, //标准电话键盘,支持+*#字符
80 UIKeyboardTypeURL, //URL键盘,支持.com按钮 只支持URL字符
81 UIKeyboardTypeNumberPad, //数字键盘
82 UIKeyboardTypePhonePad, //电话键盘
83 UIKeyboardTypeNamePhonePad, //电话键盘,也支持输入人名
84 UIKeyboardTypeEmailAddress, //用于输入电子 邮件地址的键盘
85 UIKeyboardTypeDecimalPad, //数字键盘 有数字和小数点
86 UIKeyboardTypeTwitter, //优化的键盘,方便输入@、#字符
87 UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable,
88 } UIKeyboardType;
89
90 //每输入一个字符就变成点 用语密码输入
91 text.secureTextEntry = YES;
92
93 //设置输入框的背景颜色,此时设置为白色 如果使用了自定义的背景图片边框会被忽略掉
94 text.backgroundColor = [UIColor whiteColor];
95
96 //设置背景图片
97 text.background = [UIImage imageNamed:@"dd.png"];
98
99 //设置背景
100 text.disabledBackground = [UIImage imageNamed:@"cc.png"];
101
102
103 //设置输入框内容的字体样式和大小
104 text.font = [UIFont fontWithName:@"Arial" size:20.0f];
105
106 //设置字体颜色
107 text.textColor = [UIColor redColor];
108
109 //输入框中是否有个叉号,在什么时候显示,用于一次性删除输入框中的内容
110 text.clearButtonMode = UITextFieldViewModeAlways;
111 typedef enum {
112 UITextFieldViewModeNever, //从不出现
113 UITextFieldViewModeWhileEditing, //编辑时出现
114 UITextFieldViewModeUnlessEditing, //除了编辑外都出现
115 UITextFieldViewModeAlways //一直出现
116 } UITextFieldViewMode;
117
118 //输入框中一开始就有的文字
119 text.text = @"一开始就在输入框的文字";
120
121 //是否纠错
122 text.autocorrectionType = UITextAutocorrectionTypeNo;
123 typedef enum {
124 UITextAutocorrectionTypeDefault, //默认
125 UITextAutocorrectionTypeNo, //不自动纠错
126 UITextAutocorrectionTypeYes, //自动纠错
127 } UITextAutocorrectionType;
128
129 //再次编辑就清空
130 text.clearsOnBeginEditing = YES;
131
132 //内容对齐方式
133 text.textAlignment = UITextAlignmentLeft;
134
135 //内容的垂直对齐方式 UITextField继承自UIControl,此类中有一个属性contentVerticalAlignment
136 text.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
137
138 //设置为YES时文本会自动缩小以适应文本窗口大小.默认是保持原来大小,而让长文本滚动
139 textFied.adjustsFontSizeToFitWidth = YES;
140
141 //设置自动缩小显示的最小字体大小
142 text.minimumFontSize = 20;
143
144 //首字母是否大写
145 text.autocapitalizationType = UITextAutocapitalizationTypeNone;
146
147 typedef enum {
148 UITextAutocapitalizationTypeNone, 不自动大写
149 UITextAutocapitalizationTypeWords, 单词首字母大写
150 UITextAutocapitalizationTypeSentences, 句子的首字母大写
151 UITextAutocapitalizationTypeAllCharacters, 所有字母都大写
152 } UITextAutocapitalizationType;
153
154 //return键变成什么键
155 text.returnKeyType =UIReturnKeyDone;
156
157 typedef enum {
158 UIReturnKeyDefault, //默认 灰色按钮,标有Return
159 UIReturnKeyGo, //标有Go的蓝色按钮
160 UIReturnKeyGoogle, //标有Google的蓝色按钮,用语搜索
161 UIReturnKeyJoin, //标有Join的蓝色按钮
162 UIReturnKeyNext, //标有Next的蓝色按钮
163 UIReturnKeyRoute, //标有Route的蓝色按钮
164 UIReturnKeySearch, //标有Search的蓝色按钮
165 UIReturnKeySend, //标有Send的蓝色按钮
166 UIReturnKeyYahoo, //标有Yahoo的蓝色按钮
167 UIReturnKeyYahoo, //标有Yahoo的蓝色按钮
168 UIReturnKeyEmergencyCall, //紧急呼叫按钮
169 } UIReturnKeyType;
170
171 //键盘外观
172 textView.keyboardAppearance=UIKeyboardAppearanceDefault;
173 typedef enum {
174 UIKeyboardAppearanceDefault, //默认外观,浅灰色
175 UIKeyboardAppearanceAlert, //深灰 石墨色
176
177 } UIReturnKeyType;
178
179
180 //设置代理 用于实现协议
181 text.delegate = self;
182
183 //把textfield加到视图中
184 [self.window addSubview:text];
185
186 //最右侧加图片是以下代码 左侧类似
187 UIImageView *image=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"right.png"]];
188 text.rightView=image;
189 text.rightViewMode = UITextFieldViewModeAlways;
190 typedef enum {
191 UITextFieldViewModeNever,
192 UITextFieldViewModeWhileEditing,
193 UITextFieldViewModeUnlessEditing,
194 UITextFieldViewModeAlways
195 } UITextFieldViewMode;
196
197
198 在处理密码等隐私类的信息时,可能需要将输入的信息隐藏一下。
199
200 //每输入一个字符就变成点 ,用语密码输入
201 [passwordTextField setSecureTextEntry:YES];
202
203
204 也可以设置文本框关联的键盘,如下:
205
206 //设置键盘的样式
207 text.keyboardType = UIKeyboardTypeNumberPad;
208 typedef enum {
209 UIKeyboardTypeDefault, //默认键盘,支持所有字符
210 UIKeyboardTypeASCIICapable, //支持ASCII的默认键盘
211 UIKeyboardTypeNumbersAndPunctuation, //标准电话键盘,支持+*#字符
212 UIKeyboardTypeURL, //URL键盘,支持.com按钮 只支持URL字符
213 UIKeyboardTypeNumberPad, //数字键盘
214 UIKeyboardTypePhonePad, //电话键盘
215 UIKeyboardTypeNamePhonePad, //电话键盘,也支持输入人名
216 UIKeyboardTypeEmailAddress, //用于输入电子 邮件地址的键盘
217 UIKeyboardTypeDecimalPad, //数字键盘 有数字和小数点
218 UIKeyboardTypeTwitter, //优化的键盘,方便输入@、#字符
219 UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable,
220 } UIKeyboardType;
221
222
223 有时需要限制输入文本的长度,这类操作也非常普遍和重要。
224
225 //限制输入文本的长度
226 - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
227 {
228 if ([textField.text length] > MAXLENGTH)
229 {
230 textField.text = [textField.text substringToIndex:MAXLENGTH-1];
231 return NO;
232 }
233 return YES;
234 }
235
236
237 UIButton
238
239 UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
240 // 能够定义的button类型有以下6种,
241 // typedef enum {
242 // UIButtonTypeCustom = 0, 自定义风格
243 // UIButtonTypeRoundedRect, 圆角矩形
244 // UIButtonTypeDetailDisclosure, 蓝色小箭头按钮,主要做详细说明用
245 // UIButtonTypeInfoLight, 亮色感叹号
246 // UIButtonTypeInfoDark, 暗色感叹号
247 // UIButtonTypeContactAdd, 十字加号按钮
248 // } UIButtonType;
249
250 //给定button在view上的位置
251 button1.frame = CGRectMake(20, 20, 280, 20);
252
253 //button背景色
254 button1.backgroundColor = [UIColor clearColor];
255
256 //设置button填充图片
257 //[button1 setImage:[UIImage imageNamed:@"btng.png"] forState:UIControlStateNormal];
258
259 //设置button标题
260 [button1 setTitle:@"点击" forState:UIControlStateNormal];
261 /* forState: 这个参数的作用是定义按钮的文字或图片在何种状态下才会显现*/
262 //以下是几种状态
263 // enum {
264 // UIControlStateNormal = 0, 常规状态显现
265 // UIControlStateHighlighted = 1 << 0, 高亮状态显现
266 // UIControlStateDisabled = 1 << 1, 禁用的状态才会显现
267 // UIControlStateSelected = 1 << 2, 选中状态
268 // UIControlStateApplication = 0x00FF0000, 当应用程序标志时
269 // UIControlStateReserved = 0xFF000000 为内部框架预留,可以不管他
270 // };
271
272 /*
273 * 默认情况下,当按钮高亮的情况下,图像的颜色会被画深一点,如果这下面的这个属性设置为no,
274 * 那么可以去掉这个功能
275 */
276 button1.adjustsImageWhenHighlighted = NO;
277 /*跟上面的情况一样,默认情况下,当按钮禁用的时候,图像会被画得深一点,设置NO可以取消设置*/
278 button1.adjustsImageWhenDisabled = NO;
279 /* 下面的这个属性设置为yes的状态下,按钮按下会发光*/
280 button1.showsTouchWhenHighlighted = YES;
281
282 /* 给button添加事件,事件有很多种,我会单独开一篇博文介绍它们,下面这个时间的意思是
283 按下按钮,并且手指离开屏幕的时候触发这个事件,跟web中的click事件一样。
284 触发了这个事件以后,执行butClick:这个方法,addTarget:self 的意思是说,这个方法在本类中
285 也可以传入其他类的指针*/
286 [button1 addTarget:self action:@selector(butClick:) forControlEvents:UIControlEventTouchUpInside];
287
288 //显示控件
289 [self.view addSubview:button1];