NSAttributedString用法

標(biāo)簽:

技術(shù)分享

以前看到這種字號(hào)和顏色不一樣的字符串,想出個(gè)討巧的辦法就是“¥150”一個(gè)UILable,“元/位”一個(gè)UILable。今天翻看以前的工程,command點(diǎn)進(jìn)UITextField中看到[attributedText]這個(gè)關(guān)鍵字,以前都沒注意過UITextField還有這個(gè)屬性,其實(shí)UITextView、UILable也有這個(gè)屬性,iOS6就已經(jīng)有了,說來慚愧,對(duì)此罰站1秒鐘。

NSAttributedString叫做富文本,是一種帶有屬性的字符串,通過它可以輕松的在一個(gè)字符串中表現(xiàn)出多種字體、字號(hào)、字體大小等各不相同的風(fēng)格,還可以對(duì)段落進(jìn)行格式化。

通過以下代碼即可實(shí)現(xiàn)上面圖示效果,十分方便,從此再也不用設(shè)置兩個(gè)UILable,并且處心積慮的處理它們的長度了。

1? ? UILabel * aLable = [[UILabel alloc] initWithFrame:CGRectMake(100, 500, 200, 40)];

2? ? aLable.textAlignment = NSTextAlignmentCenter;

3? ? [self.view addSubview:aLable];

4

5? ? NSString * aString = @"¥150 元/位";

6

7? ? //富文本對(duì)象

8? ? NSMutableAttributedString * aAttributedString = [[NSMutableAttributedString alloc] initWithString:aString];

9

10? ? //富文本樣式

11? ? [aAttributedString addAttribute:NSForegroundColorAttributeName? //文字顏色

12? ? ? ? ? ? ? ? ? ? ? ? ? ? ? value:[UIColor redColor]

13? ? ? ? ? ? ? ? ? ? ? ? ? ? ? range:NSMakeRange(0, 4)];

14

15? ? [aAttributedString addAttribute:NSFontAttributeName? ? ? ? ? ? //文字字體

16? ? ? ? ? ? ? ? ? ? ? ? ? ? ? value:[UIFont systemFontOfSize:25]

17? ? ? ? ? ? ? ? ? ? ? ? ? ? ? range:NSMakeRange(0, 4)];

18

19? ? aLable.attributedText = aAttributedString;

常用屬性:

NSFontAttributeName           文字字體

NSParagraphStyleAttributeName     段落樣式(字符串通過“\n”進(jìn)行分段,此設(shè)置必須在lable.numberOfLines = 0時(shí)有效,value通過NSMutableParagraphStyle設(shè)置,它有以下屬性)

[段落樣式-插曲]

1 @property(readwrite) CGFloat lineSpacing;              //行間距

2 @property(readwrite) CGFloat paragraphSpacing;           //段間距

3 @property(readwrite) NSTextAlignment alignment;           //對(duì)齊方式

4 @property(readwrite) CGFloat firstLineHeadIndent;          //首行縮緊

5 @property(readwrite) CGFloat headIndent;               //除首行之外其他行縮進(jìn)

6 @property(readwrite) CGFloat tailIndent;               //每行容納字符的寬度

7 @property(readwrite) NSLineBreakMode lineBreakMode;       ? //換行方式

8 @property(readwrite) CGFloat minimumLineHeight;           //最小行高

9 @property(readwrite) CGFloat maximumLineHeight;           //最大行高

10 @property(readwrite) NSWritingDirection baseWritingDirection;  //書寫方式(NSWritingDirectionNatural,NSWritingDirectionLeftToRight,NSWritingDirectionRightToLeft)

11 @property(readwrite) CGFloat lineHeightMultiple;

12 @property(readwrite) CGFloat paragraphSpacingBefore;

13 @property(readwrite) float hyphenationFactor;

14 @property(readwrite,copy,NS_NONATOMIC_IOSONLY) NSArray *tabStops NS_AVAILABLE_IOS(7_0);

15 @property(readwrite,NS_NONATOMIC_IOSONLY) CGFloat defaultTabInterval NS_AVAILABLE_IOS(7_0);

[段落樣式demo]

1? ? UILabel * lable = [[UILabel alloc] initWithFrame:CGRectMake(50, 100, self.view.frame.size.width-100, 200)];

2? ? lable.backgroundColor = [UIColor lightGrayColor];

3? ? lable.numberOfLines = 0;

4? ? [self.view addSubview:lable];

5

6? ? NSString * string = @"Always believe that something wonderful is about \nto happen!";

7

8? ? //富文本

9? ? NSMutableAttributedString * attributedString = [[NSMutableAttributedString alloc] initWithString:string];

10

11? ? //段落樣式

12? ? NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];

13

14 #warning? lable.numberOfLines必須為0,段落樣式才生效

15? ? //行間距

16? ? paragraphStyle.lineSpacing = 10.0;

17? ? //段落間距

18? ? paragraphStyle.paragraphSpacing = 20.0;

19

20 //? ? paragraphStyle.baseWritingDirection = NSWritingDirectionLeftToRight;

21 //? ? paragraphStyle.firstLineHeadIndent = 10.0;

22 //? ? paragraphStyle.headIndent = 50.0;

23 //? ? paragraphStyle.tailIndent = 200.0;

24

25? ? [attributedString addAttribute:NSParagraphStyleAttributeName

26? ? ? ? ? ? ? ? ? ? ? ? ? ? ? value:paragraphStyle

27? ? ? ? ? ? ? ? ? ? ? ? ? ? ? range:NSMakeRange(0, string.length)];

28

29? ? lable.attributedText = attributedString;

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

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