Custom view of non-XIB files in IOS development (98)

Source: Internet
Author: User

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:

 



 

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.