UIKit和Core Graphics绘图——字符串,线条绘制

 

// 覆盖drawRect方法,你可以在此自定义绘画和动画

- (void)drawRect:(CGRect)rect

{

    //An opaque type that represents a Quartz 2D drawing environment.

    //一个不透明类型的Quartz 2D绘画环境,相当于一个画布,你可以在上面任意绘画

    CGContextRef context = UIGraphicsGetCurrentContext();

    

    UIFont *font = [UIFont fontWithName:@"PingFang HK" size:30.0f];

    NSDictionary* dic = [NSDictionary dictionaryWithObjectsAndKeys:font, NSFontAttributeName, nil];

    

    [[UIColor brownColor] set];       //设置上下文使用的颜色

    CGContextSetLineWidth(context, 2.0f);

    CGContextMoveToPoint(context, 2.0f, 200.0f);  // 画笔移动到某点

    CGContextAddLineToPoint(context, [UIScreen mainScreen].bounds.size.width-4, 200.0f);

    CGContextAddLineToPoint(context, [UIScreen mainScreen].bounds.size.width-4, 400.0f);

    CGContextAddLineToPoint(context, 2.f, 400.0f);

    CGContextClosePath(context);//闭合矩形

    for (int i = 1; i<200/50; i++) {

        CGContextMoveToPoint(context, 2.0f, 200.0 +50*i);  // 画笔移动到某点

        CGContextAddLineToPoint(context, [UIScreen mainScreen].bounds.size.width-4, 200.0 +50*i);

        NSString *tempStr =[NSString stringWithFormat:@"项目%d",i];

        CGFloat height = [self calculateCellHeight:tempStr fontSize:30 maxWidth:100];//字体高

        [[NSString stringWithFormat:@"项目%d",i] drawAtPoint:CGPointMake(5.0f, 200.0 +50*i + (50 - height)/2) withAttributes:dic];

    }

    int count = (int)([UIScreen mainScreen].bounds.size.width-4)/100;

    for (int i = 1; i<count; i++) {

        CGContextMoveToPoint(context, 2.0f + 100*i, 200.0 );  // 画笔移动到某点

        CGContextAddLineToPoint(context, 2.0f + 100*i, 400.0f );

    }

    CGContextStrokePath(context);       //执行绘制

    

    //颜色填充

    CGRect strokeRect =  CGRectMake( 2.0f, 200.0f, [UIScreen mainScreen].bounds.size.width-5, 49);

    CGContextSetFillColorWithColor(context, [UIColor colorWithRed:0 green:0.5 blue:1 alpha:0.5].CGColor);

    CGContextFillRect(context,strokeRect);

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值