NinePatch images are specialized PNG images that allow you to stretch the center but leave the edges intact much like the image-itself. These are great for scalable buttons, backgrounds, and other UI elements. This article would talk about how to use the NinePatchKit library to implement NinePatch images in iOS and macOS applications.
Using a NinePatch Image in iOS and macOS
1. Create the NinePatch Image
Now, you have to create a NinePatch image. I recommend using the 9 Patch Editor tool to generate your image. The process is as follows:
- Go to the site of the 9 Patch Editor.
- Upload the PNG image you want to convert to a NinePatch.
- Define boundaries (you can do that by dragging the edges) and preview the result.
- Download the resultant NinePatch image. In the download options ensure that you check the "Save as a compiled image" option as this will ensure your image contains all required metadata so that it can be correctly parsed by NinePatchKit.
2. Include the NinePatch Images into the Project
You can incorporate NinePatch images into your bundle
, Data Set
, or anywhere you wish but never add them to Image Set
. During building, all pngs housed in the Image Set
will be optimized through CgBI
chunk embedding and without the extra chunks hence the NinePatch images interred in the Image Set
will no longer function correctly in parsing format due to the modifications they go through.
3. Import NinePatchKit
Next, you will require the use of the NinePatchKit library to create and render NinePatch images. This open-source library provides a very simple API for NinePatch images, simplifying their use inside applications on iOS and macOS. You can link it into your project via CocoaPods or manually add the code within your project.
-
Using CocoaPods: Add this line to your
Podfile
:pod 'NinePatchKit'
Then run
pod install
. -
Manual Integration: Download the NinePatchKit library, and add the
NinePatchUtils
folder to your project.
4. Using a NinePatch Image in Code
Now, you can use NinePatchKit to load and render your NinePatch images. This is a simple example to show how you can use them in NinePatchImageView
:
#import <NinePatchKit/NinePatchKit.h>
...
NSString * path = [[NSBundle mainBundle] pathForResource:@"YourNinePatchImageName" ofType:@"png"];
NinePatchImageView * imageView = [[NinePatchImageView alloc] init];
imageView.showImage = [NinePatchUtils imageWithContentsOfFile:path];
[imageView.contentView addSubview:CustomView]; // add subview to contentView
Replace "YourNinePatchImageName"
with the name of your NinePatch image (without an extension). This code snippet creates a NinePatchImageView
, and using NinePatchUtils
, it loads your NinePatch image. The NinePatchImageView
is similar to UITableViewCell
-they can have additional subviews added onto their contentView
for a more complex layout.
5. Summary
So now you will be able to use it easily with iOS and macOS applications using the NinePatch graphic. By using the NinePatchKit library, you can easily render high-quality stretchable images to improve the experience of your interface.
Note
- Image Format: Ensure that your image is in the correct PNG format and includes all the necessary NinePatch metadata.
- Image Location: Include the NinePatch in your project's resource package and load it with the correct path. Don't place the NinePatch into an
Image Set
because it can corrupt the image format.