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);
- }
- (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.旋转角度绘制
- <PRE class=cpp sizcache="3" sizset="3" name="code"><SPAN style="FONT-SIZE: 18px"></SPAN><PRE class=cpp name="code">- (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);
- }</PRE><BR>
- 这里的坐标系大家可以好好摸索一下。这个代码是我自己试出来的。坐标系翻转之前为什么是self.bounds.size+100,而不是-100我也不懂。望有高手解答。这里的原始坐标是x向右,y向下。在屏幕底部。这个我看网上说是Coretext的专有属性。<BR>
- <PRE></PRE>
- <PRE class=cpp name="code"><SPAN style="FONT-SIZE: 18px"></SPAN><P style="MARGIN-BOTTOM: 0px; FONT-SIZE: 14px; FONT-FAMILY: Arial; PADDING-BOTTOM: 0px; TEXT-ALIGN: left; PADDING-TOP: 0px; MARGIN-TOP: 0px; LINE-HEIGHT: 26px"><SPAN style="FONT-SIZE: 18px"> 起点随笔</SPAN></P><P style="MARGIN-BOTTOM: 0px; FONT-SIZE: 14px; FONT-FAMILY: Arial; PADDING-BOTTOM: 0px; TEXT-ALIGN: left; PADDING-TOP: 0px; MARGIN-TOP: 0px; LINE-HEIGHT: 26px"><SPAN style="FONT-SIZE: 18px"><SPAN style="WHITE-SPACE: pre"></SPAN> 2012-10-23</SPAN></P><P style="MARGIN-BOTTOM: 0px; FONT-SIZE: 14px; FONT-FAMILY: Arial; PADDING-BOTTOM: 0px; TEXT-ALIGN: left; PADDING-TOP: 0px; MARGIN-TOP: 0px; LINE-HEIGHT: 26px"><SPAN style="FONT-SIZE: 18px"><SPAN style="WHITE-SPACE: pre"></SPAN> ios交流QQ群:237305299</SPAN></P>
- </PRE><PRE class=cpp name="code"><SPAN style="FONT-SIZE: 18px">
- </SPAN></PRE><PRE class=cpp name="code"><SPAN style="FONT-SIZE: 18px">
- </SPAN></PRE><PRE class=cpp sizcache="3" sizset="9" name="code"><PRE class=cpp sizcache="3" sizset="10" name="code"><PRE class=cpp sizcache="3" sizset="11" name="code"><PRE></PRE>
- <PRE></PRE>
- <PRE></PRE>
- <PRE></PRE>
- <PRE></PRE>
- <PRE></PRE>
- </PRE></PRE></PRE></PRE>