
前言
CATextLayer適用于IOS或者M(jìn)AC,比UIlablel 和 NSTextView 能做的事很多,可以這樣說(shuō)UIlablel是通過(guò)CATextLayer實(shí)現(xiàn)的,身為CALayer的三大子類(lèi)之一,它的功能遠(yuǎn)比 UIlablel 強(qiáng)大的多的多,其最主要的特點(diǎn)是CATextLayer可以被NSMutableAttributedString直接附值。而NSMutableAttributedString有可以最自己內(nèi)容作出顏色以及大小的調(diào)整,這樣結(jié)合起來(lái)使用的話(huà),就遠(yuǎn)比UILabel 靈活的多,效果也酷炫的多,也許CATextLayer就是為了NSMutableAttributedString而生的,(哈哈,開(kāi)個(gè)玩笑)。下面就簡(jiǎn)要介紹下CATextLayer 的常規(guī)使用,不足之處,還望朋友們下面留言補(bǔ)充,不勝感謝。
蘋(píng)果官網(wǎng)給出CATextLayer的API解釋
初始化一個(gè)CATextLayer
CATextLayer *lary =[CATextLayer layer];
lary.string = @"dasfasa";
lary.bounds = CGRectMake(0, 0, 320, 20);
lary.font = @"HiraKakuProN-W3";//字體的名字 不是 UIFont
lary.fontSize = 12.f;//字體的大小
lary.alignmentMode = kCAAlignmentCenter;//字體的對(duì)齊方式
lary.position = CGPointMake(160, 410);
lary.foregroundColor =[UIColor redColor].CGColor;//字體的顏色
[self.view.layer addSublayer:lary];
/*
@property CGFloat contentsScale; 使用CATextLayer設(shè)置文本,可能會(huì)產(chǎn)生模糊狀態(tài),因?yàn)樵撃J(rèn)的分辨率不是retina,設(shè)置如下代碼即可:
*/
- CATextLayer與 CAGradientLayer(漸變圖層)結(jié)合,[金閃閃動(dòng)畫(huà)字體]

- CATextLayer與CAShapeLayer(波浪)

富文本AttributedString
AttributedString可以分為NSAttributedString和NSMutableAttributedString兩種。在使用中通過(guò)將AttributedString賦值給控件的 attributedText 屬性來(lái)添加文字樣式。有屬性的控件有UILabel、UITextField和UITextView。
-
使用方式一
初始化一個(gè)NSMutableAttributedString,然后向里面添加文字樣式,將其賦給控件的 *attributedText*屬性。 NSString *str = @"相看兩不厭,唯有敬亭山"; //創(chuàng)建NSMutableAttributedString NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc]initWithString:str]; //設(shè)置字體和設(shè)置字體的范圍 [attrStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:30.0f] range:NSMakeRange(0, 3)]; //添加文字顏色 [attrStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(17, 7)]; UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(60, 100, 200, 0)]; label.backgroundColor = [UIColor greenColor]; //自動(dòng)換行 label.numberOfLines = 0; //設(shè)置label的富文本 label.attributedText = attrStr; //label高度自適應(yīng) [label sizeToFit]; [self.view addSubview:label]; -
使用方式二
創(chuàng)建屬性字典,并將各種屬性初始化。賦值,
并利用方法appendAttributedString: 添加入NSMutableAttributedString,將其賦給控件的attributedText屬性。**//初始化NSMutableAttributedString NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]init]; //設(shè)置字體格式和大小NSString *str0 = @"設(shè)置字體格式和大小"; NSDictionary *dictAttr0 = @{NSFontAttributeName:[UIFont systemFontOfSize:14]}; NSAttributedString *attr0 = [[NSAttributedString alloc]initWithString:str0 attributes:dictAttr0]; [attributedString appendAttributedString:attr0]; //設(shè)置字體顏色NSString *str1 = @"\n設(shè)置字體顏色\n"; NSDictionary *dictAttr1 = @{NSForegroundColorAttributeName:[UIColor purpleColor]}; NSAttributedString *attr1 = [[NSAttributedString alloc]initWithString:str1 attributes:dictAttr1]; [attributedString appendAttributedString:attr1]; UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(60, 100, 300, 0)]; label.backgroundColor = [UIColor lightGrayColor]; //自動(dòng)換行 label.numberOfLines = 0; //設(shè)置label的富文本 label.attributedText = attributedString; //label高度自適應(yīng)[label sizeToFit]; [self.view addSubview:label];
文本屬性Attributes
1.直接上干貨,多個(gè)屬性可以一同使用
//NSFontAttributeName 字號(hào) UIFont 默認(rèn)12
//NSParagraphStyleAttributeName 段落樣式 NSParagraphStyle
//NSForegroundColorAttributeName 前景色 UIColor
//NSBackgroundColorAttributeName 背景色 UIColor
//NSObliquenessAttributeName 字體傾斜 NSNumber
//NSExpansionAttributeName 字體加粗 NSNumber 比例 0就是不變 1增加一倍
//NSKernAttributeName 字間距 CGFloat
//NSUnderlineStyleAttributeName 下劃線(xiàn) 1或0
//NSUnderlineColorAttributeName 下劃線(xiàn)顏色
//NSStrikethroughStyleAttributeName 刪除線(xiàn) 1或0
//NSStrikethroughColorAttributeName 某種顏色
//NSStrokeColorAttributeName same as ForegroundColor
//NSStrokeWidthAttributeName CGFloat
//NSLigatureAttributeName 連筆字 1或0 沒(méi)看出效果
//NSShadowAttributeName 陰影 NSShawdow
//NSTextEffectAttributeName 設(shè)置文本特殊效果,取值為 NSString 對(duì)象,目前只有圖版印刷效果可用:
//NSAttachmentAttributeName NSTextAttachment 設(shè)置文本附件,常用插入圖片
//NSLinkAttributeName 鏈接 NSURL (preferred) or NSString
//NSBaselineOffsetAttributeName 基準(zhǔn)線(xiàn)偏移 NSNumber
//NSWritingDirectionAttributeName 文字方向 @[@(1),@(2)] 分別代表不同的文字出現(xiàn)方向等等,我想你一定用不到它 - -
//NSVerticalGlyphFormAttributeName 水平或者豎直文本 1豎直 0水平 在iOS沒(méi)卵用,不支持豎版
2.設(shè)置段落樣式:段落樣式中允許你設(shè)置文字與文字之間的行間距、字符間距、以及對(duì)齊模式,但是注意的是,在設(shè)置段落樣式的時(shí)候,必須保證控件的
numberofline屬性必須為0
NSMutableAttributedString* str2 = [[NSMutableAttributedString alloc]initWithString:@"這是測(cè)試段落樣式的文字,這是測(cè)試段落樣式的文字,這是測(cè)試段落樣式的文字,這是測(cè)試段落樣式的文字,這是測(cè)試段落樣式的文字,這是測(cè)試段落樣式的文字。"];
NSMutableParagraphStyle* paragraphStyle = [[NSMutableParagraphStyle alloc]init];
//對(duì)齊模式
//NSTextAlignmentCenter;//居中
//NSTextAlignmentLeft //左對(duì)齊
//NSTextAlignmentCenter //居中
//NSTextAlignmentRight //右對(duì)齊
//NSTextAlignmentJustified//最后一行自然對(duì)齊
//NSTextAlignmentNatural //默認(rèn)對(duì)齊腳本
[paragraphStyle setAlignment:NSTextAlignmentLeft];
//換行裁剪模式
//NSLineBreakByWordWrapping = 0,//以空格為邊界,保留單詞
//NSLineBreakByCharWrapping, //保留整個(gè)字符
//NSLineBreakByClipping, //簡(jiǎn)單剪裁,到邊界為止
//NSLineBreakByTruncatingHead, //按照"……文字"顯示
//NSLineBreakByTruncatingTail, //按照"文字……文字"顯示
//NSLineBreakByTruncatingMiddle //按照"文字……"顯示
[paragraphStyle setLineBreakMode:NSLineBreakByCharWrapping];
//行間距
[paragraphStyle setLineSpacing:5.f];
//字符間距
[paragraphStyle setParagraphSpacing:2.f];
[str2 addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [str2 length])];
**最后,將上面設(shè)置**文字**樣式,設(shè)置**段落**樣式的兩部分代碼分別加入U(xiǎn)ILable attributedText 來(lái)查看結(jié)果:**
設(shè)置段落格式示例
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineSpacing = Scale_Y(7);// 字體的行間距
paragraphStyle.paragraphSpacing = Scale_Y(3);
attributes = @{
NSFontAttributeName:[UIFont systemFontOfSize:SMALL_FONT],
NSParagraphStyleAttributeName:paragraphStyle,
NSForegroundColorAttributeName:[UIColor whiteColor]
};
_textV.attributedText = [[NSAttributedString alloc] initWithString:_textV.text attributes:attributes];
_textV.textColor = [UIColor whiteColor];
值得注意的地方是 drawAtPoint和drawInRect的區(qū)別是后一個(gè)可以自動(dòng)換行,不過(guò)代價(jià)是 不設(shè)置屬性,都是默認(rèn)的屬性有時(shí)候是無(wú)法接受的。