UILabel
UILabel 继承自 UIView
1.初始化
UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(10, 100, 300, 60)];
CGRectMake(x, y, w, h); 是指定label的位置
2.设置相关属性
-
1、 设置颜色
label.backgroundColor = [UIColor cyanColor];
-
2、 设置文字
label.text = @"GitHubPages";
-
3、 设置字体颜色
label.textColor = [UIColor whiteColor];
-
4、 设置字体大小
label.font = [UIFont systemFontOfSize:30.0];
-
5、 设置字体类型
label.font = [UIFont fontWithName:@"Arial" size:23.0];
-
6、 遍历系统所有字体
label.font = [UIFont systemFontOfSize:30.0];
-
7、 设置字体类型
label.font = [UIFont fontWithName:@"Arial" size:23.0];
-
8 遍历系统所有字体
for (NSString * name in [UIFont familyNames]){ NSLog(@"字体名字:%@",name); } NSLog(@"%@",[UIFont familyNames]);
-
9、 设置文本的显示行数, 换行时以单词换行,,,设置为0后,自动匹配行数
label.numberOfLines = 0;
-
10、 设置文本换行时的截取方式
// NSParagraphStyle 剪切单词的方式 typedef NS_ENUM(NSInteger, NSLineBreakMode) { /* What to do with long lines */ 以空格为边界,保留整个单词 NSLineBreakByWordWrapping = 0, /* Wrap at word boundaries, default */ 保留整个字符 NSLineBreakByCharWrapping, /* Wrap at character boundaries */ 剪切到边界位置,简单剪切 NSLineBreakByClipping, /* Simply clip */ 按照“...wxyz”显示 NSLineBreakByTruncatingHead, /* Truncate at head of line: "...wxyz" */ 按照"abcd..."显示 NSLineBreakByTruncatingTail, /* Truncate at tail of line: "abcd..." */ 按照"ab...yz"显示 NSLineBreakByTruncatingMiddle /* Truncate middle of line: "ab...yz" */ } NS_ENUM_AVAILABLE_IOS(6_0); 简单剪切 label.lineBreakMode = NSLineBreakByClipping;
-
11、 设置阴影效果
-
11.1、 设置阴影颜色
label.shadowColor = [UIColor whiteColor];
-
11.2、 设置阴影的偏移量
label.shadowOffset = CGSizeMake(2, 0.5);
-
-
12、 设置文本的对齐方式
Values for NSTextAlignment */ typedef NS_ENUM(NSInteger, NSTextAlignment) { 左对齐 NSTextAlignmentLeft = 0, // Visually left aligned #if TARGET_OS_IPHONE 居中 NSTextAlignmentCenter = 1, // Visually centered 右对齐 NSTextAlignmentRight = 2, // Visually right aligned #else /* !TARGET_OS_IPHONE */ NSTextAlignmentRight = 1, // Visually right aligned NSTextAlignmentCenter = 2, // Visually centered #endif NSTextAlignmentJustified = 3, // Fully-justified. The last line in a paragraph is natural-aligned. NSTextAlignmentNatural = 4, // Indicates the default alignment for script } NS_ENUM_AVAILABLE_IOS(6_0); 居中对齐 label.textAlignment = NSTextAlignmentCenter;
3. 添加到父视图上
[self.window addSubview:label];
4. 释放(在MRC下使用)
[label release];