單一label多顏色多字型

2021-07-07 08:45:43 字數 2913 閱讀 2871

attributedlabel *label = [[attributedlabel alloc] initwithframe:cgrectmake(20, 20, 150, 40)];

// 注意!!一定要先給text賦值,然後再加屬性;

label.text = @"this is test ";

[self.view addsubview:label];

// 設定this為紅色

[label setcolor:[uicolor redcolor] fromindex:0 length:4];

// 設定is為黃色

[label setcolor:[uicolor yellowcolor] fromindex:5 length:2];

// 設定this字型為加粗16號字

[label setfont:[uifont boldsystemfontofsize:30] fromindex:0 length:4];

// 給this加上下劃線

[label setstyle:kctunderlinestyledouble fromindex:0 length:4];

label.backgroundcolor = [uicolor clearcolor];

attributedlabel *label2 = [[attributedlabel alloc] initwithframe:cgrectmake(20, 120, 250, 100)];

[self.view addsubview:label2];

[label2 setcolor:[uicolor bluecolor] fromindex:12 length:1];

label2.backgroundcolor = [uicolor clearcolor];

#import #import #import @inte***ce attributedlabel : uilabel

// 設定某段字的顏色

- (void)setcolor:(uicolor *)color fromindex:(nsinteger)location length:(nsinteger)length;

// 設定某段字的字型

- (void)setfont:(uifont *)font fromindex:(nsinteger)location length:(nsinteger)length;

// 設定某段字的風格

- (void)setstyle:(ctunderlinestyle)style fromindex:(nsinteger)location length:(nsinteger)length;

@end

#import "attributedlabel.h"

@inte***ce attributedlabel()

@property (nonatomic,retain)nsmutableattributedstring *attstring;

@end

@implementation attributedlabel

@synthesize attstring = _attstring;

- (void)dealloc

- (id)initwithframe:(cgrect)frame

return self;

}- (void)drawrect:(cgrect)rect

- (void)settext:(nsstring *)textelse

}// 設定某段字的顏色

- (void)setcolor:(uicolor *)color fromindex:(nsinteger)location length:(nsinteger)length

[_attstring addattribute:(nsstring *)kctforegroundcolorattributename

value:(id)color.cgcolor

range:nsmakerange(location, length)];

}// 設定某段字的字型

- (void)setfont:(uifont *)font fromindex:(nsinteger)location length:(nsinteger)length

[_attstring addattribute:(nsstring *)kctfontattributename

value:(id)cfbridgingrelease(ctfontcreatewithname((cfstringref)font.fontname,

font.pointsize,

null))

range:nsmakerange(location, length)];

}// 設定某段字的風格

- (void)setstyle:(ctunderlinestyle)style fromindex:(nsinteger)location length:(nsinteger)length

[_attstring addattribute:(nsstring *)kctunderlinestyleattributename

value:(id)[nsnumber numberwithint:style]

range:nsmakerange(location, length)];}/*

// only override drawrect: if you perform custom drawing.

// an empty implementation adversely affects performance during animation.

- (void)drawrect:(cgrect)rect

*/@end

Label 字型顏色

qdatetime time qdatetime currentdatetime get currnet time qstring str time.tostring yyyy mm dd hh mm ss dddd ui label time settext str 改變字型或按鈕的顏色 先加入色...

使用乙個label顯示不同的字型和顏色

最近有個需求,要實現乙個翻頁,顯示當前頁數和總頁數,頁數和總頁數字號不一樣。當然了實現這個效果是很容易的,兩個label簡單搞定。可是,我們使用乙個label也是可以的喲。即將實現的效果圖如下 在這裡我們可以使用label的乙個屬性,叫做attributedtext,很強大喲。不說了上 加1是為了讓...

iOS中Label實現顯示不同顏色與字型的方法

前言 ios中label是我們經常遇到的乙個控制項,我們大家應該都會簡單的使用它,像下面這個 就能簡單的建立乙個label 1 建立 cgrectrect cgrectmake 100,100,100,100 uilabel label uilabelalloc initwithframe rect...