CAShapeLayer
CAShapeLayer
是一個(gè)通過(guò)矢量圖形而不是bitmap來(lái)繪制的圖層子類慈省。你指定諸如顏色和線寬等屬性砂竖,用CGPath
來(lái)定義想要繪制的圖形,最后CAShapeLaye
r就自動(dòng)渲染出來(lái)了。當(dāng)然,你也可以用Core Graphics
直接向原始的CALyer
的內(nèi)容中繪制一個(gè)路徑刹衫,相比直下,使用CAShapeLayer有以下一些優(yōu)點(diǎn):
- 渲染快速搞挣。
- 高效使用內(nèi)存带迟。
- 不會(huì)被圖層邊界剪裁掉。
- 不會(huì)出現(xiàn)像素化囱桨。
創(chuàng)建一個(gè)path畫(huà)一個(gè)圓
UIBezierPath *path = [[UIBezierPath alloc] init];
[path addArcWithCenter:CGPointMake(100, 100) radius:25 startAngle:0 endAngle:2*M_PI clockwise:YES];
CAShapeLayer *shapeLayer = [CAShapeLayer layer];
shapeLayer.strokeColor = [UIColor redColor].CGColor;
shapeLayer.fillColor = [UIColor clearColor].CGColor;
shapeLayer.lineWidth = 5;
shapeLayer.lineJoin = kCALineJoinRound;
shapeLayer.lineCap = kCALineCapRound;
shapeLayer.path = path.CGPath;
//add it to our view
[self.layer addSublayer:shapeLayer];
還可以利用CAShapeLayer畫(huà)一些不規(guī)則圖形
CAShapeLayer
為創(chuàng)建圓角視圖提供了一個(gè)方法仓犬,就是CALayer
的cornerRadius
屬性。雖然使用CAShapeLayer
類需要更多的工作舍肠,但是它有一個(gè)優(yōu)勢(shì)就是可以單獨(dú)指定每個(gè)角搀继。
//define path parameters
CGRect rect = CGRectMake(50, 50, 100, 100);
CGSize radii = CGSizeMake(20, 20);
UIRectCorner corners = UIRectCornerTopRight | UIRectCornerBottomRight | UIRectCornerBottomLeft;
//create path
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:rect byRoundingCorners:corners cornerRadii:radii];
CAShapeLayer *shapeLayer = [CAShapeLayer layer];
shapeLayer.strokeColor = [UIColor redColor].CGColor;
shapeLayer.fillColor = [UIColor clearColor].CGColor;
shapeLayer.lineWidth = 5;
shapeLayer.lineJoin = kCALineJoinRound;
shapeLayer.lineCap = kCALineCapRound;
shapeLayer.path = path.CGPath;
//add it to our view
[self.layer addSublayer:shapeLayer];
CATextLayer
CATextLayer
是CALayer的子類,它以圖層的形式包含了UILabel
幾乎所有的繪制特性翠语,并且額外的提供了一些新的特性叽躯,并且額外的提供了一些新的特性。同樣CATextLayer
比UILabel
渲染得快得多肌括。
嘗試用CATextLayer
實(shí)現(xiàn)一個(gè)UILabel
的功能
CATextLayer *textLayer = [CATextLayer layer];
textLayer.frame = self.bounds;
//此處要注意設(shè)置Layer的比例点骑,不然字看起來(lái)會(huì)失幀
textLayer.contentsScale = [UIScreen mainScreen].scale;
[self.layer addSublayer:textLayer];
//set text attributes
textLayer.foregroundColor = [UIColor blackColor].CGColor;
textLayer.alignmentMode = kCAAlignmentJustified;
textLayer.wrapped = YES;
//choose a font
UIFont *font = [UIFont systemFontOfSize:15];
//set layer font
CFStringRef fontName = (__bridge CFStringRef)font.fontName;
CGFontRef fontRef = CGFontCreateWithFontName(fontName);
textLayer.font = fontRef;
textLayer.fontSize = font.pointSize;
CGFontRelease(fontRef);
textLayer.string = text;
CATextLayer
的font
屬性不是一個(gè)UIFont類型,而是一個(gè)CFTypeRef
類型谍夭。這樣可以根據(jù)你的具體需要來(lái)決定字體屬性應(yīng)該是用CGFontRef
類型還是CTFontRef
類型(Core Text
字體)黑滴。同時(shí)字體大小也是用fontSize屬性單獨(dú)設(shè)置的,因?yàn)?code>CTFontRef和CGFontRef
并不像UIFont
一樣包含點(diǎn)大小紧索。這個(gè)例子會(huì)告訴你如何將UIFont
轉(zhuǎn)換成CGFontRef
袁辈。
另外,CATextLayer
的string
屬性并不是你想象的NSString
類型珠漂,而是id
類型晚缩。這樣你既可以用NSString
也可以用NSAttributedString
來(lái)指定文本了(注意,NSAttributedString
并不是NSString
的子類)媳危。屬性化字符串是iOS用來(lái)渲染字體風(fēng)格的機(jī)制橡羞,它以特定的方式來(lái)決定指定范圍內(nèi)的字符串的原始信息,比如字體济舆,顏色卿泽,字重,斜體等滋觉。
富文本
讓我們編輯一下示例使用到NSAttributedString
.iOS 6及以上我們可以用新的NSTextAttributeName
實(shí)例來(lái)設(shè)置我們的字符串屬性签夭,但是練習(xí)的目的是為了演示在iOS 5及以下,所以我們用了Core Text
椎侠,也就是說(shuō)你需要把Core Text framework
添加到你的項(xiàng)目中第租。否則,編譯器是無(wú)法識(shí)別屬性常量的我纪。
//create a text layer
CATextLayer *textLayer = [CATextLayer layer];
textLayer.frame = self.labelView.bounds;
textLayer.contentsScale = [UIScreen mainScreen].scale;
[self.labelView.layer addSublayer:textLayer];
//set text attributes
textLayer.alignmentMode = kCAAlignmentJustified;
textLayer.wrapped = YES;
//choose a font
UIFont *font = [UIFont systemFontOfSize:15];
//choose some text
NSString *text = @"Lorem ipsum dolor sit amet, consectetur adipiscing \ elit. Quisque massa arcu, eleifend vel varius in, facilisis pulvinar \ leo. Nunc quis nunc at mauris pharetra condimentum ut ac neque. Nunc \ elementum, libero ut porttitor dictum, diam odio congue lacus, vel \ fringilla sapien diam at purus. Etiam suscipit pretium nunc sit amet \ lobortis";
//create attributed string
NSMutableAttributedString *string = nil;
string = [[NSMutableAttributedString alloc] initWithString:text];
//convert UIFont to a CTFont
CFStringRef fontName = (__bridge CFStringRef)font.fontName;
CGFloat fontSize = font.pointSize;
CTFontRef fontRef = CTFontCreateWithName(fontName, fontSize, NULL);
//set text attributes
NSDictionary *attribs = @{
(__bridge id)kCTForegroundColorAttributeName:(__bridge id)[UIColor blackColor].CGColor,
(__bridge id)kCTFontAttributeName: (__bridge id)fontRef
};
[string setAttributes:attribs range:NSMakeRange(0, [text length])];
attribs = @{
(__bridge id)kCTForegroundColorAttributeName: (__bridge id)[UIColor redColor].CGColor,
(__bridge id)kCTUnderlineStyleAttributeName: @(kCTUnderlineStyleSingle),
(__bridge id)kCTFontAttributeName: (__bridge id)fontRef
};
[string setAttributes:attribs range:NSMakeRange(6, 5)];
//release the CTFont we created earlier
CFRelease(fontRef);
//set layer text
textLayer.string = string;
}
@end
未完....