IOS: DrawRect & amp; CoreText, iosdrawrect

Source: Internet
Author: User

IOS: DrawRect & CoreText, iosdrawrect

-(Void) drawRect :( CGRect) rect {// Drawing code // Quartz 2D painting environment, a canvas CGContextRef context = UIGraphicsGetCurrentContext (); // border circle background circle CGContextSetRGBStrokeColor (context, 1.0, 0, 2.0); // The color CGContextSetLineWidth (context,) of the paint brush line; // The width of the line // void CGContextAddArc (CGContextRef c, CGFloat x, CGFloat y, CGFloat radius, CGFloat startAngle, CGFloat endAngle, int clockwise) 1 radian = 180 °/π (≈ 57.3 °) degrees = radians × 180 °/π 360 ° = 360 × π/18 0 = 2 π radians // x, y is the dot coordinate, radius, startAngle is the start radians, endAngle is the end radians, clockwise 0 is clockwise, 1 is counter-clockwise. CGContextAddArc (context, 120,120,100, 0, 2 * M_PI, 0); // Add a circle CGContextDrawPath (context, kCGPathStroke); // draw the path // proportion of the arc CGContextSetRGBStrokeColor (context, context, 1.0, 0, 3.0); // The color CGContextSetLineWidth (context, 120,120,100) of the paint brush line; // The width of the line CGContextAddArc (context, 180,-90 * M_PI, (endRadius-90) * M_PI/180, 0); // Add a circle CGContextDrawPath (context, kCGPathStroke); // draw the path // draw the string if ([UIDevice currentDevic E] systemVersion]. floatValue> = 7.0) {NSMutableAttributedString * mabstring = [[NSMutableAttributedString alloc] initWithString: drawText]; [mabstring beginEditing]; // set NSMutableDictionary * attributes = [NSMutableDictionary dictionaryWithObject :( id) [UIColor redColor]. CGColor forKey :( id) kCTForegroundColorAttributeName]; // set the font attribute CTFontRef font = CTFontCreateWithName (CFSTR ("Georgia" ), 14, NULL); [attributes setObject :( id) (_ bridge id) font forKey :( id) kCTFontAttributeName]; // underline [mabstring endEditing]; // end Editing [mabstring addAttributes: attributes range: NSMakeRange (0, mabstring. length)]; // start to draw CTFramesetterRef framesetter = reverse (CFAttributedStringRef) mabstring); CGMutablePathRef Path = CGPathCreateMutable (); CGPathAddRect (100,-100, self. bounds. size. width-10, self. bounds. size. height-10); CTFrameRef frame = CTFramesetterCreateFrame (framesetter, CFRangeMake (0, 0), Path, NULL); // press the stack into the graphical state stack. CGContextSetTextMatrix (context, CGAffineTransformIdentity); // Save the current context graphics status. CGContextSaveGState (context); // x, moves CGContextTranslateCTM (context, 0, self. bounds. size. height); // scale x and Y-axis. In the code, the coordinate system transformation is to flip 180 degrees CGContextScaleCTM (context, 1.0,-1.0) along the x axis; CTFrameDraw (frame, context ); // start to draw the CGPathRelease (Path); // start to draw the Path CFRelease (framesetter);} else {// indicates to start to draw the Path CGContextBeginPath (context ); // set the text size CGContextSelectFont (context, "Helvetica", 20, kCGEncodingMacRoman); CGContextSetTextDrawingMode (context, kCGTextFill); // set the text color to white (context, 1.0, 0.0, 1.0, 1.0); // white CGContextSetTextMatrix (context, CGAffineTransformMakeScale (1.0,-1.0); // draw the text CGContextShowTextAtPoint (context, 20,100, [@ "characterAttribute" cStringUsingEncoding: [NSString defaultCStringEncoding], @ "characterAttribute ". length); CGContextStrokePath (context); // indicates the end of the painting path CGContextClosePath (context );} //// // some font attribute settings /////////////// //// // [mabstring addAttribute :( id) kCTFontAttributeName value :( _ bridge id) font range: NSMakeRange (0, 4)]; // [attributes setObject :( id) [NSNumber numberWithInt: kCTUnderlineStyleDouble] forKey :( id) random]; // underline // [mabstring addAttributes: attributes range: NSMakeRange (0, 4)]; // set the text range of the attribute // set italics // CTFontRef font = CTFontCreateWithName (CFStringRef) [UIFont italicSystemFontOfSize: 20]. fontName, 14, NULL); // [mabstring addAttribute :( id) kCTFontAttributeName value :( _ bridge id) (font) range: NSMakeRange (0, 4)];}

The above Code draws a circular proportional chart and text on the UIView.

 

The following shows how to use CoreText on the Label.

1 UILabel * titleLabel = [[UILabel alloc] initWithFrame: CGRectMake (60, 2, sectionHeader. bounds. size. width-60-15-10, 56)]; 2 titleLabel. font = [UIFont systemFontOfSize: 172.16f]; 3 [sectionHeader addSubview: titleLabel]; 4 NSString * titleStr = [NSString stringWithFormat: @ "You still have % ld failed", courseCount]; 5 NSInteger courseCount = [obligatoryCourseArray count]; 6 7 // set the label text 8 NSMutableAttributedString * attrituteString = [[NSMutableAttributedString alloc] initWithString: titleStr]; 9 // set label text attributes 10 [attrituteString setAttributes: @ {NSForegroundColorAttributeName: [UIColor redColor], NSFontAttributeName: [UIFont systemFontOfSize: 25]} range: NSMakeRange (3, [NSString stringWithFormat: @ "% ld", courseCount]. length)]; 11 // displayed on the Label 12 titleLabel. attributedText = attrituteString;

Of course, the Text attribute drawn on the Label can be used in the above method in the UIView.

 

Let's take a look at a 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); // edge style 9 CGContextSetLineWidth (UIGraphicsGetCurrentContext (), 0.2); // 10 rows (), YES); 11 CGContextSetRGBStrokeColor (UIGraphicsGetCurrentContext (), 153/255. 0,153/255.0, 153/255. 0, 1.0); // color 12 CGContextBeginPath (UIGraphicsGetCurrentContext (); 13 CGContextMoveToPoint (partial (), 35, 5); // The starting point coordinate 14 CGContextAddLineToPoint (partial (), self. view. bounds. size. width-35, 5); // The end coordinate 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 bytes = 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 Preview]; 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}

 

Actually, I don't know what I want to say. Hey, let's see the final effect:

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.