IOS绘图使用

1.竖排绘制

  1. - (void)drawRect:(CGRect)rect //竖排绘制   
  2. {  
  3.     // Drawing code here.   
  4.     CGContextRef context = UIGraphicsGetCurrentContext();  
  5.     CGContextSetTextMatrix(context, CGAffineTransformIdentity);  
  6.     CGContextTranslateCTM(context, 0, self.bounds.size.height+20);  
  7.     CGContextScaleCTM(context, 1.0, -1.0);  
  8.     CGContextSetTextMatrix(context, CGAffineTransformIdentity);  
  9.         NSAttributedString *str = [[NSAttributedString alloc ]  
  10.                                initWithString:@"学习 Core Text. Learning Core Text. 中华人民共和国。"  
  11.                                attributes:[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES], (NSString *)kCTVerticalFormsAttributeName, nil]];  
  12.     CFAttributedStringRef attrString = (CFAttributedStringRef)str;  
  13.     CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(attrString);  
  14.     CGMutablePathRef path = CGPathCreateMutable();  
  15.     CGRect bounds = CGRectMake(10.0, 10.0, 200.0, 200.0);  
  16.     CGPathAddRect(path, NULL, bounds);  
  17.     CTFrameRef frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, 0), path, (CFDictionaryRef)[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithUnsignedInt:kCTFrameProgressionRightToLeft], (NSString *)kCTFrameProgressionAttributeName, nil]);  
  18.     CTFrameDraw(frame, context);  
  19.     CFRelease(attrString);  
  20.     CFRelease(framesetter);  
  21.     CFRelease(frame);  
  22.     CFRelease(path);  
  23. }  
- (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.旋转角度绘制

  1. <PRE class=cpp sizcache="3" sizset="3" name="code"><SPAN style="FONT-SIZE: 18px"></SPAN><PRE class=cpp name="code">- (void)drawRect:(CGRect)rect //旋转   
  2. {  
  3.     [super drawRect:rect];  
  4.     CGContextRef context = UIGraphicsGetCurrentContext();  
  5.       
  6.  //   CGContextSetTextMatrix(context, CGAffineTransformIdentity);   
  7.     CGContextTranslateCTM(context, 0, self.bounds.size.height+20);  
  8. //   
  9.     CGContextScaleCTM(context, 1.0, -1.0);  
  10.     CGContextRotateCTM(context, radians(90));  
  11.       
  12.      
  13.     CGContextTranslateCTM(context, 20, -460);  
  14.       
  15.       
  16.     CGMutablePathRef path = CGPathCreateMutable(); //1   
  17.     CGPathAddRect(path, NULL, self.bounds );  
  18.       
  19.     NSAttributedString* attString1 = [[[NSAttributedString alloc]  
  20.                                       initWithString:@"Hello core text world!"] autorelease]; //2   
  21.       
  22.     CTFramesetterRef framesetter =  
  23.     CTFramesetterCreateWithAttributedString((CFAttributedStringRef)attString1); //3   
  24.     CTFrameRef frame =  
  25.     CTFramesetterCreateFrame(framesetter,  
  26.                              CFRangeMake(0, [attString1 length]), path, NULL);  
  27.       
  28.     CTFrameDraw(frame, context); //4   
  29.       
  30.     CFRelease(frame); //5   
  31.     CFRelease(path);  
  32.     CFRelease(framesetter);    
  33. }</PRE><BR>  
  34. 这里的坐标系大家可以好好摸索一下。这个代码是我自己试出来的。坐标系翻转之前为什么是self.bounds.size+100,而不是-100我也不懂。望有高手解答。这里的原始坐标是x向右,y向下。在屏幕底部。这个我看网上说是Coretext的专有属性。<BR>  
  35. <PRE></PRE>  
  36. <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>  
  37. </PRE><PRE class=cpp name="code"><SPAN style="FONT-SIZE: 18px">  
  38. </SPAN></PRE><PRE class=cpp name="code"><SPAN style="FONT-SIZE: 18px">  
  39. </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>  
  40. <PRE></PRE>  
  41. <PRE></PRE>  
  42. <PRE></PRE>  
  43. <PRE></PRE>  
  44. <PRE></PRE>  
  45.   
  46. </PRE></PRE></PRE></PRE>  
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值