自定义一个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 画表格的更多相关文章

  1. iOS 使用drawRect: 绘制虚线椭圆

    iOS 使用drawRect: 绘制虚线椭圆 1:首先如果要使用 drawRect 绘图 要导入 CoreGraphics.framework 框架 然后 创建 自定义view, 即是 myView继 ...

  2. 画表格防OFFICE的功能

    http://files.cnblogs.com/xe2011/officetable.rar 画表格防OFFICE的功能

  3. iOS开发之画图板(贝塞尔曲线)

    贝塞尔曲线,听着挺牛气一词,不过下面我们在做画图板的时候就用到贝塞尔绘直线,没用到绘制曲线的功能.如果会点PS的小伙伴会对贝塞尔曲线有更直观的理解.这篇博文的重点不在于如何用使用贝塞尔曲线,而是利用贝 ...

  4. C算法编程题(三)画表格

    前言 上一篇<C算法编程题(二)正螺旋> 写东西前还是喜欢吐槽点东西,要不然写的真还没意思,一直的想法是在博客园把自己上学和工作时候整理的东西写出来和大家分享,就像前面写的<T-Sq ...

  5. iOS小画板画线总结

    一:基本画线: 使用贝赛尔曲线画: //创建路径 UIBezierPath* aPath = [UIBezierPath bezierPath]; //设置线宽 aPath.lineWidth = 5 ...

  6. IOS用CGContextRef画各种图形(文字、圆、直线、弧线、矩形、扇形、椭圆、三角形、圆角矩形、贝塞尔曲线、图片)

    ... 首先了解一下CGContextRef: An opaque type that represents a Quartz 2D drawing environment. Graphics Con ...

  7. (转) IOS用CGContextRef画各种图形(文字、圆、直线、弧线、矩形、扇形、椭圆、三角形、圆角矩形、贝塞尔曲线、图片)

    首先了解一下CGContextRef: An opaque type that represents a Quartz 2D drawing environment. Graphics Context ...

  8. [置顶] IOS用CGContextRef画各种图形(文字、圆、直线、弧线、矩形、扇形、椭圆、三角形、圆角矩形、贝塞尔曲线、图片)

    首先了解一下CGContextRef: An opaque type that represents a Quartz 2D drawing environment. Graphics Context ...

  9. IOS开发之画图形

    1 画线 2 画线第二个方法  相对方法1简洁 3 矩形 4 圆 5 弧线 6画文字(略) 7 画图片(略)

随机推荐

  1. java 自定义鼠标图标

    由于截图截不了,所以看不了图.源码如下: import java.awt.Cursor; import java.awt.Image; import java.awt.Point; import ja ...

  2. jQuery限制文本框只能输入正整数

    //限制键盘只能按数字键.小键盘数字键.退格键 $("#txtQty").keydown(function (e) { var code = parseInt(e.keyCode) ...

  3. Linux应用开发环境搭建

    因为笔者是一名大学生,对Linux内核开发方向非常感兴趣,可是实在是能(ji)力(shu)有(cha)限(jin),仅仅能从Linux应用开发開始,由浅入深,逐步进步,登上人生高峰,因此,昨天搭建了开 ...

  4. Android无法导入下载好的项目(和Eclipse中已经存在的项目命名一样导致冲突)解决办法

    错误提示: 在我们到导入从网络下载的项目时,经常会出现如下问题(选择的项目变灰,并且提示要选择至少一个项目): 错误原因: 出现这样的错误主要是因为你的Eclipse已经存在了和上图中New Proj ...

  5. Tomcat 原理篇

    TOMCAT 原理篇一.Tomcat 组成(Tomcat 由以下组件组成) 1.server a) Server是一个Catalina Servlet容器: b) Server 可以包含一个或多个se ...

  6. java基础之集合

    集合的定义,集合的应用,集合的分类,集合的遍历,集合的特性

  7. 在cygwin下编译c语言

    #include <stdio.h> int main (void) { printf("Hello World!\n"); ; } 1.保存到cygwin工作目录下 ...

  8. JavaScript split()

    http://www.w3school.com.cn/jsref/jsref_split.asp

  9. 华为oj 计算字符个数

    练手而已 #include <stdio.h> #include <string.h> int main(void) { char string[200]={'\0'}; in ...

  10. cell的各种使用和赋值 总结

    cell可以分为:自定义cell,系统的cell ,cell的自适应,.xib的cell //第一种cell:系统cell 在 UIViewController下创建UITableView //1.0 ...