你的位置:首页 > 软件开发 > 操作系统 > iOS 从绘图开始:DrawRectCoreText

iOS 从绘图开始:DrawRectCoreText

发布时间:2015-08-05 19:00:13
- (void)drawRect:(CGRect)rect { // Drawing code //Quartz 2D绘画环境,一张画布 CGContextRef context = UIGraphicsGetCurrentContext(); //边框圆 背景圆 ...

iOS 从绘图开始:DrawRectCoreText

- (void)drawRect:(CGRect)rect {  // Drawing code    //Quartz 2D绘画环境,一张画布  CGContextRef context = UIGraphicsGetCurrentContext();  //边框圆 背景圆  CGContextSetRGBStrokeColor(context,1,1,0,1.0);//画笔线的颜色  CGContextSetLineWidth(context, 2.0);//线的宽度  //void CGContextAddArc(CGContextRef c,CGFloat x, CGFloat y,CGFloat radius,CGFloat startAngle,CGFloat endAngle, int clockwise)1弧度=180°/π (≈57.3°) 度=弧度×180°/π 360°=360×π/180 =2π 弧度  // x,y为圆点坐标,radius半径,startAngle为开始的弧度,endAngle为 结束的弧度,clockwise 0为顺时针,1为逆时针。  CGContextAddArc(context, 120, 120, 100, 0, 2*M_PI, 0); //添加一个圆  CGContextDrawPath(context, kCGPathStroke); //绘制路径    //所占比例圆弧  CGContextSetRGBStrokeColor(context,1,0,0,1.0);//画笔线的颜色  CGContextSetLineWidth(context, 3.0);//线的宽度  CGContextAddArc(context, 120, 120, 100, -90 * M_PI/180, (endRadius-90) * M_PI/180, 0); //添加一个圆  CGContextDrawPath(context, kCGPathStroke); //绘制路径      //绘制字符串  if ([[UIDevice currentDevice] systemVersion].floatValue >= 7.0) {        NSMutableAttributedString *mabstring = [[NSMutableAttributedString alloc]initWithString:drawText];    [mabstring beginEditing];        //对同一段字体进行多属性设置    NSMutableDictionary *attributes = [NSMutableDictionary dictionaryWithObject:(id)[UIColor redColor].CGColor forKey:(id)kCTForegroundColorAttributeName];//红色字体    //设置字体属性    CTFontRef font = CTFontCreateWithName(CFSTR("Georgia"), 14, NULL);    [attributes setObject:(id)(__bridge id)font forKey:(id)kCTFontAttributeName];//下划线    [mabstring endEditing];//结束编辑    [mabstring addAttributes:attributes range:NSMakeRange(0, mabstring.length)];        //开始绘制    CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)mabstring);    CGMutablePathRef Path = CGPathCreateMutable();    CGPathAddRect(Path, NULL ,CGRectMake(100 , -100 ,self.bounds.size.width-10 , self.bounds.size.height-10));    CTFrameRef frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, 0), Path, NULL);        //压栈,压入图形状态栈中.    CGContextSetTextMatrix(context , CGAffineTransformIdentity);    //保存现在得上下文图形状态。    CGContextSaveGState(context);    //x,y轴方向移动    CGContextTranslateCTM(context , 0 ,self.bounds.size.height);    //缩放x,y轴方向缩放,代码中坐标系转换是沿x轴翻转180度    CGContextScaleCTM(context, 1.0 ,-1.0);    CTFrameDraw(frame,context);//开始绘制大小    CGPathRelease(Path);//开始绘制路径    CFRelease(framesetter);  }else {        //表示开始绘制路径    CGContextBeginPath(context);    //设置文字大小    CGContextSelectFont(context, "Helvetica", 20, kCGEncodingMacRoman);    CGContextSetTextDrawingMode(context, kCGTextFill);    // 设置文本颜色字符为白色    CGContextSetRGBFillColor(context, 1.0, 0.0, 1.0, 1.0); //白色    CGContextSetTextMatrix(context, CGAffineTransformMakeScale(1.0, -1.0));    //绘制文字    CGContextShowTextAtPoint(context,20, 100, [@"characterAttribute" cStringUsingEncoding:[NSString defaultCStringEncoding]],                 @"characterAttribute".length);    CGContextStrokePath(context);    //表示结束绘制路径    CGContextClosePath(context);  }    /////////////////////一些字体属性设置////////////////////////  //    [mabstring addAttribute:(id)kCTFontAttributeName value:(__bridge id)font range:NSMakeRange(0, 4)];  //    [attributes setObject:(id)[NSNumber numberWithInt:kCTUnderlineStyleDouble] forKey:(id)kCTUnderlineStyleAttributeName];//下划线  //    [mabstring addAttributes:attributes range:NSMakeRange(0, 4)];//设置属性的文字范围    //设置斜体字  //    CTFontRef font = CTFontCreateWithName((CFStringRef)[UIFont italicSystemFontOfSize:20].fontName, 14, NULL);  //    [mabstring addAttribute:(id)kCTFontAttributeName value:(__bridge id)(font) range:NSMakeRange(0, 4)];}

 

海外公司注册、海外银行开户、跨境平台代入驻、VAT、EPR等知识和在线办理:https://www.xlkjsw.com

原标题:iOS 从绘图开始:DrawRectCoreText

关键词:IOS

IOS
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。