iOS图形处理类和NSAttributedString

NSFont*font = [NSFontfontWithName:@"Palatino-Roman"size:14.0];
NSDictionary*attrsDictionary = [NSDictionarydictionaryWithObject:font
                                    forKey:NSFontAttributeName];
NSAttributedString*attrString = [[NSAttributedStringalloc] initWithString:@"strigil"
            attributes:attrsDictionary];

NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc]initWithString:str];

    [attributeString setAttributes:@{NSForegroundColorAttributeName: [UIColor redColor], NSFontAttributeName:[UIFont systemFontOfSize:17]} range:NSMakeRange(2, 1)];
    NSRange range =[str rangeOfString:keyword];
    [attributeString setAttributes:@{NSForegroundColorAttributeName: [UIColor redColor],NSFontAttributeName:[UIFont systemFontOfSize:17]} range:range];

  // 绘制图片
    UIImage *image = [UIImage imageNamed:@"darkLight.png"];
    [image drawAtPoint:CGPointMake(120, 300)];
    
    
    // 绘制文字
    NSString *str = @"ABCDEF";
    UIFont *font = [UIFont systemFontOfSize:12];
    NSDictionary *dictory = [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName];
    //NSAttributedString *str1 = [[NSAttributedString alloc]initWithString:str attributes:dictory];
    // 通过画图的方式自动调用绘图功能
    [str drawInRect:CGRectMake(100, 100, 200, 50) withAttributes:dictory];
    
    
    // 绘制直线
    // 获取绘图的上下文
    CGContextRef ref = UIGraphicsGetCurrentContext();
    // 设置绘图的画笔的大小
    CGContextSetLineWidth(ref, 1.0);
    // 设置绘图的边线样式
    CGContextSetLineCap(ref, kCGLineCapRound);
    // 20个像素的虚线有10个空格
    //float lenghts [] = {20, 10};
    // 设置虚线
    //CGContextSetLineDash(ref, 0, lenghts, 2);
    // 设置线条的颜色
    CGContextSetStrokeColorWithColor(ref, [UIColor blackColor].CGColor);
    // 移动绘图点
    CGContextMoveToPoint(ref, 100, 100);
    // 绘制直线
    CGContextAddLineToPoint(ref, 220, 220);
    CGContextAddLineToPoint(ref, 100, 220);
    // 封闭曲线(这样只需填写3个点即可)
    CGContextClosePath(ref);
    // 开始绘制线并在view上显示(最终调用的方法)
    CGContextStrokePath(ref);
    
    
    // 绘制图
    CGContextSetFillColorWithColor(ref, [UIColor greenColor].CGColor);
    // 线条颜色
    CGContextSetStrokeColorWithColor(ref, [UIColor blackColor].CGColor);
    // 设置透明度
    CGContextSetAlpha(ref, 0.5);
    // 设置影子偏移量
    CGContextSetShadowWithColor(ref, CGSizeMake(20, 20), 20, [UIColor orangeColor].CGColor);
    // 设置图形样式
    CGContextAddRect(ref, CGRectMake(100, 100, 100, 100));
    //图形显示方式为:线条填充图一起显示(此方法一定要在绘制图形完成后使用,否则无效)
    CGContextDrawPath(ref, kCGPathFillStroke);
    CGContextStrokePath(ref);
    
    
    //绘制圆填充颜色
    CGContextSetFillColorWithColor(ref, [UIColor redColor].CGColor);
    //如果没有这句,那么下面绘制就是一个从0度到100度的圆型的一部分,因为没有设置move点
    CGContextMoveToPoint(ref, 150, 150);
    //(100为半径,最后的0说明时顺时针[逆时针为1])
    CGContextAddArc(ref, 150, 150, 100, 0 * M_PI / 180, 100 * M_PI / 180, 0);
    CGContextFillPath(ref);
    
    CGContextSetFillColorWithColor(ref, [UIColor blackColor].CGColor);
    CGContextMoveToPoint(ref, 150, 150);
    // 150 150是坐标  100是半径 M_PI是180度
    CGContextAddArc(ref, 150, 150, 100, 100 * M_PI / 180, 230 * M_PI / 180, 0);
    CGContextFillPath(ref);
    
    CGContextSetFillColorWithColor(ref, [UIColor blueColor].CGColor);
    CGContextMoveToPoint(ref, 150, 150);
    
    CGContextAddArc(ref, 150, 150, 100, 230 * M_PI / 180, 360 * M_PI / 180, 0);
    CGContextFillPath(ref);
    
    
    // 绘制曲线(100, 350是开始点)
    CGContextMoveToPoint(ref, 100, 350);
    //(120,250)是弧线顶点的坐标 (300,350)是弧线最右端的坐标(220, 250是弯曲的曲线上的一个点)
    CGContextAddQuadCurveToPoint(ref, 220, 250, 300, 350);
    CGContextStrokePath(ref);


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值