iOS 從繪圖開始:DrawRect&CoreText,iosdrawrect

來源:互聯網
上載者:User

iOS 從繪圖開始:DrawRect&CoreText,iosdrawrect

- (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)];}

上面的代碼,是在UIView上繪製一個圓形比例圖和文字。

 

下面是CoreText在Label上的使用。

 1 UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(60, 2, sectionHeader.bounds.size.width-60-15-10, 56)]; 2 titleLabel.font = [UIFont systemFontOfSize:17.0f]; 3 [sectionHeader addSubview:titleLabel]; 4 NSString *titleStr = [NSString stringWithFormat:@"你還有%ld未通過", courseCount]; 5 NSInteger courseCount = [obligatoryCourseArray count]; 6  7  // 設定標籤文字 8  NSMutableAttributedString *attrituteString = [[NSMutableAttributedString alloc] initWithString:titleStr]; 9 // 設定標籤文字的屬性10 [attrituteString setAttributes:@{NSForegroundColorAttributeName : [UIColor redColor],   NSFontAttributeName : [UIFont systemFontOfSize:25]} range:NSMakeRange(3, [NSString stringWithFormat:@"%ld", courseCount].length)];11 // 顯示在Label上12  titleLabel.attributedText = attrituteString;

當然Label上繪製的Text的屬性可以用上面的在UIView上繪製中的方法。

 

再來看一個UIImageView的:

 1  { 2          3         UIImageView *obligatoryImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 30, tableView.bounds.size.width, ([UIScreen mainScreen].bounds.size.height-20-44)/3-30*2)]; 4         [cell addSubview:obligatoryImageView]; 5         obligatoryImageView.backgroundColor = [UIColor whiteColor]; 6         UIGraphicsBeginImageContext(obligatoryImageView.bounds.size); 7         [obligatoryImageView.image drawInRect:CGRectMake(0, 0, obligatoryImageView.bounds.size.width, obligatoryImageView.bounds.size.height)]; 8         CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);  //邊緣樣式 9         CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 0.2);  //線寬10         CGContextSetAllowsAntialiasing(UIGraphicsGetCurrentContext(), YES);11         CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(),153/255.0,153/255.0,153/255.0,1.0);  //顏色12         CGContextBeginPath(UIGraphicsGetCurrentContext());13         CGContextMoveToPoint(UIGraphicsGetCurrentContext(), 35, 5);  //起點座標14         CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), self.view.bounds.size.width-35, 5);//終點座標15         CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), self.view.bounds.size.width-35, (obligatoryImageView.bounds.size.height-5));16         CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), 35, (obligatoryImageView.bounds.size.height-5));17         CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), 35, 5);18         19         CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), 35, (obligatoryImageView.bounds.size.height-5)/5+5);20         CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), self.view.bounds.size.width-35, (obligatoryImageView.bounds.size.height-5)/5+5);21         22         CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), self.view.bounds.size.width-35, (obligatoryImageView.bounds.size.height-5)/5*2+5);23         CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), 35, (obligatoryImageView.bounds.size.height-10)/5*2+5);24         25         CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), 35, (obligatoryImageView.bounds.size.height-10)/5*3+5);26         CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), self.view.bounds.size.width-35, (obligatoryImageView.bounds.size.height-10)/5*3+5);27         28         CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), self.view.bounds.size.width-35, (obligatoryImageView.bounds.size.height-10)/5*4+5);29         CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), 35, (obligatoryImageView.bounds.size.height-10)/5*4+5);30         31         CGContextStrokePath(UIGraphicsGetCurrentContext());32         obligatoryImageView.image=UIGraphicsGetImageFromCurrentImageContext();33         UIGraphicsEndImageContext();34         35         NSArray *obligatoryArray = [courseArray objectAtIndex:indexPath.row];36         37         UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 30)];38         titleLabel.textColor = [UIColor murpTextLowColor];39         titleLabel.textAlignment = NSTextAlignmentCenter;40         titleLabel.font = [UIFont systemFontOfSize:15.0f];41         titleLabel.text = [[obligatoryArray objectAtIndex:0] objectForKey:@"Extend2"];42         [cell addSubview:titleLabel];43         44         for (int i = 0; i <= 5; i ++) {45             46             UILabel *left1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 30+(obligatoryImageView.bounds.size.height-10)/5*i, 35, 10)];47             left1.textColor = [UIColor murpTextLowColor];48             left1.textAlignment = NSTextAlignmentRight;49             left1.font = [UIFont systemFontOfSize:12.0f];50             left1.text = [NSString stringWithFormat:@"%d%%", 100-20*i];51             [cell addSubview:left1];52             53             if (i < obligatoryArray.count-1) {54                 55                 UILabel *down = [[UILabel alloc] initWithFrame:CGRectMake(35+(tableView.bounds.size.width-35*2)/5*i, ([UIScreen mainScreen].bounds.size.height-20-44)/3-30, (tableView.bounds.size.width-35*2)/5, 30)];56                 down.textColor = [UIColor murpTextLowColor];57                 down.textAlignment = NSTextAlignmentCenter;58                 down.font = [UIFont systemFontOfSize:12.0f];59                 [cell addSubview:down];60                 down.text = [NSString stringWithFormat:@"%@分",[[obligatoryArray objectAtIndex:i+1] objectForKey:@"Extend1"]];61                 62                 CGFloat heights = [[[obligatoryArray objectAtIndex:i+1] objectForKey:@"Extend3"] floatValue]/100.0*(obligatoryImageView.bounds.size.height-5*2);63                 UIImageView *heightImageView = [[UIImageView alloc] initWithFrame:CGRectMake(35+(tableView.bounds.size.width-35*2)/5*i+5, (([UIScreen mainScreen].bounds.size.height-20-44)/3-30-5)-heights, (tableView.bounds.size.width-35*2)/5-10, heights)];64                 heightImageView.backgroundColor = [UIColor murpThemeGreenColor];65                 [cell addSubview:heightImageView];66                 67                 UILabel *heightsLabel = [[UILabel alloc] initWithFrame:CGRectMake(35+(tableView.bounds.size.width-35*2)/5*i+5, (([UIScreen mainScreen].bounds.size.height-20-44)/3-30-5)-heights-20, (tableView.bounds.size.width-35*2)/5-10, 20)];68                 heightsLabel.textColor = [UIColor murpTextLowColor];69                 heightsLabel.textAlignment = NSTextAlignmentCenter;70                 heightsLabel.font = [UIFont systemFontOfSize:10.0f];71                 [cell addSubview:heightsLabel];72                 heightsLabel.text = [[obligatoryArray objectAtIndex:i+1] objectForKey:@"Extend2"];73             }74         }75     }

 

其實我也不知道想說什麼,喲嘿,最後看個效果吧:

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.