zoukankan      html  css  js  c++  java
  • iPhone中自绘实现步骤

    1. 继承@interface MyView : UIView {

    2. 实现- (void)drawRect:(CGRect)rect

    3. 调用addSubView把新生成的view加入进来显示:addSubView[window addSubview:viewController.view];

    4.示例代码

     1 - (void)drawRect:(CGRect)rect {
     2     // create the bitmap context
     3     CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
     4     CGContextRef context = CGBitmapContextCreate(nil,100,100,8,400, colorSpace,kCGImageAlphaPremultipliedLast);
     5     CFRelease(colorSpace);
     6     
     7     //    create an arrow image
     8     // set the fill color
     9     CGColorRef fillColor = [[UIColor blackColor] CGColor];
    10     CGContextSetFillColor(context, CGColorGetComponents(fillColor));
    11     
    12     CGContextBeginPath(context);
    13     CGContextMoveToPoint(context, 8.0f, 13.0f);
    14     CGContextAddLineToPoint(context, 24.0f, 4.0f);
    15     CGContextAddLineToPoint(context, 24.0f, 22.0f);
    16     CGContextClosePath(context);
    17     CGContextFillPath(context);
    18     CGContextSelectFont ( context, "Arial", 10.f, kCGEncodingMacRoman );
    19     CGContextSetRGBFillColor ( context, 0.0f, 0.0f, 0.f, 1.f );
    20     CGContextSetShouldAntialias ( context, 0 );    
    21     CGContextShowText(context, "hh", 2);
    22     
    23     // convert the context into a CGImageRef
    24     CGImageRef image = CGBitmapContextCreateImage(context);
    25     CGContextRelease(context);
    26     
    27     UIImage* image2 = [UIImage imageWithCGImage:image];
    28     [image2 drawInRect:CGRectMake(0, 0, 120, 160)];
    29     
    30     NSString* myStr = @"中文";
    31     UIFont* font = [UIFont systemFontOfSize:12.0];
    32     [myStr drawInRect: CGRectMake(160, 240, 100, 130) withFont:font lineBreakMode:UILineBreakModeWordWrap alignment:UITextAlignmentCenter];
    33     
    34 }
  • 相关阅读:
    layer子层给父层页面元素赋值,以达到向父层页面传值的效果
    根据HttpServletRequest获取用户真实IP地址
    KVM--安装及初步使用
    eslint 踩坑 -- error '***' is assigned a value but never used no-unused-vars
    eslint 效验规则
    leetcode407接雨水II
    leetcodej剑指offer41.数据流中的中位数
    leetcode44通配符匹配
    leetcode955K连续位的最小反转次数
    leetcode638大礼包
  • 原文地址:https://www.cnblogs.com/dinghing154/p/2613847.html
Copyright © 2011-2022 走看看