Iphone开发UILable和UITextField

本文详细介绍 UILabel 的初始化及详细参数设置,包括文字显示、字体样式、颜色等,并深入探讨 UITextField 的初始化及其参数,如边框风格、自动更正、占位符、键盘行为等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1)初始化UILabel

1
2
3
4
5
6
7
8
9
10
11
12
13
UILabel *scoreLabel = [ [UILabel alloc ] initWithFrame:CGRectMake((self.bounds.size.width / 2), 0.0, 150.0, 43.0) ];
<wbr></wbr>
scoreLabel.textAlignment =<wbr>UITextAlignmentCenter;</wbr>
<wbr></wbr>
scoreLabel.text = @ "我是Andy--清风" ;
<wbr></wbr>
scoreLabel.textColor = [UIColorwhiteColor];
<wbr></wbr>
scoreLabel.backgroundColor = [UIColor blackColor];
<wbr></wbr>
scoreLabel.font = [UIFont fontWithName:@ "Arial Rounded MT Bold" size:(36.0)];
<wbr></wbr>
[self addSubview:scoreLabel];

(2)详细参数解释:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
//设置显示文字<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr>
<wbr></wbr>
<wbr><wbr><wbr></wbr></wbr></wbr> scoreLabel.text = @ "我是Andy--清风" ;<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr>
<wbr></wbr>
<wbr></wbr><wbr></wbr>
<wbr></wbr>
//设置字体:粗体,正常的是 SystemFontOfSize,调用系统的字体配置<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr>
<wbr></wbr>
<wbr><wbr><wbr></wbr></wbr></wbr> scoreLabel.font = [UIFont boldSystemFontOfSize:20];<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr>
<wbr></wbr>
<wbr></wbr><wbr></wbr>
<wbr></wbr>
//设置文字颜色,可以有多种颜色可以选择
<wbr></wbr>
<wbr><wbr><wbr></wbr></wbr></wbr> scoreLabel.textColor = [UIColor orangeColor];<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr>
<wbr></wbr>
<wbr><wbr><wbr></wbr></wbr></wbr> scoreLabel.textColor = [UIColor purpleColor];<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr>
<wbr></wbr>
<wbr></wbr><wbr></wbr>
<wbr></wbr>
//设置文字对齐位置,居左,居中,居右<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr>
<wbr></wbr>
<wbr><wbr><wbr></wbr></wbr></wbr> scoreLabel.textAlignment = UITextAlignmentRight;<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr>
<wbr></wbr>
<wbr><wbr><wbr></wbr></wbr></wbr> scoreLabel.textAlignment = UITextAlignmentCenter;<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr>
<wbr></wbr>
//设置字体大小是否适应label宽度<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr>
<wbr></wbr>
<wbr><wbr><wbr></wbr></wbr></wbr> scoreLabel.adjustsFontSizeToFitWidt<wbr>h = YES;<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr>
<wbr></wbr>
<wbr></wbr><wbr></wbr>
<wbr></wbr>
//设置label的行数,这个可以根据上节的UITextView自适应高度<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr>
<wbr></wbr>
<wbr><wbr><wbr></wbr></wbr></wbr> scoreLabel.numberOfLines = 2;<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr>
<wbr></wbr>
<wbr></wbr><wbr></wbr>
<wbr></wbr>
<wbr></wbr> //设置文本是否高亮和高亮时的颜色<wbr><wbr></wbr></wbr>
<wbr></wbr>
<wbr><wbr><wbr></wbr></wbr></wbr> scoreLabel.highlighted = YES;<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr>
<wbr></wbr>
<wbr><wbr><wbr></wbr></wbr></wbr> scoreLabel.highlightedTextColor = [UIColor orangeColor];<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr>
<wbr></wbr>
<wbr></wbr><wbr></wbr>
<wbr></wbr>
//设置阴影的颜色和阴影的偏移位置<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr>
<wbr></wbr>
<wbr><wbr><wbr></wbr></wbr></wbr> scoreLabel.shadowColor = [UIColor redColor];<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr>
<wbr></wbr>
<wbr><wbr><wbr></wbr></wbr></wbr> scoreLabel.shadowOffset = CGSizeMake(1.0,1.0);<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr>
<wbr></wbr>
<wbr></wbr><wbr></wbr>
<wbr></wbr>
//设置是否能与用户进行交互<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr>
<wbr></wbr>
<wbr><wbr><wbr></wbr></wbr></wbr> scoreLabel.userInteractionEnabled = YES;<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr>
<wbr></wbr>
<wbr></wbr><wbr></wbr>
<wbr></wbr>
<wbr></wbr> //设置label中的文字是否可变,默认值是YES<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr>
<wbr></wbr>
<wbr><wbr><wbr></wbr></wbr></wbr> scoreLabel.enabled = NO;<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr>
<wbr></wbr>
<wbr></wbr><wbr></wbr>
<wbr></wbr>
//设置文字过长时的显示格式<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr>
<wbr></wbr>
<wbr><wbr><wbr></wbr></wbr></wbr> scoreLabel.lineBreakMode = UILineBreakModeMiddleTru<wbr>ncation;</wbr> //截去中间
在定义里面允许有以下格式显示:
<wbr><div> <div> <table border="0" cellpadding="0" cellspacing="0" style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; line-height:normal"><tbody><tr> <td style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; font-size:12px; font-family:Verdana,宋体,sans-serif; line-height:18px"> <div>1</div> <div>2</div> <div>3</div> <div>4</div> <div>5</div> <div>6</div> <div>7</div> <div>8</div> <div>9</div> <div>10</div> <div>11</div> <div>12</div> <div>13</div> <div>14</div> <div>15</div> </td> <td style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; font-size:12px; font-family:Verdana,宋体,sans-serif; line-height:18px"> <div> <div> <code>typedef</code><code>enum</code><code>{<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></code> </div> <div><wbr></wbr></div> <div> <code><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></code><code>UILineBreakModeWordWrap = 0,<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></code> </div> <div><wbr></wbr></div> <div> <code><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></code><code>UILineBreakModeCharacter<wbr>Wrap,<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></code> </div> <div><wbr></wbr></div> <div> <code><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></code><code>UILineBreakModeClip,</code><code>//截去多余部分<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></code> </div> <div><wbr></wbr></div> <div> <code><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></code><code>UILineBreakModeHeadTrunc<wbr>ation,</wbr></code><code>//截去头部<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></code> </div> <div><wbr></wbr></div> <div> <code><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></code><code>UILineBreakModeTailTrunc<wbr>ation,</wbr></code><code>//截去尾部<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></code> </div> <div><wbr></wbr></div> <div> <code><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></code><code>UILineBreakModeMiddleTru<wbr>ncation,</wbr></code><code>//截去中间<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></code> </div> <div><wbr></wbr></div> <div> <code><wbr></wbr></code><code>} UILineBreakMode;</code> </div> </div> </td> </tr></tbody></table> </div> </div> 如果adjustsFontSizeToFitWidt<wbr>h属性设置为YES,这个属性就来控制文本基线的行为 <div> <div> <table border="0" cellpadding="0" cellspacing="0" style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; line-height:normal"><tbody><tr> <td style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; font-size:12px; font-family:Verdana,宋体,sans-serif; line-height:18px"> <div>1</div> </td> <td style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; font-size:12px; font-family:Verdana,宋体,sans-serif; line-height:18px"> <div> <div><code>scoreLabel.baselineAdjustment = UIBaselineAdjustmentNone<wbr>;</wbr></code></div> </div> </td> </tr></tbody></table> </div> </div> 在定义里面允许有以下格式显示: <div> <div> <table border="0" cellpadding="0" cellspacing="0" style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; line-height:normal"><tbody><tr> <td style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; font-size:12px; font-family:Verdana,宋体,sans-serif; line-height:18px"> <div>1</div> <div>2</div> <div>3</div> <div>4</div> <div>5</div> <div>6</div> <div>7</div> <div>8</div> <div>9</div> </td> <td style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; font-size:12px; font-family:Verdana,宋体,sans-serif; line-height:18px"> <div> <div> <code>typedef</code><code>enum</code><code>{<wbr><wbr><wbr></wbr></wbr></wbr></code> </div> <div><wbr></wbr></div> <div> <code><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></code><code>UIBaselineAdjustmentAlig<wbr>nBaselines,<wbr><wbr><wbr></wbr></wbr></wbr></wbr></code> </div> <div><wbr></wbr></div> <div> <code><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></code><code>UIBaselineAdjustmentAlig<wbr>nCenters,<wbr><wbr><wbr></wbr></wbr></wbr></wbr></code> </div> <div><wbr></wbr></div> <div> <code><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></code><code>UIBaselineAdjustmentNone<wbr>,<wbr><wbr><wbr></wbr></wbr></wbr></wbr></code> </div> <div><wbr></wbr></div> <div><code>} UIBaselineAdjustment;</code></div> </div> </td> </tr></tbody></table> </div> </div> 设置背景色为透明<span style="word-wrap:normal; word-break:normal; line-height:21px; text-decoration:underline"><br></span> <p style="margin-top:0px; margin-right:0px; margin-bottom:5px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; list-style-type:none; list-style-position:initial; word-wrap:normal; word-break:normal; line-height:21px"> <code>scoreLabel.backgroudColor=[UIColor clearColor];</code></p> <p style="margin-top:0px; margin-right:0px; margin-bottom:5px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; list-style-type:none; list-style-position:initial; word-wrap:normal; word-break:normal; line-height:21px"> 之外你还可以使用自定义的颜色:</p> <p style="margin-top:0px; margin-right:0px; margin-bottom:5px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; list-style-type:none; list-style-position:initial; word-wrap:normal; word-break:normal; line-height:21px"> <code>UIColor *color = [UIColor colorWithRed:1.0f green:50.0f blue:0.0f alpha:1.0f];</code></p> <div> <table style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; line-height:normal"><tbody><tr> <td style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; font-size:12px; font-family:Verdana,宋体,sans-serif; line-height:18px"> <code>scoreLabel.textColor = [UIColor color];</code> </td> </tr></tbody></table> </div> <p style="margin-top:0px; margin-right:0px; margin-bottom:5px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; list-style-type:none; list-style-position:initial; word-wrap:normal; word-break:normal; line-height:21px"> <code>//UIColor 里的 RGB 值是CGFloat类型的在0~1范围内,对应0~255的颜色值范围。</code></p> <p style="margin-top:0px; margin-right:0px; margin-bottom:5px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; list-style-type:none; list-style-position:initial; word-wrap:normal; word-break:normal; line-height:21px"> 二、UITextField</p> <p style="margin-top:0px; margin-right:0px; margin-bottom:5px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; list-style-type:none; list-style-position:initial; word-wrap:normal; word-break:normal; line-height:21px"> (1)初始化UITextField</p> <div> <div> <table border="0" cellpadding="0" cellspacing="0" style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; line-height:normal"><tbody><tr> <td style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; font-size:12px; font-family:Verdana,宋体,sans-serif; line-height:18px"> <div>1</div> <div>2</div> <div>3</div> <div>4</div> <div>5</div> <div>6</div> <div>7</div> <div>8</div> <div>9</div> <div>10</div> <div>11</div> <div>12</div> <div>13</div> <div>14</div> <div>15</div> <div>16</div> <div>17</div> </td> <td style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; font-size:12px; font-family:Verdana,宋体,sans-serif; line-height:18px"> <div> <div><code>UITextField* text = [[UITextField alloc] initWithFrame:CGRectMake(10, 50, 300, 30)];</code></div> <div> <code><wbr></wbr></code><wbr></wbr> </div> <div> <code><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></code><code>text.borderStyle = UITextBorderStyleRounded<wbr>Rect;</wbr></code> </div> <div> <code><wbr></wbr></code><wbr></wbr> </div> <div> <code><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></code><code>text.autocorrectionType = UITextAutocorrectionType<wbr>Yes;</wbr></code> </div> <div> <code><wbr></wbr></code><wbr></wbr> </div> <div> <code><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></code><code>text.placeholder = @</code><code>"您好,我是Andy—清风"</code><code>;</code> </div> <div> <code><wbr></wbr></code><wbr></wbr> </div> <div> <code><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></code><code>text.returnKeyType = UIReturnKeyDone;</code> </div> <div> <code><wbr></wbr></code><wbr></wbr> </div> <div> <code><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></code><code>text.clearButtonMode = UITextFieldViewModeWhile<wbr>Editing;</wbr></code> </div> <div> <code><wbr></wbr></code><wbr></wbr> </div> <div> <code><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></code><code>[text setBackgroundColor:[UIColor whiteColor]];</code> </div> <div> <code><wbr></wbr></code><wbr></wbr> </div> <div> <code><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></code><code>text.delegate = self;</code> </div> <div> <code><wbr></wbr></code><wbr></wbr> </div> <div> <code><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></code><code>[self.view addSubview:text];</code> </div> </div> </td> </tr></tbody></table> </div> </div> (2)详细参数解释 <p style="margin-top:0px; margin-right:0px; margin-bottom:5px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; list-style-type:none; list-style-position:initial; word-wrap:normal; word-break:normal; line-height:21px"> borderStyle:文本框的边框风格</p> <p style="margin-top:0px; margin-right:0px; margin-bottom:5px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; list-style-type:none; list-style-position:initial; word-wrap:normal; word-break:normal; line-height:21px"> autocorrectionType:可以设置是否启动自动提醒更正功能。</p> <p style="margin-top:0px; margin-right:0px; margin-bottom:5px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; list-style-type:none; list-style-position:initial; word-wrap:normal; word-break:normal; line-height:21px"> placeholder:设置默认的文本显示</p> <p style="margin-top:0px; margin-right:0px; margin-bottom:5px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; list-style-type:none; list-style-position:initial; word-wrap:normal; word-break:normal; line-height:21px"> returnKeyType:设置键盘完成的按钮</p> <p style="margin-top:0px; margin-right:0px; margin-bottom:5px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; list-style-type:none; list-style-position:initial; word-wrap:normal; word-break:normal; line-height:21px"> backgroundColor:设置背景颜色</p> <p style="margin-top:0px; margin-right:0px; margin-bottom:5px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; list-style-type:none; list-style-position:initial; word-wrap:normal; word-break:normal; line-height:21px"> delegate:设置委托</p> <p style="margin-top:0px; margin-right:0px; margin-bottom:5px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; list-style-type:none; list-style-position:initial; word-wrap:normal; word-break:normal; line-height:21px"> (3)委托方法</p> <p></p> <table border="0" cellpadding="0" cellspacing="0" style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; line-height:normal"><tbody><tr> <td style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; font-size:12px; font-family:Verdana,宋体,sans-serif; line-height:18px"> <div>1</div> <div>2</div> <div>3</div> <div>4</div> <div>5</div> <div>6</div> <div>7</div> <div>8</div> <div>9</div> <div>10</div> <div>11</div> <div>12</div> <div>13</div> <div>14</div> <div>15</div> <div>16</div> <div>17</div> <div>18</div> <div>19</div> </td> <td style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; font-size:12px; line-height:18px"> <div style="font-family:Verdana,宋体,sans-serif"> <div> <code>-(</code><code>void</code><code>)textFieldDidBeginEditing<wbr>:(UITextField *)textField;</wbr></code> </div> <div><wbr></wbr></div> <div><code>//当开始点击textField会调用的方法</code></div> <div><wbr></wbr></div> <div> <code><wbr></wbr></code><wbr></wbr> </div> <div><wbr></wbr></div> <div> <code>-(</code><code>void</code><code>)textFieldDidEndEditing:(UITextField *)textField;</code> </div> <div><wbr></wbr></div> <div><code>//当textField编辑结束时调用的方法</code></div> <div><wbr></wbr></div> <div><code>//按下Done按钮的调用方法,我们让键盘消失</code></div> <div><wbr></wbr></div> <div> <code>-(</code><code>BOOL</code><code>)textFieldShouldReturn:(UITextField *)textField{</code> </div> <div><wbr></wbr></div> <div> <code><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></code><code>[textField resignFirstResponder];</code> </div> <div><wbr></wbr></div> <div> <code><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></code><code>return</code><code>YES;</code> </div> <div><wbr></wbr></div> <div><code>}</code></div> </div> <div><span style="font-family:monospace">密码框显示:xxx.<span style="font-family:Tahoma,'Microsoft Yahei',Simsun; color:#444444; border-collapse:collapse; font-size:14px; line-height:21px">secureTextEntry = yes</span></span></div> </td> </tr></tbody></table> 本文来自:http://blog.sina.com.cn/s/blog_728114610100yglc.html</wbr></wbr>

<wbr><div> <div> <table border="0" cellpadding="0" cellspacing="0" style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; line-height:normal"><tbody><tr> <td style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; font-size:12px; font-family:Verdana,宋体,sans-serif; line-height:18px"> <div>1</div> <div>2</div> <div>3</div> <div>4</div> <div>5</div> <div>6</div> <div>7</div> <div>8</div> <div>9</div> <div>10</div> <div>11</div> <div>12</div> <div>13</div> <div>14</div> <div>15</div> </td> <td style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; font-size:12px; font-family:Verdana,宋体,sans-serif; line-height:18px"> <div> <div> <code>typedef</code><code>enum</code><code>{<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></code> </div> <div><wbr></wbr></div> <div> <code><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></code><code>UILineBreakModeWordWrap = 0,<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></code> </div> <div><wbr></wbr></div> <div> <code><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></code><code>UILineBreakModeCharacter<wbr>Wrap,<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></code> </div> <div><wbr></wbr></div> <div> <code><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></code><code>UILineBreakModeClip,</code><code>//截去多余部分<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></code> </div> <div><wbr></wbr></div> <div> <code><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></code><code>UILineBreakModeHeadTrunc<wbr>ation,</wbr></code><code>//截去头部<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></code> </div> <div><wbr></wbr></div> <div> <code><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></code><code>UILineBreakModeTailTrunc<wbr>ation,</wbr></code><code>//截去尾部<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></code> </div> <div><wbr></wbr></div> <div> <code><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></code><code>UILineBreakModeMiddleTru<wbr>ncation,</wbr></code><code>//截去中间<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></code> </div> <div><wbr></wbr></div> <div> <code><wbr></wbr></code><code>} UILineBreakMode;</code> </div> </div> </td> </tr></tbody></table> </div> </div> 如果adjustsFontSizeToFitWidt<wbr>h属性设置为YES,这个属性就来控制文本基线的行为 <div> <div> <table border="0" cellpadding="0" cellspacing="0" style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; line-height:normal"><tbody><tr> <td style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; font-size:12px; font-family:Verdana,宋体,sans-serif; line-height:18px"> <div>1</div> </td> <td style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; font-size:12px; font-family:Verdana,宋体,sans-serif; line-height:18px"> <div> <div><code>scoreLabel.baselineAdjustment = UIBaselineAdjustmentNone<wbr>;</wbr></code></div> </div> </td> </tr></tbody></table> </div> </div> 在定义里面允许有以下格式显示: <div> <div> <table border="0" cellpadding="0" cellspacing="0" style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; line-height:normal"><tbody><tr> <td style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; font-size:12px; font-family:Verdana,宋体,sans-serif; line-height:18px"> <div>1</div> <div>2</div> <div>3</div> <div>4</div> <div>5</div> <div>6</div> <div>7</div> <div>8</div> <div>9</div> </td> <td style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; font-size:12px; font-family:Verdana,宋体,sans-serif; line-height:18px"> <div> <div> <code>typedef</code><code>enum</code><code>{<wbr><wbr><wbr></wbr></wbr></wbr></code> </div> <div><wbr></wbr></div> <div> <code><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></code><code>UIBaselineAdjustmentAlig<wbr>nBaselines,<wbr><wbr><wbr></wbr></wbr></wbr></wbr></code> </div> <div><wbr></wbr></div> <div> <code><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></code><code>UIBaselineAdjustmentAlig<wbr>nCenters,<wbr><wbr><wbr></wbr></wbr></wbr></wbr></code> </div> <div><wbr></wbr></div> <div> <code><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></code><code>UIBaselineAdjustmentNone<wbr>,<wbr><wbr><wbr></wbr></wbr></wbr></wbr></code> </div> <div><wbr></wbr></div> <div><code>} UIBaselineAdjustment;</code></div> </div> </td> </tr></tbody></table> </div> </div> 设置背景色为透明<span style="word-wrap:normal; word-break:normal; line-height:21px; text-decoration:underline"><br></span> <p style="margin-top:0px; margin-right:0px; margin-bottom:5px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; list-style-type:none; list-style-position:initial; word-wrap:normal; word-break:normal; line-height:21px"> <code>scoreLabel.backgroudColor=[UIColor clearColor];</code></p> <p style="margin-top:0px; margin-right:0px; margin-bottom:5px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; list-style-type:none; list-style-position:initial; word-wrap:normal; word-break:normal; line-height:21px"> 之外你还可以使用自定义的颜色:</p> <p style="margin-top:0px; margin-right:0px; margin-bottom:5px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; list-style-type:none; list-style-position:initial; word-wrap:normal; word-break:normal; line-height:21px"> <code>UIColor *color = [UIColor colorWithRed:1.0f green:50.0f blue:0.0f alpha:1.0f];</code></p> <div> <table style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; line-height:normal"><tbody><tr> <td style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; font-size:12px; font-family:Verdana,宋体,sans-serif; line-height:18px"> <code>scoreLabel.textColor = [UIColor color];</code> </td> </tr></tbody></table> </div> <p style="margin-top:0px; margin-right:0px; margin-bottom:5px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; list-style-type:none; list-style-position:initial; word-wrap:normal; word-break:normal; line-height:21px"> <code>//UIColor 里的 RGB 值是CGFloat类型的在0~1范围内,对应0~255的颜色值范围。</code></p> <p style="margin-top:0px; margin-right:0px; margin-bottom:5px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; list-style-type:none; list-style-position:initial; word-wrap:normal; word-break:normal; line-height:21px"> 二、UITextField</p> <p style="margin-top:0px; margin-right:0px; margin-bottom:5px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; list-style-type:none; list-style-position:initial; word-wrap:normal; word-break:normal; line-height:21px"> (1)初始化UITextField</p> <div> <div> <table border="0" cellpadding="0" cellspacing="0" style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; line-height:normal"><tbody><tr> <td style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; font-size:12px; font-family:Verdana,宋体,sans-serif; line-height:18px"> <div>1</div> <div>2</div> <div>3</div> <div>4</div> <div>5</div> <div>6</div> <div>7</div> <div>8</div> <div>9</div> <div>10</div> <div>11</div> <div>12</div> <div>13</div> <div>14</div> <div>15</div> <div>16</div> <div>17</div> </td> <td style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; font-size:12px; font-family:Verdana,宋体,sans-serif; line-height:18px"> <div> <div><code>UITextField* text = [[UITextField alloc] initWithFrame:CGRectMake(10, 50, 300, 30)];</code></div> <div> <code><wbr></wbr></code><wbr></wbr> </div> <div> <code><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></code><code>text.borderStyle = UITextBorderStyleRounded<wbr>Rect;</wbr></code> </div> <div> <code><wbr></wbr></code><wbr></wbr> </div> <div> <code><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></code><code>text.autocorrectionType = UITextAutocorrectionType<wbr>Yes;</wbr></code> </div> <div> <code><wbr></wbr></code><wbr></wbr> </div> <div> <code><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></code><code>text.placeholder = @</code><code>"您好,我是Andy—清风"</code><code>;</code> </div> <div> <code><wbr></wbr></code><wbr></wbr> </div> <div> <code><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></code><code>text.returnKeyType = UIReturnKeyDone;</code> </div> <div> <code><wbr></wbr></code><wbr></wbr> </div> <div> <code><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></code><code>text.clearButtonMode = UITextFieldViewModeWhile<wbr>Editing;</wbr></code> </div> <div> <code><wbr></wbr></code><wbr></wbr> </div> <div> <code><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></code><code>[text setBackgroundColor:[UIColor whiteColor]];</code> </div> <div> <code><wbr></wbr></code><wbr></wbr> </div> <div> <code><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></code><code>text.delegate = self;</code> </div> <div> <code><wbr></wbr></code><wbr></wbr> </div> <div> <code><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></code><code>[self.view addSubview:text];</code> </div> </div> </td> </tr></tbody></table> </div> </div> (2)详细参数解释 <p style="margin-top:0px; margin-right:0px; margin-bottom:5px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; list-style-type:none; list-style-position:initial; word-wrap:normal; word-break:normal; line-height:21px"> borderStyle:文本框的边框风格</p> <p style="margin-top:0px; margin-right:0px; margin-bottom:5px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; list-style-type:none; list-style-position:initial; word-wrap:normal; word-break:normal; line-height:21px"> autocorrectionType:可以设置是否启动自动提醒更正功能。</p> <p style="margin-top:0px; margin-right:0px; margin-bottom:5px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; list-style-type:none; list-style-position:initial; word-wrap:normal; word-break:normal; line-height:21px"> placeholder:设置默认的文本显示</p> <p style="margin-top:0px; margin-right:0px; margin-bottom:5px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; list-style-type:none; list-style-position:initial; word-wrap:normal; word-break:normal; line-height:21px"> returnKeyType:设置键盘完成的按钮</p> <p style="margin-top:0px; margin-right:0px; margin-bottom:5px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; list-style-type:none; list-style-position:initial; word-wrap:normal; word-break:normal; line-height:21px"> backgroundColor:设置背景颜色</p> <p style="margin-top:0px; margin-right:0px; margin-bottom:5px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; list-style-type:none; list-style-position:initial; word-wrap:normal; word-break:normal; line-height:21px"> delegate:设置委托</p> <p style="margin-top:0px; margin-right:0px; margin-bottom:5px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; list-style-type:none; list-style-position:initial; word-wrap:normal; word-break:normal; line-height:21px"> (3)委托方法</p> <p></p> <table border="0" cellpadding="0" cellspacing="0" style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; line-height:normal"><tbody><tr> <td style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; font-size:12px; font-family:Verdana,宋体,sans-serif; line-height:18px"> <div>1</div> <div>2</div> <div>3</div> <div>4</div> <div>5</div> <div>6</div> <div>7</div> <div>8</div> <div>9</div> <div>10</div> <div>11</div> <div>12</div> <div>13</div> <div>14</div> <div>15</div> <div>16</div> <div>17</div> <div>18</div> <div>19</div> </td> <td style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; font-size:12px; line-height:18px"> <div style="font-family:Verdana,宋体,sans-serif"> <div> <code>-(</code><code>void</code><code>)textFieldDidBeginEditing<wbr>:(UITextField *)textField;</wbr></code> </div> <div><wbr></wbr></div> <div><code>//当开始点击textField会调用的方法</code></div> <div><wbr></wbr></div> <div> <code><wbr></wbr></code><wbr></wbr> </div> <div><wbr></wbr></div> <div> <code>-(</code><code>void</code><code>)textFieldDidEndEditing:(UITextField *)textField;</code> </div> <div><wbr></wbr></div> <div><code>//当textField编辑结束时调用的方法</code></div> <div><wbr></wbr></div> <div><code>//按下Done按钮的调用方法,我们让键盘消失</code></div> <div><wbr></wbr></div> <div> <code>-(</code><code>BOOL</code><code>)textFieldShouldReturn:(UITextField *)textField{</code> </div> <div><wbr></wbr></div> <div> <code><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></code><code>[textField resignFirstResponder];</code> </div> <div><wbr></wbr></div> <div> <code><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></code><code>return</code><code>YES;</code> </div> <div><wbr></wbr></div> <div><code>}</code></div> </div> <div><span style="font-family:monospace">密码框显示:xxx.<span style="font-family:Tahoma,'Microsoft Yahei',Simsun; color:#444444; border-collapse:collapse; font-size:14px; line-height:21px">secureTextEntry = yes</span></span></div> </td> </tr></tbody></table> 本文来自:http://blog.sina.com.cn/s/blog_728114610100yglc.html</wbr></wbr>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值