UI篇-CATextLayer和 富文本的交融

前言

CATextLayer適用于IOS或者MAC,比UIlablel 和 NSTextView 能做的事很多,可以這樣說UIlablel是通過CATextLayer實現(xiàn)的,身為CALayer的三大子類之一,它的功能遠比 UIlablel 強大的多的多,其最主要的特點是CATextLayer可以被NSMutableAttributedString直接附值。而NSMutableAttributedString有可以最自己內(nèi)容作出顏色以及大小的調(diào)整,這樣結(jié)合起來使用的話,就遠比UILabel 靈活的多,效果也酷炫的多,也許CATextLayer就是為了NSMutableAttributedString而生的,(哈哈,開個玩笑)。下面就簡要介紹下CATextLayer 的常規(guī)使用,不足之處,還望朋友們下面留言補充,不勝感謝。
蘋果官網(wǎng)給出CATextLayer的API解釋


初始化一個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;//字體的對齊方式    
lary.position = CGPointMake(160, 410);     
lary.foregroundColor =[UIColor redColor].CGColor;//字體的顏色  
[self.view.layer addSublayer:lary];
/*
 @property CGFloat contentsScale; 使用CATextLayer設(shè)置文本,可能會產(chǎn)生模糊狀態(tài),因為該默認的分辨率不是retina,設(shè)置如下代碼即可:
*/
  • CATextLayer與 CAGradientLayer(漸變圖層)結(jié)合,[金閃閃動畫字體]
字.gif
  • CATextLayer與CAShapeLayer(波浪)
波浪.gif

這里只提供思路 具體代碼地址

富文本AttributedString

AttributedString可以分為NSAttributedString和NSMutableAttributedString兩種。在使用中通過將AttributedString賦值給控件的 attributedText 屬性來添加文字樣式。有屬性的控件有UILabel、UITextField和UITextView。

  • 使用方式一

    初始化一個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];
    //自動換行
    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];
    //自動換行
    label.numberOfLines = 0;
    //設(shè)置label的富文本
    label.attributedText = attributedString;
    //label高度自適應(yīng)[label sizeToFit];
    [self.view addSubview:label];
    

文本屬性Attributes

1.直接上干貨,多個屬性可以一同使用

//NSFontAttributeName 字號 UIFont 默認12 
//NSParagraphStyleAttributeName 段落樣式 NSParagraphStyle 
//NSForegroundColorAttributeName 前景色 UIColor 
//NSBackgroundColorAttributeName 背景色 UIColor 
//NSObliquenessAttributeName 字體傾斜 NSNumber 
//NSExpansionAttributeName 字體加粗 NSNumber 比例 0就是不變 1增加一倍 
//NSKernAttributeName 字間距 CGFloat 
//NSUnderlineStyleAttributeName 下劃線 1或0 
//NSUnderlineColorAttributeName 下劃線顏色
 //NSStrikethroughStyleAttributeName 刪除線 1或0
 //NSStrikethroughColorAttributeName 某種顏色 
//NSStrokeColorAttributeName same as ForegroundColor 
//NSStrokeWidthAttributeName CGFloat 
//NSLigatureAttributeName 連筆字 1或0 沒看出效果 
//NSShadowAttributeName 陰影 NSShawdow
 //NSTextEffectAttributeName 設(shè)置文本特殊效果,取值為 NSString 對象,目前只有圖版印刷效果可用: 
//NSAttachmentAttributeName NSTextAttachment 設(shè)置文本附件,常用插入圖片 
//NSLinkAttributeName 鏈接 NSURL (preferred) or NSString
 //NSBaselineOffsetAttributeName 基準線偏移 NSNumber 
//NSWritingDirectionAttributeName 文字方向 @[@(1),@(2)] 分別代表不同的文字出現(xiàn)方向等等,我想你一定用不到它 - -
 //NSVerticalGlyphFormAttributeName 水平或者豎直文本 1豎直 0水平 在iOS沒卵用,不支持豎版

2.設(shè)置段落樣式:段落樣式中允許你設(shè)置文字與文字之間的行間距、字符間距、以及對齊模式,但是注意的是,在設(shè)置段落樣式的時候,必須保證控件的

numberofline屬性必須為0

NSMutableAttributedString* str2 = [[NSMutableAttributedString alloc]initWithString:@"這是測試段落樣式的文字,這是測試段落樣式的文字,這是測試段落樣式的文字,這是測試段落樣式的文字,這是測試段落樣式的文字,這是測試段落樣式的文字。"];
NSMutableParagraphStyle* paragraphStyle = [[NSMutableParagraphStyle alloc]init];
//對齊模式
//NSTextAlignmentCenter;//居中
//NSTextAlignmentLeft //左對齊
//NSTextAlignmentCenter //居中
//NSTextAlignmentRight //右對齊
//NSTextAlignmentJustified//最后一行自然對齊
//NSTextAlignmentNatural //默認對齊腳本
[paragraphStyle setAlignment:NSTextAlignmentLeft];
//換行裁剪模式
 //NSLineBreakByWordWrapping = 0,//以空格為邊界,保留單詞
//NSLineBreakByCharWrapping, //保留整個字符
//NSLineBreakByClipping, //簡單剪裁,到邊界為止
//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è)置**段落**樣式的兩部分代碼分別加入UILable  attributedText 來查看結(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ū)別是后一個可以自動換行,不過代價是 不設(shè)置屬性,都是默認的屬性有時候是無法接受的。


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容