1 Preface
In the previous section, we talked about the custom view built with the XIB file. Let's take a look at it today without using the custom view of the XIB file.
2. Details
Directory structure
This time, we will not create XIB files, but directly use the Objective-C file to replace XIB files.
ZYCustomView. m:
[Plain]
-(Void) drawRect :( CGRect) rect
{
CGRect bounds = [self bounds];
CGPoint center;
Center. x = bounds. origin. x + bounds. size. width/2.0;
Center. y = bounds. origin. y + bounds. size. height/2.0;
// Create a graphical path handle
CGMutablePathRef path = CGPathCreateMutable ();
// Set the boundary of the rectangle
CGRect rectangle = CGRectMake (center. X-100, center. y-150, 200366f, 300366f );
// Add a rectangle to the path
CGPathAddRect (path, NULL, rectangle );
// Obtain the context handle
CGContextRef currentContext = UIGraphicsGetCurrentContext ();
// Add the path to the context
CGContextAddPath (currentContext, path );
// Fill color
[[UIColor colorWithRed: 0.20f green: 0.60f blue: 0.80f alpha: 1.0f] setFill];
// Set the paint brush color
[[UIColor brownColor] setStroke];
// Set the border line width
CGContextSetLineWidth (currentContext, 5.0f );
// Draw
CGContextDrawPath (currentContext, kCGPathFillStroke );
/* Release path */
CGPathRelease (path );
// Create a string
NSString * text = @ "I'm pai_zhang ";
UIFont * font = [UIFont boldSystemFontOfSize: 20];
// Configure Rect
CGRect textRect;
TextRect. size = [text sizeWithFont: font];
TextRect. origin. x = center. x-textRect. size. width/2.0;
TextRect. origin. y = center. y-textRect. size. height/2.0;
// Set the font color
[[UIColor redColor] setFill];
// Set the shadow.
CGSize offset = CGSizeMake (4, 3 );
// The shadow color is black.
CGColorRef color = [[UIColor blackColor] CGColor];
// The Blur radius is 2.0
CGContextSetShadowWithColor (currentContext, offset, 2.0, color );
[Text drawInRect: textRect
WithFont: font];
}
-(Void) drawRect :( CGRect) rect
{
CGRect bounds = [self bounds];
CGPoint center;
Center. x = bounds. origin. x + bounds. size. width/2.0;
Center. y = bounds. origin. y + bounds. size. height/2.0;
// Create a graphical path handle
CGMutablePathRef path = CGPathCreateMutable ();
// Set the boundary of the rectangle
CGRect rectangle = CGRectMake (center. X-100, center. y-150, 200366f, 300366f );
// Add a rectangle to the path
CGPathAddRect (path, NULL, rectangle );
// Obtain the context handle
CGContextRef currentContext = UIGraphicsGetCurrentContext ();
// Add the path to the context
CGContextAddPath (currentContext, path );
// Fill color
[[UIColor colorWithRed: 0.20f green: 0.60f blue: 0.80f alpha: 1.0f] setFill];
// Set the paint brush color
[[UIColor brownColor] setStroke];
// Set the border line width
CGContextSetLineWidth (currentContext, 5.0f );
// Draw
CGContextDrawPath (currentContext, kCGPathFillStroke );
/* Release path */
CGPathRelease (path );
// Create a string
NSString * text = @ "I'm pai_zhang ";
UIFont * font = [UIFont boldSystemFontOfSize: 20];
// Configure Rect
CGRect textRect;
TextRect. size = [text sizeWithFont: font];
TextRect. origin. x = center. x-textRect. size. width/2.0;
TextRect. origin. y = center. y-textRect. size. height/2.0;
// Set the font color
[[UIColor redColor] setFill];
// Set the shadow.
CGSize offset = CGSizeMake (4, 3 );
// The shadow color is black.
CGColorRef color = [[UIColor blackColor] CGColor];
// The Blur radius is 2.0
CGContextSetShadowWithColor (currentContext, offset, 2.0, color );
[Text drawInRect: textRect
WithFont: font];
}
ZYViewController. m:
[Plain]
-(Void) viewDidLoad
{
[Super viewDidLoad];
// Create a CGRect of the form size
CGRect wholeWindow = [[self. view window] bounds];
// Create a form-sized HypnosisView instance
Self. view = [[ZYCustomView alloc] initWithFrame: wholeWindow];
[Self. view setBackgroundColor: [UIColor whiteColor];
}
-(Void) viewDidLoad
{
[Super viewDidLoad];
// Create a CGRect of the form size
CGRect wholeWindow = [[self. view window] bounds];
// Create a form-sized HypnosisView instance
Self. view = [[ZYCustomView alloc] initWithFrame: wholeWindow];
[Self. view setBackgroundColor: [UIColor whiteColor];
}
Running result: