创建表格视图
2016-06-24 14:27
267 查看
创建表格方法,demo git地址
介绍: 本代码提供两种方式,让开发者更方便地创建一个表格视图,第一种方式是通过代理的方法实现整个视图的搭建,仿造UITableView模式进行编写,其功能强大;第二种方式是对第一种方式的一种封装,让开发者能更快速地实现视图的搭建,但对于过于个性的表格,建议采用第一种方式。
KWFormViewQuickBuilder.h
KWFromView
其中UIImage(KWFromView)是对UIImage的拓展,用于个性化生成图片
@interface UIImage ()
- (CGImageRef)newBorderMask:(NSUInteger)borderSize size:(CGSize)size;
@end
@implementation UIImage (Alpha)
- (BOOL)hasAlpha
{
CGImageAlphaInfo alpha = CGImageGetAlphaInfo(self.CGImage);
return (alpha == kCGImageAlphaFirst ||
alpha == kCGImageAlphaLast ||
alpha == kCGImageAlphaPremultipliedFirst ||
alpha == kCGImageAlphaPremultipliedLast);
}
- (UIImage *)imageWithAlpha
{
if ([self hasAlpha]) {
return self;
}
CGImageRef imageRef = self.CGImage;
size_t width = CGImageGetWidth(imageRef);
size_t height = CGImageGetHeight(imageRef);
// The bitsPerComponent and bitmapInfo values are hard-coded to prevent an "unsupported parameter combination" error
CGContextRef offscreenContext = CGBitmapContextCreate(NULL,
width,
height,
8,
0,
CGImageGetColorSpace(imageRef),
kCGBitmapByteOrderDefault | kCGImageAlphaPremultipliedFirst);
// Draw the image into the context and retrieve the new image, which will now have an alpha layer
CGContextDrawImage(offscreenContext, CGRectMake(0, 0, width, height), imageRef);
CGImageRef imageRefWithAlpha = CGBitmapContextCreateImage(offscreenContext);
UIImage *imageWithAlpha = [UIImage imageWithCGImage:imageRefWithAlpha];
// Clean up
CGContextRelease(offscreenContext);
CGImageRelease(imageRefWithAlpha);
return imageWithAlpha;
}
- (UIImage *)transparentBorderImage:(NSUInteger)borderSize
{
// If the image does not have an alpha layer, add one
UIImage *image = [self imageWithAlpha];
CGRect newRect = CGRectMake(0, 0, image.size.width + borderSize * 2, image.size.height + borderSize * 2);
// Build a context that's the same dimensions as the new size
CGContextRef bitmap = CGBitmapContextCreate(NULL,
newRect.size.width,
newRect.size.height,
CGImageGetBitsPerComponent(self.CGImage),
0,
CGImageGetColorSpace(self.CGImage),
CGImageGetBitmapInfo(self.CGImage));
// Draw the image in the center of the context, leaving a gap around the edges
CGRect imageLocation = CGRectMake(borderSize, borderSize, image.size.width, image.size.height);
CGContextDrawImage(bitmap, imageLocation, self.CGImage);
CGImageRef borderImageRef = CGBitmapContextCreateImage(bitmap);
// Create a mask to make the border transparent, and combine it with the image
CGImageRef maskImageRef = [self newBorderMask:borderSize size:newRect.size];
CGImageRef transparentBorderImageRef = CGImageCreateWithMask(borderImageRef, maskImageRef);
UIImage *transparentBorderImage = [UIImage imageWithCGImage:transparentBorderImageRef];
// Clean up
CGContextRelease(bitmap);
CGImageRelease(borderImageRef);
CGImageRelease(maskImageRef);
CGImageRelease(transparentBorderImageRef);
return transparentBorderImage;
}
#pragma mark - Private helper methods
- (CGImageRef)newBorderMask:(NSUInteger)borderSize size:(CGSize)size
{
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
// Build a context that's the same dimensions as the new size
CGContextRef maskContext = CGBitmapContextCreate(NULL,
size.width,
size.height,
8, // 8-bit grayscale
0,
colorSpace,
kCGBitmapByteOrderDefault | kCGImageAlphaNone);
// Start with a mask that's entirely transparent
CGContextSetFillColorWithColor(maskContext, [UIColor blackColor].CGColor);
CGContextFillRect(maskContext, CGRectMake(0, 0, size.width, size.height));
// Make the inner part (within the border) opaque
CGContextSetFillColorWithColor(maskContext, [UIColor whiteColor].CGColor);
CGContextFillRect(maskContext, CGRectMake(borderSize, borderSize, size.width - borderSize * 2, size.height - borderSize * 2));
// Get an image of the context
CGImageRef maskImageRef = CGBitmapContextCreateImage(maskContext);
// Clean up
CGContextRelease(maskContext);
CGColorSpaceRelease(colorSpace);
return maskImageRef;
}
- (UIImage *)imageByApplyingAlpha:(CGFloat) alpha
{
UIGraphicsBeginImageContextWithOptions(self.size, NO, 0.0f);
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGRect area = CGRectMake(0, 0, self.size.width, self.size.height);
CGContextScaleCTM(ctx, 1, -1);
CGContextTranslateCTM(ctx, 0, -area.size.height);
CGContextSetBlendMode(ctx, kCGBlendModeMultiply);
CGContextSetAlpha(ctx, alpha);
CGContextDrawImage(ctx, area, self.CGImage);
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
+ (UIImage *)imageFromColor:(UIColor *)color
{
CGSize size = CGSizeMake(20, 20);
UIGraphicsBeginImageContext(size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, (CGRect){CGPointZero,size});
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
@end
介绍: 本代码提供两种方式,让开发者更方便地创建一个表格视图,第一种方式是通过代理的方法实现整个视图的搭建,仿造UITableView模式进行编写,其功能强大;第二种方式是对第一种方式的一种封装,让开发者能更快速地实现视图的搭建,但对于过于个性的表格,建议采用第一种方式。
KWFormViewQuickBuilder.h
#import <Foundation/Foundation.h> #import "KWFormView.h" extern NSString *const noAction; extern NSString *const noColor; @interface KWFormViewQuickBuilder : NSObject - (KWFormView *)startCreatWithWidths:(NSArray *)widths startPoint:(CGPoint)point; - (void)setTextColor:(UIColor *)tcolor backgroundColor:(UIColor *)bcolor forRow:(NSInteger)row; - (void)setBorderColor:(UIColor *)color; - (void)setActionTarget:(id)target; - (void)addRecord:(NSArray *)record SELNames:(NSArray *)SELs; - (void)addRecord:(NSArray *)record; - (NSString *)valueAtcolumn:(NSInteger)column InRow:(NSInteger)row; @end
KWFromView
#import <UIKit/UIKit.h> @class KWFormView; typedef NS_ENUM(NSInteger, KWFormViewMode) { KWFormViewModeDefault, KWFormViewModeLeft, KWFormViewModeRight, KWFormViewModeMiddle, }; @protocol KWFormViewDataSource <NSObject> - (NSInteger)numberOfRowsInFormView:(KWFormView *)formView; - (NSInteger)formViewColumnsInRow:(KWFormView *)formView; - (CGFloat)formView:(KWFormView *)formView widthForColumn:(NSInteger)column; - (NSString *)formView:(KWFormView *)formView textForColumn:(NSInteger)column inRow:(NSInteger)row; @optional - (CGFloat)formView:(KWFormView *)formView heightForRow:(NSInteger)row; - (UIColor *)formView:(KWFormView *)formView colorOfColumn:(NSInteger)column inRow:(NSInteger)row; - (UIColor *)formView:(KWFormView *)formView contentColorOfColumn:(NSInteger)column inRow:(NSInteger)row; - (UIColor *)formViewBorderColor:(KWFormView *)formView; - (UIView *)formViewViewForFooter:(KWFormView *)formView; - (UIFont *)formViewFontOfContent:(KWFormView *)formView; - (BOOL)formView:(KWFormView *)formView addActionForColumn:(NSInteger)column inRow:(NSInteger)row; @end @protocol KWFormViewDelegate <NSObject> @optional - (void)formView:(KWFormView *)formView didSelectColumn:(NSInteger)column inRow:(NSInteger)row; @end @interface KWFormView : UIView @property (nonatomic, weak) id<KWFormViewDataSource> dataSource; @property (nonatomic, weak) id<KWFormViewDelegate> delegate; @property (nonatomic, assign) BOOL thickBorder; @property (nonatomic, assign ,readonly) KWFormViewMode mode; - (void)setMode:(KWFormViewMode)mode withMargin:(CGFloat)margin; - (UIButton *)itemAtColumn:(NSInteger)column inRow:(NSInteger)row; @end @interface UIImage (KWFormView) - (BOOL)hasAlpha; - (UIImage *)imageWithAlpha; - (UIImage *)transparentBorderImage:(NSUInteger)borderSize; - (UIImage *)imageByApplyingAlpha:(CGFloat)alpha; + (UIImage *)imageFromColor:(UIColor *)color; @end
其中UIImage(KWFromView)是对UIImage的拓展,用于个性化生成图片
@interface UIImage ()
- (CGImageRef)newBorderMask:(NSUInteger)borderSize size:(CGSize)size;
@end
@implementation UIImage (Alpha)
- (BOOL)hasAlpha
{
CGImageAlphaInfo alpha = CGImageGetAlphaInfo(self.CGImage);
return (alpha == kCGImageAlphaFirst ||
alpha == kCGImageAlphaLast ||
alpha == kCGImageAlphaPremultipliedFirst ||
alpha == kCGImageAlphaPremultipliedLast);
}
- (UIImage *)imageWithAlpha
{
if ([self hasAlpha]) {
return self;
}
CGImageRef imageRef = self.CGImage;
size_t width = CGImageGetWidth(imageRef);
size_t height = CGImageGetHeight(imageRef);
// The bitsPerComponent and bitmapInfo values are hard-coded to prevent an "unsupported parameter combination" error
CGContextRef offscreenContext = CGBitmapContextCreate(NULL,
width,
height,
8,
0,
CGImageGetColorSpace(imageRef),
kCGBitmapByteOrderDefault | kCGImageAlphaPremultipliedFirst);
// Draw the image into the context and retrieve the new image, which will now have an alpha layer
CGContextDrawImage(offscreenContext, CGRectMake(0, 0, width, height), imageRef);
CGImageRef imageRefWithAlpha = CGBitmapContextCreateImage(offscreenContext);
UIImage *imageWithAlpha = [UIImage imageWithCGImage:imageRefWithAlpha];
// Clean up
CGContextRelease(offscreenContext);
CGImageRelease(imageRefWithAlpha);
return imageWithAlpha;
}
- (UIImage *)transparentBorderImage:(NSUInteger)borderSize
{
// If the image does not have an alpha layer, add one
UIImage *image = [self imageWithAlpha];
CGRect newRect = CGRectMake(0, 0, image.size.width + borderSize * 2, image.size.height + borderSize * 2);
// Build a context that's the same dimensions as the new size
CGContextRef bitmap = CGBitmapContextCreate(NULL,
newRect.size.width,
newRect.size.height,
CGImageGetBitsPerComponent(self.CGImage),
0,
CGImageGetColorSpace(self.CGImage),
CGImageGetBitmapInfo(self.CGImage));
// Draw the image in the center of the context, leaving a gap around the edges
CGRect imageLocation = CGRectMake(borderSize, borderSize, image.size.width, image.size.height);
CGContextDrawImage(bitmap, imageLocation, self.CGImage);
CGImageRef borderImageRef = CGBitmapContextCreateImage(bitmap);
// Create a mask to make the border transparent, and combine it with the image
CGImageRef maskImageRef = [self newBorderMask:borderSize size:newRect.size];
CGImageRef transparentBorderImageRef = CGImageCreateWithMask(borderImageRef, maskImageRef);
UIImage *transparentBorderImage = [UIImage imageWithCGImage:transparentBorderImageRef];
// Clean up
CGContextRelease(bitmap);
CGImageRelease(borderImageRef);
CGImageRelease(maskImageRef);
CGImageRelease(transparentBorderImageRef);
return transparentBorderImage;
}
#pragma mark - Private helper methods
- (CGImageRef)newBorderMask:(NSUInteger)borderSize size:(CGSize)size
{
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
// Build a context that's the same dimensions as the new size
CGContextRef maskContext = CGBitmapContextCreate(NULL,
size.width,
size.height,
8, // 8-bit grayscale
0,
colorSpace,
kCGBitmapByteOrderDefault | kCGImageAlphaNone);
// Start with a mask that's entirely transparent
CGContextSetFillColorWithColor(maskContext, [UIColor blackColor].CGColor);
CGContextFillRect(maskContext, CGRectMake(0, 0, size.width, size.height));
// Make the inner part (within the border) opaque
CGContextSetFillColorWithColor(maskContext, [UIColor whiteColor].CGColor);
CGContextFillRect(maskContext, CGRectMake(borderSize, borderSize, size.width - borderSize * 2, size.height - borderSize * 2));
// Get an image of the context
CGImageRef maskImageRef = CGBitmapContextCreateImage(maskContext);
// Clean up
CGContextRelease(maskContext);
CGColorSpaceRelease(colorSpace);
return maskImageRef;
}
- (UIImage *)imageByApplyingAlpha:(CGFloat) alpha
{
UIGraphicsBeginImageContextWithOptions(self.size, NO, 0.0f);
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGRect area = CGRectMake(0, 0, self.size.width, self.size.height);
CGContextScaleCTM(ctx, 1, -1);
CGContextTranslateCTM(ctx, 0, -area.size.height);
CGContextSetBlendMode(ctx, kCGBlendModeMultiply);
CGContextSetAlpha(ctx, alpha);
CGContextDrawImage(ctx, area, self.CGImage);
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
+ (UIImage *)imageFromColor:(UIColor *)color
{
CGSize size = CGSizeMake(20, 20);
UIGraphicsBeginImageContext(size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, (CGRect){CGPointZero,size});
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
@end
相关文章推荐
- iOS 获取 顶层控制器
- Android:一个TextView分段改变样式、添加监听事件的实现方式(类似朋友圈评论)
- Android app架构经验总结
- 10个对Web开发者最有用的Python包
- Android app1打开app2的实现
- Android5.0报错:java.lang.IllegalArgumentException: Service Intent must be explicit
- Openfire 的安装和配置
- Android系统架构
- 该年的第几天
- 从我自己的角度总结一下移动互联网的技术
- android:layout_gravity和android:gravity的区别
- 浅析C语言中堆和栈的区别
- 【POJ1699】-Best Sequence 搜索剪枝
- Android 实现“调用手机已安装市场,进行软件评分的功能”
- Android App实现第三方微信登录
- android:windowSoftInputMode属性使用
- [Android]如何修改android模拟器的IMEI号
- SQL2000数据库密码被替换,重置密码提示未能找到存储过程sp_password解决方案
- [Android]MUC获取服务器HostedRoom产生异常
- Android XMPP实现IM 群聊功能