關(guān)于 Label 文字樣式的一些設(shè)置

- (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號(hào)字

[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個(gè)";

label4.text = @"label4--label4--label4--label4--4個(gè)~~~";

label5.text = @"label5--label5--label5--label5--label5--label5--6個(gè)";

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è)置文字過長時(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會(huì)導(dǎo)致兩者效果完全相同。使用sdk6.1運(yùn)行的時(shí)候可以有效區(qū)分開

// 如果adjustsFontSizeToFitWidth屬性設(shè)置為YES,這個(gè)屬性就來控制文本基線的行為

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個(gè)字符顏色設(shè)置成藍(lán)色

[attributedStr01 addAttribute: NSForegroundColorAttributeName value: [UIColor blueColor] range: NSMakeRange(0, 5)];

//分段控制,第5個(gè)字符開始的3個(gè)字符,即第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;


// ***** 給 固定 文字替換成 指定 符號(hào) (多用于禁用字) *****

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個(gè)屬性:陰影顏色,模糊半徑和偏移

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) 字號(hào):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 對象

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • iOS開發(fā)系列--網(wǎng)絡(luò)開發(fā) 概覽 大部分應(yīng)用程序都或多或少會(huì)牽扯到網(wǎng)絡(luò)開發(fā),例如說新浪微博、微信等,這些應(yīng)用本身可...
    lichengjin閱讀 3,721評論 2 7
  • 用法: 先添加指針視圖,轉(zhuǎn)盤背景和開始按鈕。按鈕的點(diǎn)擊事件里設(shè)置開始動(dòng)畫,在動(dòng)畫開始的代理方法中讓開始按鈕不響應(yīng)點(diǎn)...
    全棧的貓南北閱讀 1,628評論 1 3
  • --繪圖與濾鏡全面解析 概述 在iOS中可以很容易的開發(fā)出絢麗的界面效果,一方面得益于成功系統(tǒng)的設(shè)計(jì),另一方面得益...
    韓七夏閱讀 2,791評論 2 10
  • 與NSString類似,在iOS中AttributedString也分為NSAttributedString和 N...
    錢十六閱讀 804評論 0 0
  • 1 在朋友的推薦下,看了一部比較小眾的臺(tái)灣電視劇《荼蘼》,徐譽(yù)庭編劇,楊丞琳飾演女主角鄭如薇。和很多女孩子一樣,鄭...
    塵璽閱讀 583評論 0 0