http://blog.csdn.net/xcysuccess3 版权所有 ,转载请说明
1.竖排绘制
- (void)drawRect:(CGRect)rect //竖排绘制
{
// Drawing code here.
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetTextMatrix(context, CGAffineTransformIdentity);
CGContextTranslateCTM(context, 0, self.bounds.size.height+20);
CGContextScaleCTM(context, 1.0, -1.0);
CGContextSetTextMatrix(context, CGAffineTransformIdentity);
NSAttributedString *str = [[NSAttributedString alloc ]
initWithString:@"学习 Core Text. Learning Core Text. 中华人民共和国。"
attributes:[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES], (NSString *)kCTVerticalFormsAttributeName, nil]];
CFAttributedStringRef attrString = (CFAttributedStringRef)str;
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(attrString);
CGMutablePathRef path = CGPathCreateMutable();
CGRect bounds = CGRectMake(10.0, 10.0, 200.0, 200.0);
CGPathAddRect(path, NULL, bounds);
CTFrameRef frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, 0), path, (CFDictionaryRef)[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithUnsignedInt:kCTFrameProgressionRightToLeft], (NSString *)kCTFrameProgressionAttributeName, nil]);
CTFrameDraw(frame, context);
CFRelease(attrString);
CFRelease(framesetter);
CFRelease(frame);
CFRelease(path);
}
这里一定要是给NSAttrubuted字设置好属性,否则中文将是旋转角度。
2.旋转角度绘制
- (void)drawRect:(CGRect)rect //旋转 { [super drawRect:rect]; CGContextRef context = UIGraphicsGetCurrentContext(); // CGContextSetTextMatrix(context, CGAffineTransformIdentity); CGContextTranslateCTM(context, 0, self.bounds.size.height+20); // CGContextScaleCTM(context, 1.0, -1.0); CGContextRotateCTM(context, radians(90)); CGContextTranslateCTM(context, 20, -460); CGMutablePathRef path = CGPathCreateMutable(); //1 CGPathAddRect(path, NULL, self.bounds ); NSAttributedString* attString1 = [[[NSAttributedString alloc] initWithString:@"Hello core text world!"] autorelease]; //2 CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)attString1); //3 CTFrameRef frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, [attString1 length]), path, NULL); CTFrameDraw(frame, context); //4 CFRelease(frame); //5 CFRelease(path); CFRelease(framesetter); }
这里的坐标系大家可以好好摸索一下。这个代码是我自己试出来的。坐标系翻转之前为什么是self.bounds.size+100,而不是-100我也不懂。望有高手解答。这里的原始坐标是x向右,y向下。在屏幕底部。这个我看网上说是Coretext的专有属性。
起点随笔
2012-10-23
ios交流QQ群:237305299