最普通的文本標簽, 說白了就是一個字符串的集合,來展示文本用的。
如下詳細說明了label的各種屬性
- (void)viewDidLoad
{
[super viewDidLoad];
UILabel *label = [[UILabel alloc] init];
//設置顯示的文字
label.text = @"這是一個文本控件";
//設置顯示的文字字體
label.font = [UIFont systemFontOfSize:18.0];
//設置文字顏色
label.textColor = [UIColor redColor];
//設置陰影顏色
label.shadowColor = [UIColor greenColor];
//設置陰影的大小
label.shadowOffset = CGSizeMake(1.0, 1.0);
/*
typedef NS_ENUM(NSInteger, NSTextAlignment)
{
NSTextAlignmentLeft = 0, // 左對齊
#if iphone
NSTextAlignmentCenter = 1, // 居中
NSTextAlignmentRight = 2, // 右對齊
#else ipad
NSTextAlignmentRight = 1, // Visually right aligned
NSTextAlignmentCenter = 2, // Visually centered
#endif 其他
NSTextAlignmentJustified = 3, // 和段落對齊
NSTextAlignmentNatural = 4, // 默認狀態 正常情況下
} NS_ENUM_AVAILABLE_IOS(6_0);
*/
//文字顯示模式
label.textAlignment = NSTextAlignmentCenter;
//設置自動換行 不過是在ios使用 現在已經過期
label.lineBreakMode = UILineBreakModeCharacterWrap;
//現在可以使用這個換行
label.numberOfLines = 0;
//設置高亮狀態下的文字顏色
label.highlightedTextColor = [UIColor greenColor];
//設置字體最小值 不過minimumFontSize已經過期
label.minimumFontSize = 18.0;
label.minimumScaleFactor = 18.0;
//設置控件對象的大小是否隨標題內容的大小自動調整
[label setAutoresizesSubviews:true];
//設置用戶可不可以操作
label.userInteractionEnabled = NO;
//設置控件的狀態 YES為可用
label.enabled = YES;
//指定了線寬度的最大值,以便計算固有內容大小
/*
UILabel和NSTextField對於多行文本的固有內容大小是模糊不清的。
文本的高度取決於線的寬度,這也是解決約束條件時需要弄清的問題。
為了解決這個問題,這兩個類都有一個叫做preferredMaxLayoutWidth的屬性,
這個屬性指定了線寬度的最大值,以便計算固有內容大小。
*/
label.preferredMaxLayoutWidth = 19.0;
//設置文本字體是否要減小來適應label的區域
label.adjustsFontSizeToFitWidth = NO;
/*
typedef NS_ENUM(NSInteger, UIBaselineAdjustment) {
UIBaselineAdjustmentAlignBaselines = 0, // 默認 文本最上端於label中線對齊
UIBaselineAdjustmentAlignCenters, // 文本中線於label中線對齊
UIBaselineAdjustmentNone, // 文本最低端與label中線對齊
};
*/
//這個值設置文本的基線位置
label.baselineAdjustment = UIBaselineAdjustmentAlignBaselines;
//把文本添加到控制器的視圖中
[self.view addSubview:label];