UILabel属性
1 #import "LabelTestViewController.h"
2
3 @implementation LabelTestViewController
4
5 /*
6
7 Accessing the Text Attributes
8
9 text property
10
11 font property
12
13 textColor property
14
15 textAlignment property
16
17 lineBreakMode property
18
19 enabled property
20
21 Sizing the Label’s Text
22
23 adjustsFontSizeToFitWidth property
24
25 baselineAdjustment property
26
27 minimumFontSize property 无例
28
29 numberOfLines property
30
31 Managing Highlight Values
32
33 highlightedTextColor property
34
35 highlighted property
36
37 Drawing a Shadow
38
39 shadowColor property
40
41 shadowOffset property
42
43 Drawing and Positioning Overrides
44
45 – textRectForBounds:limitedToNumberOfLines: 无例
46
47 – drawTextInRect: 无例
48
49 Setting and Getting Attributes
50
51 userInteractionEnabled property
52
53 */
54
55 // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
56
57 - (void)viewDidLoad {
58
59 UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake(50.0, 20.0, 200.0, 50.0)];
60
61 UILabel *label2 = [[UILabel alloc]initWithFrame:CGRectMake(50.0, 80.0, 200.0, 50.0)];
62
63 UILabel *label3 = [[UILabel alloc]initWithFrame:CGRectMake(50.0, 140.0, 200.0, 50.0)];
64
65 UILabel *label4 = [[UILabel alloc]initWithFrame:CGRectMake(50.0, 200.0, 200.0, 50.0)];
66
67 UILabel *label5 = [[UILabel alloc]initWithFrame:CGRectMake(50.0, 260.0, 200.0, 50.0)];
68
69 UILabel *label6 = [[UILabel alloc]initWithFrame:CGRectMake(50.0, 320.0, 200.0, 50.0)];
70
71 UILabel *label7 = [[UILabel alloc]initWithFrame:CGRectMake(50.0, 380.0, 200.0, 50.0)];
72
73 //设置显示文字
74
75 label1.text = @"label1";
76
77 label2.text = @"label2";
78
79 label3.text = @"label3--label3--label3--label3--label3--label3--label3--label3--label3--label3--label3--";
80
81 label4.text = @"label4--label4--label4--label4--";
82
83 label5.text = @"label5--label5--label5--label5--label5--label5--";
84
85 label6.text = @"label6";
86
87 label7.text = @"label7";
88
89 //设置字体:粗体,正常的是 SystemFontOfSize
90
91 label1.font = [UIFont boldSystemFontOfSize:20];
92
93 //设置文字颜色
94
95 label1.textColor = [UIColor orangeColor];
96
97 label2.textColor = [UIColor purpleColor];
98
99 //设置文字位置
100
101 label1.textAlignment = UITextAlignmentRight;
102
103 label2.textAlignment = UITextAlignmentCenter;
104
105 //设置字体大小适应label宽度
106
107 label4.adjustsFontSizeToFitWidth = YES;
108
109
110
111 //设置label的行数
112
113 label5.numberOfLines = 2;
114
115 UIlabel.backgroudColor=[UIColor clearColor]; //可以去掉背景色
116
117
118
119 //设置高亮
120
121 label6.highlighted = YES;
122
123 label6.highlightedTextColor = [UIColor orangeColor];
124
125 //设置阴影
126
127 label7.shadowColor = [UIColor redColor];
128
129 label7.shadowOffset = CGSizeMake(1.0,1.0);
130
131 //设置是否能与用户进行交互
132
133 label7.userInteractionEnabled = YES;
134
135 //设置label中的文字是否可变,默认值是YES
136
137 label3.enabled = NO;
138
139 //设置文字过长时的显示格式
140
141 label3.lineBreakMode = UILineBreakModeMiddleTruncation;//截去中间
142
143 // typedef enum {
144
145 // UILineBreakModeWordWrap = 0,
146
147 // UILineBreakModeCharacterWrap,
148
149 // UILineBreakModeClip,//截去多余部分
150
151 // UILineBreakModeHeadTruncation,//截去头部
152
153 // UILineBreakModeTailTruncation,//截去尾部
154
155 // UILineBreakModeMiddleTruncation,//截去中间
156
157 // } UILineBreakMode;
158
159 //如果adjustsFontSizeToFitWidth属性设置为YES,这个属性就来控制文本基线的行为
160
161 label4.baselineAdjustment = UIBaselineAdjustmentNone;
162
163 // typedef enum {
164
165 // UIBaselineAdjustmentAlignBaselines,
166
167 // UIBaselineAdjustmentAlignCenters,
168
169 // UIBaselineAdjustmentNone,
170
171 // } UIBaselineAdjustment;
172
173 [self.view addSubview:label1];
174
175 [self.view addSubview:label2];
176
177 [self.view addSubview:label3];
178
179 [self.view addSubview:label4];
180
181 [self.view addSubview:label5];
182
183 [self.view addSubview:label6];
184
185 [self.view addSubview:label7];
186
187 [label1 release];
188
189 [label2 release];
190
191 [label3 release];
192
193 [label4 release];
194
195 [label5 release];
196
197 [label6 release];
198
199 [label7 release];
200
201 [super viewDidLoad];
202
203 }
204
205 /*
206
207 // Override to allow orientations other than the default portrait orientation.
208
209 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
210
211 // Return YES for supported orientations
212
213 return (interfaceOrientation == UIInterfaceOrientationPortrait);
214
215 }
216
217 */
218
219 - (void)didReceiveMemoryWarning {
220
221 // Releases the view if it doesn't have a superview.
222
223 [super didReceiveMemoryWarning];
224
225 // Release any cached data, images, etc that aren't in use.
226
227 }
228
229 - (void)viewDidUnload {
230
231 // Release any retained subviews of the main view.
232
233 // e.g. self.myOutlet = nil;
234
235 }
236
237 - (void)dealloc {
238
239 [super dealloc];
240
241 }
242
243 @end