@interface CATextLayer : CALayer
{
@private
struct CATextLayerPrivate *_state;
}
//需要呈现的文本,可以是NSString和NSAttributedString,默认情况下为nil
@property(copy) id string;
//使用的字体,可以是CTFontRef, CGFontRef或者是一个font的名字,默认为Helvetica font,当且仅当string为NSAttributedString时使用
@property CFTypeRef font;
//字体的大小,默认为36,当且仅当string不为NSAttributedString或者Animatable时使用
@property CGFloat fontSize;
//用来绘制text的颜色,默认为不透明白色,当且仅当string不为NSAttributedString和Animatable时使用
@property CGColorRef foregroundColor;
//默认为NO,若为YES表示可自适应layer的bounds
@property(getter=isWrapped) BOOL wrapped;
//描述string如何在层内实现截断自适应bounds,可以为‘none’,‘start’,以及‘end’,默认情况下为'none‘
@property(copy) NSString *truncationMode;
//描述各个行间对齐的方式,可以为‘natural','left','right','center',以及'natural',默认为'natural'
@property(copy) NSString *alignmentMode;
@end
CATextLayer *textLayer = [CATextLayer layer];
[textLayer setFrame:CGRectMake(10, 100, 300, 50)];
NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc]initWithString:@"CATextLayer测试学习" attributes:@{NSForegroundColorAttributeName :[UIColor redColor]}];
[attributeString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:30] range:NSMakeRange(0, 11)];
[attributeString addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(11, 4)];
[textLayer setString:attributeString];
[textLayer setAlignmentMode:@"center"];//设置文本对齐方式
[textLayer setFontSize:13];//此方法在此无效,因为string为NSMutableAttributedString
[self.view.layer addSublayer:textLayer];