UILabel 是iOS開發中用來顯示文字的控件,是UIView的子類.右移具有UIView的所有功能,隻不過比UIView多了文字顯示的功能.
UILabel的使用過程和UIView類似:
1.建立對象
2.配置屬性
3.添加到父視圖
4.釋放所有權
UILabel * v1 = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
v1.backgroundColor =[UIColor whiteColor];
設定label上顯示的文字
v1.text = @"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
label 文字位置
0 NSTextAlignmentLeft 左對齊
1 NSTextAlignmentCenter 居中
2 NSTextAlignmentRight 右對齊
v1.textAlignment = NSTextAlignmentCenter;
文字換行 預設為1 如果不限制行數将值設定為 0
v1.numberOfLines = 0;
換行的标準(文本的截取原則) 預設 以單詞為一組
NSLineBreakByWordWrapping = 0, Wrap at word boundaries, default
NSLineBreakByCharWrapping, Wrap at character boundaries
NSLineBreakByClipping, Simply clip
NSLineBreakByTruncatingHead, Truncate at head of line: "...wxyz"
NSLineBreakByTruncatingTail, Truncate at tail of line: "abcd..."
NSLineBreakByTruncatingMiddle Truncate middle of line: "ab...yz"
v1.lineBreakMode = NSLineBreakByClipping;
設定陰影的偏移量 正值向X Y 軸 正方向偏移 負值向反方向偏移.
v1.shadowOffset = CGSizeMake(2, 2) ;
設定陰影的顔色
v1.shadowColor = [UIColor yellowColor];
label 文字顔色
v1.textColor = [UIColor redColor];
label 文字大小
(1)字型樣式
(2)字号(預設 17)
systemFontOfSize: 預設使用系統預設字型,可以更改字型大小.
v1.font = [UIFont systemFontOfSize:25];
v1.font = [UIFont fontWithName:@"Thonburi-Bold" size:20];
UIFont familyNames 擷取字型家族的名字
NSLog(@"%@",[UIFont familyNames]);
UIFont fontNamesForFamilyName:@"Thonburi" 擷取對應字型下屬的字型名字
NSLog(@"%@",[UIFont fontNamesForFamilyName:@"Thonburi"]);
[_View addSubview:v1];
[v1 release];
}