iOS Development UI Chapter-quartz2d simple use (ii)
I. Drawing words
Code:
1//2//YYTEXTVIEW.M 3//04-Write Text 4//5//Created by hole Medical self on 14-6-10. 6//Copyright (c) 2014 itcast. All rights reserved. 7//8 9 #import "YYtextview.h" @implementation YYtextview12-(void) DrawRect: (CGRect) rect15 {16 17// Painting text NSString *str = @ "The amount of search broke up the pink hair of the two sides of the offff-roofed brick-and-air you f reply F into the membership dues wfh; fly; f reply; Wfh My skin good fhisfhsifh dividend Apifrhi Wei Feng net i"; 19 20//1. Get context//Cgcontextref CTX = Uigraphicsgetcurrentcontext (); 22//2. Drawing 23//not recommended for C-language method painting text, because the coordinate system in the quraz2d is inconsistent with the coordinate system in the Uikit, the text that is drawn is reversed, and it is quite troublesome to draw the text by means of the C language Cgcontextselectfont (< #CGContextRef c#> ;, < #const char *name#>, < #CGFloat Size#>, < #CGTextEncoding textencoding#>)//Cgcontextshowtex T (CTX, < #const char *string#>, < #size_t length#>) 26 27//Draw Rectangle 28//1. Get Context Cgcontextref CTX = Uigraphicsgetcurrentcontext (); 30//2. Drawing Cgcontextaddrect (CTX, CGRectMake (50, 50, 100, 100)); 32//3. Rendering 33 CgcontextstrokEpath (CTX), nsmutabledictionary *md = [nsmutabledictionary dictionary];37////Set Text color//MD [Nsforegroundcolorattributename] =[uicolor redcolor];39////Set Text background color +//md[nsbackgroundcolorattributename] = [UI Color greencolor];41////Set Text size//md[nsfontattributename] = [Uifont systemfontofsize:20];43 44// The word is drawn to the pointing position.//[str drawatpoint:cgpointmake (TEN) withattributes:md];46 47//Draw text into the specified range, if one line does not fit automatically Line breaks, when the text goes out of range, does not display the [str drawinrect:cgrectmake (+, +, +) withattributes:nil];49}50 @end
Effect:
Second, the picture
Code Listing 1:
1//2// YYIMAGE.M 3// 04-Write Text 4//5// Created by Hole Doctor on 14-6-10.6// Copyright (c) 2014 itcast. All rights reserved. 7//8 9 #import "YYimage.h" @implementation YYimage12-(void) DrawRect: (CGRect) rect15 {//
1. Loading pictures into memory UIImage *image = [UIImage imagenamed:@ "Me"];19 // Use the Drawaspatterninrec method to draw the image to layer, by tiling the original picture [image drawaspatterninrect:cgrectmake (0, 0, 320, 480)];23}24 25 26 @end
Effect (tiled):
Code Listing 2:
1 #import "YYimage.h" 2 3 @implementation yyimage 4 5 6-(void) DrawRect: (cgrect) rect 7 {8 9 // 1. Load image into memory UIImage *image = [UIImage imagenamed:@ "Me"];11 //Use OC method to draw pictures onto layer 15 //Use the Drawinrect method to draw the image to layer by stretching the original image [Image drawinrect:cgrectmake (0, 0, 19,];17) //Use Drawaspatterninrec method to draw the image to layer, is by tiling the original image// [Image drawaspatterninrect:cgrectmake (0, 0, 480)];20}21 @end
Effect (stretch picture):
Code Listing 3:
1//2// YYIMAGE.M 3// 04-Write Text 4//5// Created by Hole Doctor on 14-6-10.6// Copyright (c) 2014 itcast. All rights reserved. 7//8 9 #import "YYimage.h" @implementation YYimage12-(void) DrawRect: (CGRect) rect15 {//
1. Loading picture into memory UIImage *image = [UIImage imagenamed:@ "Me"];19 //Use OC method to draw the picture onto the layer //Draw the picture to the specified location [image Drawatpoint:cgpointmake (+)];25 }
Effect (draw the picture to a fixed position):
iOS Development UI Chapter-quartz2d simple use (ii)