/創(chuàng)建UILabel
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 40, 280, 80)];
UILabel *label = [[UILabel alloc] initWithFrame:self.view.bounds];
Alpha = 0 - 1 ; 透明度
//設(shè)置顯示文本
label.text = @“我是標(biāo)簽”;
//設(shè)置背景色
label.backgroundColor = [UIColor grayColor];
//設(shè)置tag
label.tag = 91;
//設(shè)置標(biāo)簽文本字體和字體大小
label.font = [UIFont systemFontOfSize:14]; // boldSystemFormOfSize 加粗 italicSystemFontOfSize 斜體
//文本frame
CGRect rect =label.frame;
label.frame =rect;
//文本顏色
label.textColor = [UIColor blueColor];
//文本最多行數(shù),為0時沒有最大行數(shù)限制
label.numberOfLines = 2;
//最小字體,行數(shù)為1時有效,默認(rèn)為0.0
label.minimumFontSize = 10.0;
//文本高亮
label.highlighted = YES;
//文本是否可變
label.enabled = YES;
//label背景色
label.backgroundColor = [UIColor whiteColor];
//文本陰影顏色
label.shadowColor = [UIColor grayColor];
//陰影大小
label.shadowOffset = CGSizeMake(1.0, 1.0);
//是否能與用戶交互
label.userInteractionEnabled = YES;
//設(shè)置label的旋轉(zhuǎn)角度
label.transform = CGAffineTransformMakeRotation(0.2);
//文本文字自適應(yīng)大小
//設(shè)置文本對齊方式
label.textAlignment = UITextAlignmentCenter;
//文本對齊方式有以下三種
//typedef enum {
UITextAlignmentLeft = 0,左對齊
UITextAlignmentCenter,居中對齊
UITextAlignmentRight, 右對齊
//} UITextAlignment;
label.adjustsFontSizeToFitWidth = YES;
//將label 添加到view 中 這樣才能夠顯示
[self.view addSubview:label];
if ([subclassModel.special isEqualToString:strNULL]) {
self.priceLabel.text =[NSString stringWithFormat:@"$%@",str];
}else{
NSString *str2 =[subclassModel.special substringToIndex:[subclassModel.special length]-2];
self.specialLabel.text =[NSString stringWithFormat:@"$%@",str2];
self.priceLabel.text =[NSString stringWithFormat:@"$%@",str];
//富文本
NSDictionary *attribtDic = @{NSStrikethroughStyleAttributeName:[NSNumber numberWithInteger:NSUnderlineStyleSingle]};
NSMutableAttributedString *attributStr =[[NSMutableAttributedString alloc]initWithString:self.priceLabel.text attributes:attribtDic];
self.priceLabel.attributedText =attributStr;
}
富文本 下劃線 和 中劃線
UILabel *label1=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
NSDictionary *attribtDic = @{NSUnderlineStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleSingle]};
NSMutableAttributedString *attribtStr = [[NSMutableAttributedString alloc]initWithString:@"下劃線" attributes:attribtDic];
label1.attributedText = attribtStr;
[self.view addSubview:label1];
UILabel *label2=[[UILabel alloc]initWithFrame:CGRectMake(0, 100, 100, 100)];
NSDictionary *attribtDic2 = @{NSStrikethroughStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleSingle]};
NSMutableAttributedString *attribtStr2 = [[NSMutableAttributedString alloc]initWithString:@"中劃線" attributes:attribtDic2];
label2.attributedText = attribtStr2;
[self.view addSubview:label2];
//設(shè)置文字過長時的顯示格式 //截去中間
label.lineBreakMode = UILineBreakModeTailTruncation;
//截取方式有以下6種
//typedef enum {
// UILineBreakModeWordWrap = 0, 以空格為邊界,保留整個單詞
// UILineBreakModeCharacterWrap, 保留整個字符
// UILineBreakModeClip, 到邊界為止
// UILineBreakModeHeadTruncation, 截去頭部
// UILineBreakModeTailTruncation, 截去尾部
// UILineBreakModeMiddleTruncation, 截去中間
//} UILineBreakMode;
//當(dāng)adjustsFontSizeToFitWidth=YES時候,如果文本font要縮小時
//baselineAdjustment這個值控制文本的基線位置,只有文本行數(shù)為1是有效
label.baselineAdjustment = UIBaselineAdjustmentAlignCenters;
//有三種方式
//typedef enum {
// UIBaselineAdjustmentAlignBaselines = 0, 默認(rèn)值文本最上端于label中線對齊
// UIBaselineAdjustmentAlignCenters,//文本中線于label中線對齊
// UIBaselineAdjustmentNone,//文本最低端與label中線對齊
//} UIBaselineAdjustment;
拖拽控件 創(chuàng)建輸出口 輸出口屬性
@property (weak,nonatomic)IBOutlet UILabel *label ;
編程中,要想訪問視圖,我們需要將這些視圖定義為屬性,OC版本中也可以將視圖定義為 成員變量,不需要使用Outlet 關(guān)鍵字修飾
成員變量:
@property (strong ,nonatomic)UILabel *label;
區(qū)別:weak 和 strong
xib 和 故事板 中創(chuàng)建的label 用weak ; 對象所有權(quán)在故事板上或XIB。
因為對象所有權(quán)在視圖控制器中: 所以第二個label 用strong
Plain 普通樣式
Attributed 帶有屬性的。 富文本相關(guān) 里面可以有 文字 圖標(biāo) 圖片
Alpha = 0 - 1 ; 透明度
Line Breaks 默認(rèn)多出的文字 省略的位置 左 中 右