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 画图片(略)
随机推荐
- HBase二级索引的设计(案例讲解)
摘要 最近做的一个项目涉及到了多条件的组合查询,数据存储用的是HBase,恰恰HBase对于这种场景的查询特别不给力,一般HBase的查询都是通过RowKey(要把多条件组合查询的字段都拼接在RowK ...
- ashx实现文件下载以及文件MD5码测试
cs using System; using System.Collections.Generic; using System.Linq; using System.Web; using System ...
- MVC与三层架构的关系
- cocos2dx 音效 粒子 数据存储
1.音效 引入头文件 #include "simpleAudioEngine.h" 命名空间 using namespace CocosDenshion; //创建一个背景 Siz ...
- C#如何获得本地电脑IP
using System; using System.Collections.Generic; using System.Text; using System.Net; //需要引用.Net命名空 ...
- 基于Pojo的开发模式(day03)
上一次的文章讨论到了Spring的设计目标:使得JEE开发更易用. ok,作为一个Java开发人员,应该都知道struts这个框架,不知道是否大家都清楚struts1和struts2的区别. 首先,这 ...
- mysql对GIS空间数据的支持,包括创建空间索引
CREATE TABLE tb_geo( id INT PRIMARY KEY AUTO_INCREMENT, NAME ) NOT NULL, pnt POINT NOT NULL, SPATIAL ...
- ORA-942 SP2-0611
环境:oracle 11.2.04 问题描述: 在使用hr用户启用set autot trace时报错 set">HR@test>set autot trace; Error O ...
- Bootstrap学习笔记(未整理)
强调class 这些class通过颜色来表示强调.也可以应用于链接,当鼠标盘旋于链接上时,其颜色会变深,就像默认的链接样式. <p class="text-muted"> ...
- MVC请求过程 简单分析(一)
在服务端判断客户端传过来的文件的类型,如果是静态文件,直接返回,在页面输出显示.如果是动态文件,通过aspnet_isapi.dll转交过.NetFrameWork框架执行. 创建ISAPIrunti ...