- (void)loadView
{
[super loadView];
//1.UILable的大小自適應(yīng)實(shí)例:
// ***** label基本屬性 *****
UILabel *myLabel=[[UILabel alloc] initWithFrame:CGRectMake(50, 20, 2, 2)];? // 設(shè)定位置與大小
//? ? [myLabel setFont:[UIFont fontWithName:@"Helvetica" size:20.0]]; // 字體和大小
//? ? myLabel.font = [UIFont boldSystemFontOfSize:18];? // 黑體18號字
[myLabel setNumberOfLines:0];? // 行數(shù),只有設(shè)為 0 才可以自適應(yīng)
[myLabel setBackgroundColor:[UIColor clearColor]];? // 背景色
myLabel.shadowColor = [UIColor darkGrayColor];? // 陰影顏色
myLabel.shadowOffset = CGSizeMake(1.0,1.0);? // 陰影偏移量
NSString *text = @"abcdefghigklmnopqrstuvwxyz";
UIFont *font = [UIFont fontWithName:@"Helvetica" size:20.0];
// *******? 根據(jù)要顯示的text計(jì)算label高度? *******
// iOS 7 之后被棄用
CGSize size = [text sizeWithFont:font constrainedToSize:CGSizeMake(175.0f, 2000.0f) lineBreakMode:UILineBreakModeWordWrap];
// iOS 7 后 :
{
NSDictionary * tdic = [NSDictionary dictionaryWithObjectsAndKeys:font, NSFontAttributeName,nil];
size =[text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin |NSStringDrawingUsesFontLeading attributes:tdic context:nil].size;
}
CGRect rect = myLabel.frame;
rect.size = size;
[myLabel setFrame:rect];
[myLabel setText:text];
myLabel.shadowColor = [UIColor darkGrayColor];//陰影顏色
myLabel.shadowOffset = CGSizeMake(2.0,2.0);//陰影大小
[self.view addSubview:myLabel];
//2.UILable的基本用法
UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake(50.0, 80.0, 200.0, 30.0)];
UILabel *label2 = [[UILabel alloc]initWithFrame:CGRectMake(50.0, 140.0, 200.0, 50.0)];
UILabel *label3 = [[UILabel alloc]initWithFrame:CGRectMake(50.0, 200.0, 200.0, 50.0)];
UILabel *label4 = [[UILabel alloc]initWithFrame:CGRectMake(50.0, 260.0, 200.0, 50.0)];
UILabel *label5 = [[UILabel alloc]initWithFrame:CGRectMake(50.0, 320.0, 200.0, 50.0)];
UILabel *label6 = [[UILabel alloc]initWithFrame:CGRectMake(50.0, 380.0, 200.0, 50.0)];
UILabel *label7 = [[UILabel alloc]initWithFrame:CGRectMake(50.0, 440.0, 200.0, 50.0)];
//設(shè)置顯示文字
label1.text = @"label1";
label2.text = @"label2";
label3.text = @"label3--label3--label3--label3--label3--label3--label3--label3--label3--label3--label3--11個";
label4.text = @"label4--label4--label4--label4--4個~~~";
label5.text = @"label5--label5--label5--label5--label5--label5--6個";
label6.text = @"label6";
label7.text = @"label7";
//設(shè)置字體: 黑體字,正常的是 SystemFontOfSize
label1.font = [UIFont boldSystemFontOfSize:20];
//設(shè)置文字顏色
label1.textColor = [UIColor orangeColor];
label2.textColor = [UIColor purpleColor];
//設(shè)置背景顏色
label1.backgroundColor = [UIColor clearColor];
label2.backgroundColor = [UIColor colorWithRed:0.5f green:30/255.0f blue:0.3f alpha:0.5f];
//設(shè)置文字位置
label1.textAlignment = NSTextAlignmentRight;? // UITextAlignmentRight 等寫法在 iOS 6 后棄用了
label2.textAlignment = NSTextAlignmentCenter;
//設(shè)置字體大小適應(yīng)label寬度
label4.adjustsFontSizeToFitWidth = YES;? // 字越多,越窄
//設(shè)置label的行數(shù)
label5.numberOfLines = 2;
//設(shè)置高亮
label6.highlighted = YES;
label6.highlightedTextColor = [UIColor orangeColor];
//設(shè)置陰影
label7.shadowColor = [UIColor redColor];
label7.shadowOffset = CGSizeMake(1.0,1.0);
//設(shè)置是否能與用戶進(jìn)行交互
label7.userInteractionEnabled = YES;
//設(shè)置label中的文字是否可變蹦骑,默認(rèn)值是YES
label3.enabled = NO;
//設(shè)置文字過長時的顯示格式
// iOS 6 后棄用 UILineBreakModeMiddleTruncation
{
label3.lineBreakMode = UILineBreakModeMiddleTruncation;? //截去中間
//? typedef enum {
//? ? ? UILineBreakModeWordWrap = 0,? //以空格為邊界,保留單詞憔维。
//? ? ? UILineBreakModeCharacterWrap,
//? ? ? UILineBreakModeClip,? //截去多余部分
//? ? ? UILineBreakModeHeadTruncation,? ? //截去頭部
//? ? ? UILineBreakModeTailTruncation,? ? //截去尾部
//? ? ? UILineBreakModeMiddleTruncation,? //截去中間
//? } UILineBreakMode;
}
label3.lineBreakMode = NSLineBreakByWordWrapping;
//? ? typedef NS_ENUM(NSInteger, NSLineBreakMode) {
//? ? ? ? NSLineBreakByWordWrapping = 0,? ? // //以空格為邊界涛救,保留單詞。
//? ? ? ? NSLineBreakByCharWrapping, // 截取到范圍內(nèi)(字符串)
//? ? ? ? NSLineBreakByClipping, //簡單剪裁业扒,到邊界為止
//? ? ? ? NSLineBreakByTruncatingHead, // 從前面開始裁剪字符串: "...wxyz"
//? ? ? ? NSLineBreakByTruncatingTail, // 從后面開始裁剪字符串: "abcd..."
//? ? ? ? NSLineBreakByTruncatingMiddle? // 從中間裁剪字符串:? "ab...yz"
//? ? }
// 我們使用的xcode是5.0版本检吆,默認(rèn)使用的是sdk7.0。使用sdk7.0會導(dǎo)致兩者效果完全相同程储。使用sdk6.1運(yùn)行的時候可以有效區(qū)分開
// 如果adjustsFontSizeToFitWidth屬性設(shè)置為YES蹭沛,這個屬性就來控制文本基線的行為
label4.baselineAdjustment = UIBaselineAdjustmentAlignCenters;
//? typedef enum {
//? ? ? UIBaselineAdjustmentAlignBaselines,
//? ? ? UIBaselineAdjustmentAlignCenters,
//? ? ? UIBaselineAdjustmentNone,
//? } UIBaselineAdjustment;
// ***** 根據(jù)屬性給固定位置的字體更換顏色或字體大小 *****
NSString *originStr = @"Hello,World!";
//創(chuàng)建 NSMutableAttributedString
NSMutableAttributedString *attributedStr01 = [[NSMutableAttributedString alloc] initWithString: originStr];
//添加屬性
//給所有字符設(shè)置字體為Zapfino,字體高度為15像素
[attributedStr01 addAttribute: NSFontAttributeName value: [UIFont fontWithName: @"Zapfino" size: 15] range: NSMakeRange(0, originStr.length)];
//分段控制章鲤,最開始4個字符顏色設(shè)置成藍(lán)色
[attributedStr01 addAttribute: NSForegroundColorAttributeName value: [UIColor blueColor] range: NSMakeRange(0, 5)];
//分段控制摊灭,第5個字符開始的3個字符,即第5咏窿、6斟或、7字符設(shè)置為紅色
[attributedStr01 addAttribute: NSForegroundColorAttributeName value: [UIColor redColor] range: NSMakeRange(5, 3)];
//賦值給顯示控件label01的 attributedText
UILabel *label01 = [[UILabel alloc] initWithFrame:CGRectMake(50.0, 520.0, 200.0, 50.0)];
label01.attributedText = attributedStr01;
// ***** 給 固定 文字替換成 指定 符號 (多用于禁用字) *****
NSString *search = @"王木木";
NSString *replace = @"***";
NSMutableString *mstr = [[NSMutableString alloc]initWithString:@"dafkasdhf王木木kasddsf"];
NSRange range = [mstr rangeOfString:search];
[mstr replaceCharactersInRange:range withString:replace];
UILabel *labelStr = [[UILabel alloc] initWithFrame:CGRectMake(50.0, 580.0, 200.0, 50.0)];
labelStr.text = master;
// NSUnderlineStyleNone? 不設(shè)置刪除線
// NSUnderlineStyleSingle 設(shè)置刪除線為細(xì)單實(shí)線
// NSUnderlineStyleThick? 設(shè)置刪除線為粗單實(shí)線
// NSUnderlineStyleDouble 設(shè)置刪除線為細(xì)雙實(shí)線
// 例:
UILabel *labeltext1 = [[UILabel alloc] initWithFrame:CGRectMake(240.0, 380.0, 200.0, 50.0)];
UILabel *labeltext2 = [[UILabel alloc] initWithFrame:CGRectMake(240.0, 440.0, 200.0, 50.0)];
UILabel *labeltext3 = [[UILabel alloc] initWithFrame:CGRectMake(240.0, 500.0, 200.0, 50.0)];
NSDictionary *attrDict1 = @{ NSStrikethroughStyleAttributeName: @(NSUnderlineStyleSingle),
NSFontAttributeName: [UIFont systemFontOfSize:18] };
labeltext1.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict1];
NSDictionary *attrDict2 = @{ NSStrikethroughStyleAttributeName: @(NSUnderlineStyleThick),
NSFontAttributeName: [UIFont systemFontOfSize:18] };
labeltext2.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict2];
NSDictionary *attrDict3 = @{ NSStrikethroughStyleAttributeName: @(NSUnderlineStyleDouble),
NSFontAttributeName: [UIFont systemFontOfSize:18] };
labeltext3.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict3];
//NSUnderlineColorAttributeName 設(shè)置下劃線顏色素征,取值為 UIColor 對象集嵌,默認(rèn)值為黑色
UILabel *labeltext4 = [[UILabel alloc] initWithFrame:CGRectMake(200.0, 380.0, 200.0, 50.0)];
UILabel *labeltext5 = [[UILabel alloc] initWithFrame:CGRectMake(200.0, 440.0, 200.0, 50.0)];
UILabel *labeltext6 = [[UILabel alloc] initWithFrame:CGRectMake(200.0, 500.0, 200.0, 50.0)];
NSDictionary *attrDict4 = @{ NSUnderlineColorAttributeName: [UIColor blueColor],
NSUnderlineStyleAttributeName: @(NSUnderlineStyleSingle),
NSFontAttributeName: [UIFont systemFontOfSize:22] };
labeltext4.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict4];
NSDictionary *attrDict5 = @{ NSUnderlineColorAttributeName: [UIColor orangeColor],
NSUnderlineStyleAttributeName: @(NSUnderlineStyleThick),
NSFontAttributeName: [UIFont systemFontOfSize:22] };
labeltext5.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict5];
NSDictionary *attrDict6 = @{ NSUnderlineColorAttributeName: [UIColor greenColor],
NSUnderlineStyleAttributeName: @(NSUnderlineStyleDouble),
NSFontAttributeName: [UIFont systemFontOfSize:22] };
labeltext6.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict6];
// NSStrokeWidthAttributeName 設(shè)置筆畫寬度,取值為 NSNumber 對象(整數(shù))
// 負(fù)值填充效果御毅,正值中空效果
UILabel *labeltext7 = [[UILabel alloc] initWithFrame:CGRectMake(200.0, 380.0, 200.0, 50.0)];
UILabel *labeltext8 = [[UILabel alloc] initWithFrame:CGRectMake(200.0, 440.0, 200.0, 50.0)];
UILabel *labeltext9 = [[UILabel alloc] initWithFrame:CGRectMake(200.0, 500.0, 200.0, 50.0)];
NSDictionary *attrDict7 = @{ NSStrokeWidthAttributeName: @(-3),
NSFontAttributeName: [UIFont systemFontOfSize:30] };
labeltext7.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict7];
NSDictionary *attrDict8 = @{ NSStrokeWidthAttributeName: @(0),
NSFontAttributeName: [UIFont systemFontOfSize:30] };
labeltext8.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict8];
NSDictionary *attrDict9 = @{ NSStrokeWidthAttributeName: @(3),
NSFontAttributeName: [UIFont systemFontOfSize:30] };
labeltext9.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict9];
//NSShadowAttributeName 設(shè)置陰影屬性根欧,取值為 NSShadow 對象
UILabel *labelH01 = [[UILabel alloc] initWithFrame:CGRectMake(200.0, 500.0, 200.0, 50.0)];
NSShadow *shadow1 = [[NSShadow alloc] init];? //NSShadow 對象比較簡單,只有3個屬性:陰影顏色端蛆,模糊半徑和偏移
shadow1.shadowOffset = CGSizeMake(16, 5);? ? ? //陰影偏移(X方向偏移和Y方向偏移)
shadow1.shadowBlurRadius = 3;? ? ? ? ? ? ? //模糊半徑
shadow1.shadowColor = [UIColor orangeColor];? //陰影顏色
NSDictionary *attr = @{ NSShadowAttributeName: shadow1,
NSFontAttributeName: [UIFont systemFontOfSize:20] };
labelH01.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attr];
//NSExpansionAttributeName 設(shè)置文本橫向拉伸屬性凤粗,取值為 NSNumber (float),正值橫向拉伸文本,負(fù)值橫向壓縮文本
UILabel *labelH02 = [[UILabel alloc] initWithFrame:CGRectMake(170.0, 380.0, 200.0, 50.0)];
UILabel *labelH03 = [[UILabel alloc] initWithFrame:CGRectMake(170.0, 440.0, 200.0, 50.0)];
UILabel *labelH04 = [[UILabel alloc] initWithFrame:CGRectMake(170.0, 500.0, 200.0, 50.0)];
NSDictionary *attr1 = @{ NSExpansionAttributeName: @(-1),
NSFontAttributeName: [UIFont systemFontOfSize:20] };
labelH02.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attr1];
NSDictionary *attr2 = @{ NSExpansionAttributeName: @(0),
NSFontAttributeName: [UIFont systemFontOfSize:20] };
labelH03.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attr2];
NSDictionary *attr3 = @{ NSExpansionAttributeName: @(0.6),
NSFontAttributeName: [UIFont systemFontOfSize:20] };
labelH04.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attr3];
[self.view addSubview:label1];
[self.view addSubview:label2];
[self.view addSubview:label3];
[self.view addSubview:label4];
[self.view addSubview:label5];
[self.view addSubview:label6];
[self.view addSubview:label7];
[self.view addSubview:label01];
[self.view addSubview:labelStr];
//? ? [self.view addSubview:labeltext1];
//? ? [self.view addSubview:labeltext2];
//? ? [self.view addSubview:labeltext3];
//? ? [self.view addSubview:labeltext4];
//? ? [self.view addSubview:labeltext5];
//? ? [self.view addSubview:labeltext6];
//? ? [self.view addSubview:labeltext7];
//? ? [self.view addSubview:labeltext8];
//? ? [self.view addSubview:labeltext9];
//? ? [self.view addSubview:labelH01];
//? ? [self.view addSubview:labelH02];
//? ? [self.view addSubview:labelH03];
//? ? [self.view addSubview:labelH04];
}
// ****** AttributedString 可以設(shè)置的那些屬性 ******
// NSFontAttributeName? ? ? ? ? ? ? ? 設(shè)置字體屬性今豆,默認(rèn)值:字體:Helvetica(Neue) 字號:12
// NSForegroundColorAttributeNam? ? ? 設(shè)置字體顏色嫌拣,取值為 UIColor對象,默認(rèn)值為黑色
// NSBackgroundColorAttributeName? ? 設(shè)置字體所在區(qū)域背景顏色呆躲,取值為 UIColor對象异逐,默認(rèn)值為nil, 透明色
// NSLigatureAttributeName? ? ? ? ? ? 設(shè)置連體屬性,取值為NSNumber 對象(整數(shù))插掂,0 表示沒有連體字符灰瞻,1 表示使用默認(rèn)的連體字符
// NSKernAttributeName? ? ? ? ? ? ? ? 設(shè)定字符間距,取值為 NSNumber 對象(整數(shù))辅甥,正值間距加寬酝润,負(fù)值間距變窄
// NSStrikethroughStyleAttributeName? 設(shè)置刪除線,取值為 NSNumber 對象(整數(shù))
// NSStrikethroughColorAttributeName? 設(shè)置刪除線顏色璃弄,取值為 UIColor 對象要销,默認(rèn)值為黑色
// NSUnderlineStyleAttributeName? ? ? 設(shè)置下劃線,取值為 NSNumber 對象(整數(shù))夏块,枚舉常量 NSUnderlineStyle中的值蕉陋,與刪除線類似
// NSUnderlineColorAttributeName? ? ? 設(shè)置下劃線顏色捐凭,取值為 UIColor 對象,默認(rèn)值為黑色
// NSStrokeWidthAttributeName? ? ? ? 設(shè)置筆畫寬度凳鬓,取值為 NSNumber 對象(整數(shù))茁肠,負(fù)值填充效果,正值中空效果
// NSStrokeColorAttributeName? ? ? ? 填充部分顏色缩举,不是字體顏色垦梆,取值為 UIColor 對象
// NSShadowAttributeName? ? ? ? ? ? ? 設(shè)置陰影屬性,取值為 NSShadow 對象
// NSTextEffectAttributeName? ? ? ? ? 設(shè)置文本特殊效果仅孩,取值為 NSString 對象托猩,目前只有圖版印刷效果可用:
// NSBaselineOffsetAttributeName? ? ? 設(shè)置基線偏移值,取值為 NSNumber (float),正值上偏辽慕,負(fù)值下偏
// NSObliquenessAttributeName? ? ? ? 設(shè)置字形傾斜度京腥,取值為 NSNumber (float),正值右傾,負(fù)值左傾
// NSExpansionAttributeName? ? ? ? ? 設(shè)置文本橫向拉伸屬性溅蛉,取值為 NSNumber (float),正值橫向拉伸文本公浪,負(fù)值橫向壓縮文本
// NSWritingDirectionAttributeName? ? 設(shè)置文字書寫方向,從左向右書寫或者從右向左書寫
// NSVerticalGlyphFormAttributeName? 設(shè)置文字排版方向船侧,取值為 NSNumber 對象(整數(shù))欠气,0 表示橫排文本,1 表示豎排文本
// NSLinkAttributeName? ? ? ? ? ? ? ? 設(shè)置鏈接屬性镜撩,點(diǎn)擊后調(diào)用瀏覽器打開指定URL地址
// NSAttachmentAttributeName? ? ? ? ? 設(shè)置文本附件,取值為NSTextAttachment對象,常用于文字圖片混排
// NSParagraphStyleAttributeName? ? ? 設(shè)置文本段落排版格式预柒,取值為 NSParagraphStyle 對象