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 画图片(略)
随机推荐
- 贪心-hdu-1789-Doing Homework again
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1789 题目意思: 有n个作业,每个作业有一个截止日期,每个作业如果超过截止日期完成的时候有一个惩罚值 ...
- spring Scurity终于测试OK了,复杂的功能还待深入研究!发布出来一起探讨吧!
spring Scurity终于测试OK了,复杂的功能还待深入研究!发布出来一起探讨吧! 就是因 为研究它,我的个天啦!头都大了一圈!还待修改完整版!我的目标不是每个项目拿到它就能使用!到时再说啦.. ...
- HDOJ 4937 Lucky Number
当进制转换后所剩下的为数较少时(2位.3位),相应的base都比較大.能够用数学的方法计算出来. 预处理掉转换后位数为3位后,base就小于n的3次方了,能够暴力计算. . .. Lucky Numb ...
- C#控制台吹泡泡算法
代码如下: static void Main(string[] args) { Bubbling(100, 100, "O", 1000); Console.ReadLine(); ...
- IOS6和IOS7的屏幕适配问题
自从IOS7出来以后,以前写在IOS6上或者更低版本的程序,跑在IOS7的模拟器上就会出现一些问题.最大的问题就是,所有的UI空间都会统一向上移动20个点(如果空间的y值为0,就会被StatusBar ...
- (转)IIS5.1的安装配置并发布ASP.NET网站
最近跟老师做一个桥梁养护系统的项目,要求用VS2008+Sql Server2000,服务器用IIS.由于之前做过的ASP.NET项目都是用的VS内置的服务器,并没有使用过IIS,第一次搭,花了几个小 ...
- (转)添加服务引用和添加Web引用对比
在WindowsForm程序中添加服务引用和Web引用对比 为了验证书上有关Visual Studio 2010添加服务引用和Web引用的区别,进行实验. 一.建立一个Web服务程序项目新建项目,选择 ...
- sqlserver2005重新安装(安装汇编错误,安装程序无法连接到数据库服务进行服务配置)
2014-01-09 16:41 1687人阅读 评论(1) 收藏 举报 分类: 数据库(1) 版权声明:本文为博主原创文章,未经博主允许不得转载. sqlserver2005重新安装(安装汇编错误, ...
- 脚本化HTTP
1.HTTP: 定义:超文本传输协议 (HTTP-Hypertext transfer protocol) 是一种详细规定了浏览器和万维网服务器之间互相通信的规则,通过因特网传送万维网文档的数据传送协 ...
- C++中的cout输出机制
代码: #include <iostream> using namespace std; int hello(){ cout<<"hello"<< ...