1、初始化web
1 |
UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(40, 40, 120, 44)]; |
3 |
[self.view addSubview:myLabel]; |
2、設置文字app
①、設置默認文本函數
1 |
NSString *text = @ "標籤文本" ; |
效果:
字體
②、設置標籤文本(此屬性是iOS6.0以後纔出現,如若不是必要,不建議使用此屬性)spa
1 |
NSString *text = @ "其實沒什麼" ; |
3 |
NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:text]; |
5 |
[attributeString setAttributes:@{NSForegroundColorAttributeName : [UIColor redColor], NSFontAttributeName : [UIFont systemFontOfSize:17]} range:NSMakeRange(2, 1)]; |
7 |
myLabel.attributedText = attributeString; |
效果:
.net
關鍵字標紅的效果code
01 |
NSString *keyword = @ "開源" ; |
02 |
NSString *result = @ "開源中國社區" ; |
05 |
NSMutableAttributedString *attrituteString = [[NSMutableAttributedString alloc] initWithString:result]; |
08 |
NSRange range = [result rangeOfString:keyword]; |
11 |
[attrituteString setAttributes:@{NSForegroundColorAttributeName : [UIColor redColor], NSFontAttributeName : [UIFont systemFontOfSize:17]} range:range]; |
14 |
label.attributedText = attrituteString; |
③、設置字體,若是是使用②中的文本,那在設置AttributeString的屬性時已經設置過Font了和textColor了,直接使用①設置文本時設置文本時,設置字體方法orm
1 |
myLabel.font = [UIFont systemFontOfSize:13]; |
④、設置顏色ip
1 |
myLabel.textColor = [UIColor blueColor]; |
⑤、設置對齊方式ci
1 |
myLabel.textAlignment = NSTextAlignmentCenter; //居中 |
1 |
NSTextAlignmentLeft //左對齊 |
2 |
NSTextAlignmentCenter //居中 |
3 |
NSTextAlignmentRight //右對齊 |
4 |
NSTextAlignmentJustified //最後一行天然對齊 |
5 |
NSTextAlignmentNatural //默認對齊腳本 |
NSTextAlignmentJustified和 NSTextAlignmentNatural用的時候會報錯,程序崩潰,暫時不知道何時可使用,但願知道的指教一下,感激涕零。
⑥、文字剪裁方式
1 |
NSLineBreakByWordWrapping = 0, //以空格爲邊界,保留單詞 |
2 |
NSLineBreakByCharWrapping, //保留整個字符 |
3 |
NSLineBreakByClipping, //簡單剪裁,到邊界爲止 |
4 |
NSLineBreakByTruncatingHead, //按照"……文字"顯示 |
5 |
NSLineBreakByTruncatingTail, //按照"文字……文字"顯示 |
6 |
NSLineBreakByTruncatingMiddle //按照"文字……"顯示 |
1 |
myLabel.lineBreakMode = NSLineBreakByTruncatingHead; |
⑦、設置Label enabled屬性
若是設置爲No,則文字顏色會變暗,代表其是不可用的,默認值爲YES。
2、匹配Label上的文字
①、是否根據文本寬度改變字體大小
1 |
myLabel.adjustsFontSizeToFitWidth = YES; |
2 |
//假設文字內容爲@"曾在月光之下望煙花,曾共看夕陽漸降下",Label長度爲200,則一行顯示不下,若設置此屬性爲YES,則會下降字體大小,以顯示所有內容。 |
先後對比:
![](http://static.javashuo.com/static/loading.gif)
②、改變字母之間的間距來適應label大小
1 |
//當這個屬性是YES,標籤可能改變標籤文本的字母間距,以使該文本更適合標籤的邊界內。此屬性的字符串,而無論當前行的行的裁剪模式。該屬性的默認值是NO。 |
2 |
myLabel.adjustsLetterSpacingToFitWidth = NO; |
4 |
//我的使用了一下,沒發現有什麼區別,不知道具體是何時發揮做用。 |
③、設置對齊基線
1 |
myLabel.adjustsFontSizeToFitWidth = YES; //調整基線位置需將此屬性設置爲YES |
1 |
myLabel.baselineAdjustment = UIBaselineAdjustmentAlignBaselines; |
此屬性有三個值可選
1 |
UIBaselineAdjustmentAlignBaselines //文本最上端與Label中線對齊,默認值 |
2 |
UIBaselineAdjustmentAlignCenters //文本中線與Label中線對齊 |
3 |
UIBaselineAdjustmentNone //文本最下端與Label中線對齊 |
④、最小字體大小,當字體小於這個最小值時無效,顯示此屬性值
iOS6.0以前:minimumFontSize
iOS6.0以後:minimumScaleFactor
1 |
myLabel.minimumScaleFactor = 10.0; //默認值爲0,爲當前字體大小 |
⑤、行數
1 |
myLabel.numberOfLines = 2; //Label行數 |
⑥、高亮
1 |
myLabel.highlighted = YES; //是否高亮 |
2 |
myLabel.highlightedTextColor = [UIColor redColor]; //高亮顏色;此屬性在設置按鈕的titleLabel時,不管highlighted是YES仍是NO,在按鈕按下時標題都顯示此高亮顏色 |
⑦、陰影
1 |
myLabel.shadowColor = [UIColor grayColor]; //陰影顏色,默認爲nil |
2 |
myLabel.shadowOffset = CGSizeMake(1, 1); //陰影的偏移點 |
3、Label位置
①、計算UIlabel 隨字體多行後的高度
2 |
bounds = CGRectMake(0, 0,200, 300); |
3 |
heightLabel = [myLabel textRectForBounds:bounds limitedToNumberOfLines:20]; //計算20行後的Label的Frame |
4 |
NSLog(@ "%f" ,heightLabel.size.height); |
②、繪製text到指定區域
1 |
- ( void )drawTextInRect:(CGRect)rect |
2 |
//須要重載此方法,而後由子類調用,重寫時調用super能夠按默認圖形屬性繪製,若本身徹底重寫繪製函數,就不用調用super了 |