IOS 用drawRect 画表格
自定义一个View DrawLine DrawLine.h #import <UIKit/UIKit.h> @protocol gridTouchDelete <NSObject> - (void)gridTouchColumn:(NSInteger)column touchRow:(NSInteger)row; @end @interface DrawLine : UIView @property(nonatomic, assign) id<gridTouchDelete>delegate; @end #import "DrawLine.h" #define GRID_WIDTH 44
#define GRID_HEIGHT 44 @implementation DrawLine - (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
} - (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext(); // 格子线条颜色
CGContextSetStrokeColorWithColor(context, [UIColor grayColor].CGColor);
CGContextBeginPath(context); NSInteger numRows = ;
float gridHeight = numRows*(GRID_HEIGHT+)+; // 背景色
CGRect rectangleGrid = CGRectMake(,,self.frame.size.width,gridHeight);
CGContextAddRect(context, rectangleGrid);
CGContextSetFillColorWithColor(context, [[UIColor grayColor] colorWithAlphaComponent:0.2].CGColor);
CGContextFillPath(context); for (int i = ; i < ; i++) {
//columns 列
CGContextMoveToPoint(context, i*(GRID_WIDTH+)+i*, );
CGContextAddLineToPoint(context, i*(GRID_WIDTH+)+i*, gridHeight); if (i > numRows) continue; //rows 行
CGContextMoveToPoint(context, , i*(GRID_HEIGHT+)+i*+);
CGContextAddLineToPoint(context, self.frame.size.width, +i*(GRID_HEIGHT+)+i*+); } CGContextStrokePath(context);
CGContextSetAllowsAntialiasing(context, YES); NSInteger gridNum = numRows * ;
for (int i = ; i < gridNum; i ++) {
int targetColumn = i%;
int targetRow = i/;
int targetX = targetColumn * (GRID_WIDTH+)+;
int targetY = targetRow * (GRID_HEIGHT+)+; NSString *gridStr = [NSString stringWithFormat:@"%d",i+]; if ([[[UIDevice currentDevice] systemVersion] floatValue] >=7.0) {
UIColor *fontColor;
if (targetColumn == || targetColumn == ) {
fontColor = [UIColor redColor];
}
else
{
fontColor = [UIColor blackColor];
} [gridStr drawInRect:CGRectMake(targetX, targetY, self.frame.size.width, GRID_WIDTH) withAttributes:@{NSFontAttributeName:[UIFont fontWithName:@"HelveticaNeue-Bold" size:],NSForegroundColorAttributeName:fontColor}];
}
else
{
[gridStr drawInRect:CGRectMake(targetX, targetY, self.frame.size.width, GRID_WIDTH) withFont:[UIFont fontWithName:@"HelveticaNeue-Bold" size:] lineBreakMode:NSLineBreakByClipping alignment:NSTextAlignmentCenter]; CGContextSetFillColorWithColor(context,[UIColor redColor].CGColor);
} }
} -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint touchPoint = [touch locationInView:self]; float xLocation = touchPoint.x;
float yLocation = touchPoint.y; int column = floorf(xLocation/(GRID_HEIGHT+));
int row = floorf(yLocation/(GRID_WIDTH+)); if ([_delegate respondsToSelector:@selector(gridTouchColumn:touchRow:)]) {
[_delegate gridTouchColumn:column touchRow:row];
}
} MainViewController - (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view. DrawLine *lineView = [[DrawLine alloc]initWithFrame:CGRectMake(, , self.view.frame.size.width, )];
lineView.backgroundColor = [UIColor clearColor];
lineView.delegate = self;
[self.view addSubview:lineView];
} - (void)gridTouchColumn:(NSInteger)column touchRow:(NSInteger)row
{
NSLog(@"行:%d,列:%d",column,row);
}

IOS 用drawRect 画表格的更多相关文章
- iOS 使用drawRect: 绘制虚线椭圆
iOS 使用drawRect: 绘制虚线椭圆 1:首先如果要使用 drawRect 绘图 要导入 CoreGraphics.framework 框架 然后 创建 自定义view, 即是 myView继 ...
- 画表格防OFFICE的功能
http://files.cnblogs.com/xe2011/officetable.rar 画表格防OFFICE的功能
- iOS开发之画图板(贝塞尔曲线)
贝塞尔曲线,听着挺牛气一词,不过下面我们在做画图板的时候就用到贝塞尔绘直线,没用到绘制曲线的功能.如果会点PS的小伙伴会对贝塞尔曲线有更直观的理解.这篇博文的重点不在于如何用使用贝塞尔曲线,而是利用贝 ...
- C算法编程题(三)画表格
前言 上一篇<C算法编程题(二)正螺旋> 写东西前还是喜欢吐槽点东西,要不然写的真还没意思,一直的想法是在博客园把自己上学和工作时候整理的东西写出来和大家分享,就像前面写的<T-Sq ...
- iOS小画板画线总结
一:基本画线: 使用贝赛尔曲线画: //创建路径 UIBezierPath* aPath = [UIBezierPath bezierPath]; //设置线宽 aPath.lineWidth = 5 ...
- IOS用CGContextRef画各种图形(文字、圆、直线、弧线、矩形、扇形、椭圆、三角形、圆角矩形、贝塞尔曲线、图片)
... 首先了解一下CGContextRef: An opaque type that represents a Quartz 2D drawing environment. Graphics Con ...
- (转) IOS用CGContextRef画各种图形(文字、圆、直线、弧线、矩形、扇形、椭圆、三角形、圆角矩形、贝塞尔曲线、图片)
首先了解一下CGContextRef: An opaque type that represents a Quartz 2D drawing environment. Graphics Context ...
- [置顶] IOS用CGContextRef画各种图形(文字、圆、直线、弧线、矩形、扇形、椭圆、三角形、圆角矩形、贝塞尔曲线、图片)
首先了解一下CGContextRef: An opaque type that represents a Quartz 2D drawing environment. Graphics Context ...
- IOS开发之画图形
1 画线 2 画线第二个方法 相对方法1简洁 3 矩形 4 圆 5 弧线 6画文字(略) 7 画图片(略)
随机推荐
- 文件夹65ad47d7-2e27-4a5c-b238-26643fdaeb98
这几天发现电脑中毒了,本地开的服务预览页面时,页面会被插入Html代码.我用360扫描之后发现有木马病毒(c:\programData有个65ad47d7-2e27-4a5c-b238-26643fd ...
- 用Scertify Professional实现代码审查
用Scertify Professional实现代码审查 作者:chszs,转载需注明.博客主页: http://blog.csdn.net/chszs Scertify Professional是一 ...
- Operation System - Peterson's Solution算法 解决多线程冲突
Person's solution 是用来一种基于软件的解决关键区域问题的算法(critical-section). 它并不是完美的,有可能不对地工作.并且是限制解决两个进程同步的问题. 可是它非常e ...
- js计算日期天数差-2013-9-26
function daymath(sdate, edate) { var startdate = sdate; var enddate = edate; ...
- with admin option 与with grant option
在赋予user 权限或者role 时,常常会用到with admin option 和with grant option,而在使用中,可能会很容易出现混淆的情况,现把他们的相同点和不同点总结如下:相同 ...
- [CSAPP笔记][第六章存储器层次结构]
第六章 存储器层次结构 在简单模型中,存储器系统是一个线性的字节数组,CPU能够在一个常数访问每个存储器位置. 虽然是一个行之有效的模型,但没有反应现代系统实际工作方式. 实际上,存储器系统(memo ...
- python-文件压缩和解压
import tarfile #压缩 tar = tarfile.open('your.tar','w') tar.add('ooo.xml',arcname='ooo.xml') tar.close ...
- 巧记--Css选择器
love ------> hate 即: a:link --> a:visited --> a:hover --> a:active a:link ...
- FTS下载地址
http://download.microsoft.com/download/5/2/e/52e22b90-2ba7-427b-9ea4-604d3b37a2e7/vs2012_tfs_chs.iso
- css导航条
#nav ul { display: none; position: absolute; padding-top: 0px;} #nav li:hover ul { display: block;}