一、初始化
uilabel *mylabel = [[uilabel alloc] initwithframe:cgrectmake(40, 40, 120, 44)];
[self.view addsubview:mylabel];
二、設置文字
1、設置默認文本
nsstring *text = @"標簽文本";
mylabel.text = text;
效果:
2、設置標簽文本(此屬性是ios6.0之后才出現,如若不是必要,不建議使用此屬性)
nsstring *text = @"其實沒什么";
nsmutableattributedstring *attributestring = [[nsmutableattributedstring alloc] initwithstring:text];
[attributestring setattributes:@{nsforegroundcolorattributename : [uicolor redcolor], nsfontattributename : [uifont systemfontofsize:17]} range:nsmakerange(2, 1)];
mylabel.attributedtext = attributestring;
效果:
關鍵字標紅的效果
nsstring *keyword = @"開源";
nsstring *result = @"開源中國社區";
// 設置標簽文字
nsmutableattributedstring *attritutestring = [[nsmutableattributedstring alloc] initwithstring:result];
// 獲取標紅的位置和長度
nsrange range = [result rangeofstring:keyword];
// 設置標簽文字的屬性
[attritutestring setattributes:@{nsforegroundcolorattributename : [uicolor redcolor], nsfontattributename : [uifont systemfontofsize:17]} range:range];
// 顯示在label上
label.attributedtext = attritutestring;
3、設置字體,如果是使用②中的文本,那在設置attributestring的屬性時已經設置過font了和textcolor了,直接使用①設置文本時設置文本時,設置字體方法
mylabel.font = [uifont systemfontofsize:13];
4、設置顏色
mylabel.textcolor = [uicolor bluecolor];
5、設置對齊方式
mylabel.textalignment = nstextalignmentcenter;//居中
nstextalignmentleft //左對齊
nstextalignmentcenter //居中
nstextalignmentright //右對齊
nstextalignmentjustified//最后一行自然對齊
nstextalignmentnatural //默認對齊腳本
nstextalignmentjustified和 nstextalignmentnatural用的時候會報錯,程序崩潰,暫時不知道什么時候可以使用,希望知道的指教一下,感激不盡。
5、文字剪裁方式
nslinebreakbywordwrapping = 0,//以空格為邊界,保留單詞
nslinebreakbycharwrapping, //保留整個字符
nslinebreakbyclipping, //簡單剪裁,到邊界為止
nslinebreakbytruncatinghead, //按照"……文字"顯示
nslinebreakbytruncatingtail, //按照"文字……文字"顯示
nslinebreakbytruncatingmiddle //按照"文字……"顯示
mylabel.linebreakmode = nslinebreakbytruncatinghead;
7、設置label enabled屬性
如果設置為no,則文字顏色會變暗,表明其是不可用的,默認值為yes。
mylabel.enabled = no;
三、匹配label上的文字
1、是否根據文本寬度改變字體大小
mylabel.adjustsfontsizetofitwidth = yes;
//假設文字內容為@"曾在月光之下望煙花,曾共看夕陽漸降下",label長度為200,則一行顯示不下,若設置此屬性為yes,則會降低字體大小,以顯示全部內容。
前后對比:
2、改變字母之間的間距來適應label大小
//當這個屬性是yes,標簽可能改變標簽文本的字母間距,以使該文本更適合標簽的邊界內。此屬性的字符串,而不管當前行的行的裁剪模式。該屬性的默認值是no。
mylabel.adjustsletterspacingtofitwidth = no;
//個人使用了一下,沒發現有什么區別,不知道具體是什么時候發揮作用。
3、設置對齊基線
mylabel.adjustsfontsizetofitwidth = yes;//調整基線位置需將此屬性設置為yes
mylabel.baselineadjustment = uibaselineadjustmentalignbaselines;
此屬性有三個值可選
uibaselineadjustmentalignbaselines //文本最上端與label中線對齊,默認值
uibaselineadjustmentaligncenters //文本中線與label中線對齊
uibaselineadjustmentnone //文本最下端與label中線對齊
4、最小字體大小,當字體小于這個最小值時無效,顯示此屬性值
ios6.0之前:minimumfontsize
ios6.0之后:minimumscalefactor
mylabel.minimumscalefactor = 10.0;//默認值為0,為當前字體大小
5、行數
mylabel.numberoflines = 2;//label行數
6、高亮
mylabel.highlighted = yes;//是否高亮
mylabel.highlightedtextcolor = [uicolor redcolor];//高亮顏色;此屬性在設置按鈕的titlelabel時,無論highlighted是yes還是no,在按鈕按下時標題都顯示此高亮顏色
7、陰影
mylabel.shadowcolor = [uicolor graycolor];//陰影顏色,默認為nil
mylabel.shadowoffset = cgsizemake(1, 1);//陰影的偏移點
四、label位置
1、計算uilabel 隨字體多行后的高度
cgrect result,bounds;
bounds = cgrectmake(0, 0,200, 300);
heightlabel = [mylabel textrectforbounds:bounds limitedtonumberoflines:20];//計算20行后的label的frame
nslog(@"%f",heightlabel.size.height);
2、繪制text到指定區域
- (void)drawtextinrect:(cgrect)rect
//需要重載此方法,然后由子類調用,重寫時調用super可以按默認圖形屬性繪制,若自己完全重寫繪制函數,就不用調用super了
ps:關于字體名字的問題
在ios下使用自定義字體比較蛋疼的就是名字不知道是什么,大部分字體可以在打開字體冊就可以看到,如正常的xxx-regular,但有時候名字并不是這個,有可能是xxx而已,還有可能是xxxitalic(而不是xxx-italic)。名字錯了,建立的字體肯定是空的。
那怎么辦呢?
有辦法的,有一段代碼可以把當前所有的字體名字輸出來。
如:
1.把xxx字體加入到resouce;
2.info.plist的fonts provided by application里面加入字體文件名
3.運行下面代碼
nsarray *familynames = [uifont familynames];
for( nsstring *familyname in familynames ){
printf( "family: %s \n", [familyname utf8string] );
nsarray *fontnames = [uifont fontnamesforfamilyname:familyname];
for( nsstring *fontname in fontnames ){
printf( "\tfont: %s \n", [fontname utf8string] );
}
}
4.找到你的字體xxx,如下,font:后面的就是我們要的字體名字
1
2
3
4
5
6
7
8
|
family: courier new font: couriernewpsmt font: couriernewps-boldmt font: couriernewps-italicmt font: couriernewps-bolditalicmt family: oriya sangam mn font: oriyasangammn font: oriyasangammn-bold |
5.自然是使用,如
uifont *font_regular = [uifont fontwithname:@"couriernewps-italicmt" size:84];