自定义一个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. iPhone、iPod和iPad离线固件升级的方法

    我们知道iOS升级的过程过程超级简单,特别是在线升级只需要点击几个按钮就ok了,但是对于开发者来说,经常升级的iOS固件都是preview版的,需要自己下载好固件之后,手动来更新,我找了一下网上的资料 ...

  2. [Redux] React Todo List Example (Filtering Todos)

    /** * A reducer for a single todo * @param state * @param action * @returns {*} */ const todo = ( st ...

  3. 支持多QQ登录的软件

    支持多QQ登录,批量加好友,批量回复QQ消息,当然也能接收 下载链接:多QQ登录软件

  4. CSS3 之 box-shadow

    1. css3 box-shadow CSS3的box-shadow属性可以让我们轻松实现图层阴影效果 box-shadow:  inset(可选 默认没有) x-offset    y-offset ...

  5. .Net的PDF转图片

    用的是破解版的 O2S.Components.PDFRender4NET.dll 插件, 简单引用即可 public static class PdfToImage { , , ) { try { / ...

  6. spark-shell启动集群

    使用spark-shell  启动spark集群时的流程简析: spark-shell->spark-submit->spark-class 在sprk-class中根据条件会从不同的入口 ...

  7. 2、 Spark Streaming方式从socket中获取数据进行简单单词统计

    Spark 1.5.2 Spark Streaming 学习笔记和编程练习 Overview 概述 Spark Streaming is an extension of the core Spark ...

  8. 3 linux、windows环境---路径分隔符不同导致的问题

    问题:通常在eclipse,IntelliJ IDEA等进行代码编写时,程序中用到路径通常采用/job/test.properties或D:/job/test.properties等是形式作为文件路径 ...

  9. linux下启动和关闭网卡命令及DHCP上网

    ifup.ifdown:如果在 /etc/sysconfig/network-scripts里面的ifcfg-ethx等文件存在的话,就可以通过ifdown或ifup来实现网卡的开和关,例如:ifup ...

  10. 高级I/O函数(2)-splice函数

    splice函数: 功能描述:用于在两个文件描述符之间移动数据,也是零拷贝操作.函数定义如下: #include <fcntl.h> ssize_t splice(int fd_in,lo ...