IOS简单的画线功能实现
2017-05-03 16:54
211 查看
#import "MyTestView.h"
#define maxNum 10
#define FormWidth [[UIScreen mainScreen] bounds].size.width/(maxNum+1)
#define FormHeight FormWidth
@interface MyTestView()
@property (nonatomic,copy)
NSArray *xDatas;
@property (nonatomic,copy)
NSArray *yDatas;
@property (nonatomic,strong)
NSMutableArray *fillDatas; //
填充的数据
@property (nonatomic,strong)
NSMutableArray *frameArr;
@end
@implementation
MyTestView
- (instancetype)initWithFrame:(CGRect)frame{
self = [superinitWithFrame:frame];
if (self) {
self.backgroundColor = [UIColorwhiteColor];
_yDatas =@[@"01期",@"02期",@"03期",@"04期",@"05期",@"06期",@"07期",@"08期",@"09期",@"10期"];
_xDatas =@[@"0",@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9"];
_fillDatas = [NSMutableArrayarray]
;
_frameArr = [NSMutableArraynew];
for (NSInteger i=0; i<_xDatas.count;
i++) {
NSString *number = [selfrandomFormArray:_xDatas];
[_fillDatasaddObject:number];
}
}
returnself;
}
- (NSString *)randomFormArray:(NSArray *)array{
NSInteger count = array.count;
NSInteger index =arc4random()%count;
return [NSStringstringWithFormat:@"%ld",index];
}
- (void)drawRect:(CGRect)rect{
CGContextRef context =UIGraphicsGetCurrentContext();
//FormHeight 为格子的高度 7是离上边界的距离不设置就会出现显示边界的线不好控制
for (int i=0; i<=_yDatas.count;
i++) {
if (i<_yDatas.count) {
NSString *period =_yDatas[i];
//调用计算字符串的宽高的方法
CGSize size = [selfcalculaTetextSize:periodandSize:CGSizeMake(FormWidth
,FormHeight)];
[period drawInRect:CGRectMake((FormWidth-size.width)/2.0,(FormHeight-size.height)/2.0+FormHeight*i+7,FormWidth,
FormHeight)
withAttributes:@{NSFontAttributeName:[UIFontsystemFontOfSize:12],NSForegroundColorAttributeName:[UIColorblackColor]}];
}
// 设置画笔颜色
CGContextSetStrokeColorWithColor(context, [UIColorgrayColor].CGColor);
// CGContextSetLineWidth(context, .4);
CGContextSetLineWidth(context,0.5);
// 画笔的起始坐标
CGContextMoveToPoint(context,0, i*FormHeight +7);
CGContextMoveToPoint(context,0, i*FormHeight +7);
CGContextAddLineToPoint(context, [[UIScreenmainScreen]
bounds].size.width, i*FormHeight+7);
CGContextDrawPath(context,kCGPathStroke);
// 画线
}
for (int j=0; j <=_yDatas.count;
j++) {
if (j<maxNum) {
for (int i=0; i<maxNum;
i++) {
NSString *period =_xDatas[j];
CGSize size = [selfcalculaTetextSize:periodandSize:CGSizeMake(FormWidth,FormHeight)];
[period drawInRect:CGRectMake((FormWidth-size.width)/2.0+j*FormWidth+FormWidth,(FormHeight-size.height)/2.0+i*FormHeight+7,FormWidth,
FormHeight)
withAttributes:@{NSFontAttributeName:[UIFontsystemFontOfSize:12],NSForegroundColorAttributeName:[UIColorblackColor]}];
}
}
CGContextSetStrokeColorWithColor(context, [UIColorgrayColor].CGColor);
CGContextSetLineWidth(context,.4);
CGContextMoveToPoint(context, j*FormWidth,7);
CGContextAddLineToPoint(context, j*FormWidth,maxNum*FormHeight+7);
CGContextDrawPath(context,kCGPathStroke);
}
// 画填充圆
for (int i =0; i<_fillDatas.count;
i++) {
CGContextSetStrokeColorWithColor(context, [UIColorgrayColor].CGColor);
CGContextSetLineWidth(context,.4);
NSString *number =_fillDatas[i];
for (int x =0; x <
_xDatas.count; x++) {
if ([numberintValue] ==x) {
//画圆圈
CGContextAddArc(context,FormWidth*x+FormWidth/2+FormWidth,FormHeight*i+FormHeight/2+7,FormHeight/2,0,
M_PI*2,1);
CGPoint point =CGPointMake(FormWidth*x+FormWidth/2+FormWidth,FormHeight*i+FormHeight/2+7);
NSString *str =NSStringFromCGPoint(point);
//保存圆中心的位置给下面的连线
[_frameArraddObject:str];
CGContextSetStrokeColorWithColor(context, [UIColorgreenColor].CGColor);
CGContextDrawPath(context,kCGPathStroke);
//填满整个圆
CGContextAddArc(context,FormWidth*x+FormWidth/2+FormWidth,FormHeight*i+FormHeight/2+7,FormHeight/2,0,
M_PI*2,1);
CGContextSetFillColorWithColor(context, [UIColorgreenColor].CGColor);
CGContextDrawPath(context,kCGPathFill);
NSString *numberStr = [NSStringstringWithFormat:@"%@",number];
CGSize size = [selfcalculaTetextSize:numberStrandSize:CGSizeMake(FormWidth,FormHeight)];
//画内容
[numberStr drawInRect:CGRectMake((FormWidth-size.width)/2.0+x*FormWidth+FormWidth,(FormHeight-size.height)/2.0+i*FormHeight+7,FormWidth,
FormHeight)
withAttributes:@{NSFontAttributeName:[UIFontsystemFontOfSize:12],NSForegroundColorAttributeName:[UIColorredColor]}];
}
}
}
for (int i=0; i<_frameArr.count;
i++) {
NSString *str = [_frameArrobjectAtIndex:i];
CGPoint point =CGPointFromString(str);
// 设置画笔颜色
CGContextSetStrokeColorWithColor(context, [UIColorgreenColor].CGColor);
CGContextSetLineWidth(context,.4);
if (i==0) {
// 画笔的起始坐标
CGContextMoveToPoint(context, point.x, point.y);
}else{
NSString *str1 = [_frameArrobjectAtIndex:i-1];
CGPoint point1 =CGPointFromString(str1);
CGContextMoveToPoint(context, point1.x, point1.y);
CGContextAddLineToPoint(context, point.x, point.y);
}
CGContextDrawPath(context,kCGPathStroke);
}
}
- (CGSize )calculaTetextSize:(NSString *)text andSize:(CGSize)size{
NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStylenew];
paragraphStyle.alignment =NSTextAlignmentLeft;
paragraphStyle.lineBreakMode =NSLineBreakByWordWrapping;
NSDictionary * attributes =@{NSFontAttributeName :[UIFontsystemFontOfSize:12],
NSParagraphStyleAttributeName : paragraphStyle};
CGSize contentSize = [textboundingRectWithSize:CGSizeMake(size.width,MAXFLOAT)
options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading)
attributes:attributes
context:nil].size;
return contentSize;
}
@end
#define maxNum 10
#define FormWidth [[UIScreen mainScreen] bounds].size.width/(maxNum+1)
#define FormHeight FormWidth
@interface MyTestView()
@property (nonatomic,copy)
NSArray *xDatas;
@property (nonatomic,copy)
NSArray *yDatas;
@property (nonatomic,strong)
NSMutableArray *fillDatas; //
填充的数据
@property (nonatomic,strong)
NSMutableArray *frameArr;
@end
@implementation
MyTestView
- (instancetype)initWithFrame:(CGRect)frame{
self = [superinitWithFrame:frame];
if (self) {
self.backgroundColor = [UIColorwhiteColor];
_yDatas =@[@"01期",@"02期",@"03期",@"04期",@"05期",@"06期",@"07期",@"08期",@"09期",@"10期"];
_xDatas =@[@"0",@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9"];
_fillDatas = [NSMutableArrayarray]
;
_frameArr = [NSMutableArraynew];
for (NSInteger i=0; i<_xDatas.count;
i++) {
NSString *number = [selfrandomFormArray:_xDatas];
[_fillDatasaddObject:number];
}
}
returnself;
}
- (NSString *)randomFormArray:(NSArray *)array{
NSInteger count = array.count;
NSInteger index =arc4random()%count;
return [NSStringstringWithFormat:@"%ld",index];
}
- (void)drawRect:(CGRect)rect{
CGContextRef context =UIGraphicsGetCurrentContext();
//FormHeight 为格子的高度 7是离上边界的距离不设置就会出现显示边界的线不好控制
for (int i=0; i<=_yDatas.count;
i++) {
if (i<_yDatas.count) {
NSString *period =_yDatas[i];
//调用计算字符串的宽高的方法
CGSize size = [selfcalculaTetextSize:periodandSize:CGSizeMake(FormWidth
,FormHeight)];
[period drawInRect:CGRectMake((FormWidth-size.width)/2.0,(FormHeight-size.height)/2.0+FormHeight*i+7,FormWidth,
FormHeight)
withAttributes:@{NSFontAttributeName:[UIFontsystemFontOfSize:12],NSForegroundColorAttributeName:[UIColorblackColor]}];
}
// 设置画笔颜色
CGContextSetStrokeColorWithColor(context, [UIColorgrayColor].CGColor);
// CGContextSetLineWidth(context, .4);
CGContextSetLineWidth(context,0.5);
// 画笔的起始坐标
CGContextMoveToPoint(context,0, i*FormHeight +7);
CGContextMoveToPoint(context,0, i*FormHeight +7);
CGContextAddLineToPoint(context, [[UIScreenmainScreen]
bounds].size.width, i*FormHeight+7);
CGContextDrawPath(context,kCGPathStroke);
// 画线
}
for (int j=0; j <=_yDatas.count;
j++) {
if (j<maxNum) {
for (int i=0; i<maxNum;
i++) {
NSString *period =_xDatas[j];
CGSize size = [selfcalculaTetextSize:periodandSize:CGSizeMake(FormWidth,FormHeight)];
[period drawInRect:CGRectMake((FormWidth-size.width)/2.0+j*FormWidth+FormWidth,(FormHeight-size.height)/2.0+i*FormHeight+7,FormWidth,
FormHeight)
withAttributes:@{NSFontAttributeName:[UIFontsystemFontOfSize:12],NSForegroundColorAttributeName:[UIColorblackColor]}];
}
}
CGContextSetStrokeColorWithColor(context, [UIColorgrayColor].CGColor);
CGContextSetLineWidth(context,.4);
CGContextMoveToPoint(context, j*FormWidth,7);
CGContextAddLineToPoint(context, j*FormWidth,maxNum*FormHeight+7);
CGContextDrawPath(context,kCGPathStroke);
}
// 画填充圆
for (int i =0; i<_fillDatas.count;
i++) {
CGContextSetStrokeColorWithColor(context, [UIColorgrayColor].CGColor);
CGContextSetLineWidth(context,.4);
NSString *number =_fillDatas[i];
for (int x =0; x <
_xDatas.count; x++) {
if ([numberintValue] ==x) {
//画圆圈
CGContextAddArc(context,FormWidth*x+FormWidth/2+FormWidth,FormHeight*i+FormHeight/2+7,FormHeight/2,0,
M_PI*2,1);
CGPoint point =CGPointMake(FormWidth*x+FormWidth/2+FormWidth,FormHeight*i+FormHeight/2+7);
NSString *str =NSStringFromCGPoint(point);
//保存圆中心的位置给下面的连线
[_frameArraddObject:str];
CGContextSetStrokeColorWithColor(context, [UIColorgreenColor].CGColor);
CGContextDrawPath(context,kCGPathStroke);
//填满整个圆
CGContextAddArc(context,FormWidth*x+FormWidth/2+FormWidth,FormHeight*i+FormHeight/2+7,FormHeight/2,0,
M_PI*2,1);
CGContextSetFillColorWithColor(context, [UIColorgreenColor].CGColor);
CGContextDrawPath(context,kCGPathFill);
NSString *numberStr = [NSStringstringWithFormat:@"%@",number];
CGSize size = [selfcalculaTetextSize:numberStrandSize:CGSizeMake(FormWidth,FormHeight)];
//画内容
[numberStr drawInRect:CGRectMake((FormWidth-size.width)/2.0+x*FormWidth+FormWidth,(FormHeight-size.height)/2.0+i*FormHeight+7,FormWidth,
FormHeight)
withAttributes:@{NSFontAttributeName:[UIFontsystemFontOfSize:12],NSForegroundColorAttributeName:[UIColorredColor]}];
}
}
}
for (int i=0; i<_frameArr.count;
i++) {
NSString *str = [_frameArrobjectAtIndex:i];
CGPoint point =CGPointFromString(str);
// 设置画笔颜色
CGContextSetStrokeColorWithColor(context, [UIColorgreenColor].CGColor);
CGContextSetLineWidth(context,.4);
if (i==0) {
// 画笔的起始坐标
CGContextMoveToPoint(context, point.x, point.y);
}else{
NSString *str1 = [_frameArrobjectAtIndex:i-1];
CGPoint point1 =CGPointFromString(str1);
CGContextMoveToPoint(context, point1.x, point1.y);
CGContextAddLineToPoint(context, point.x, point.y);
}
CGContextDrawPath(context,kCGPathStroke);
}
}
- (CGSize )calculaTetextSize:(NSString *)text andSize:(CGSize)size{
NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStylenew];
paragraphStyle.alignment =NSTextAlignmentLeft;
paragraphStyle.lineBreakMode =NSLineBreakByWordWrapping;
NSDictionary * attributes =@{NSFontAttributeName :[UIFontsystemFontOfSize:12],
NSParagraphStyleAttributeName : paragraphStyle};
CGSize contentSize = [textboundingRectWithSize:CGSizeMake(size.width,MAXFLOAT)
options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading)
attributes:attributes
context:nil].size;
return contentSize;
}
@end
相关文章推荐
- 【IOS功能实现】简单画线
- iOS-实现最简单的画线功能 . 转
- iOS-实现最简单的画线功能
- iOS-实现最简单的画线功能 . 转
- iOS tableview 实现简单的搜索功能
- iOS tableview 实现简单的搜索功能
- [iOS]画线功能实现
- IOS开发之使用Speex格式实现简单的语音聊天功能(二)
- iOS table简单实现增加移动和删除功能与自定义cell的实现
- iOS,侧滑(最简单效果却很好的侧滑功能实现
- ios-day20-01(使用UIWebView实现一个简单功能的浏览器)
- 分享一个iOS下实现基本绘画板功能的简单方法
- sqlite之增删改查--简单的实现iOS收藏夹功能
- IOS开发简单登录LoginViewController、注册RegisterViewController、UcenterViewController功能实现方法
- 关于iOS中简单实现调用系统相机及相册功能
- IOS开发简单登录LoginViewController、注册RegisterViewController、UcenterViewController功能实现方法
- IOS开发之使用Speex格式实现简单的语音聊天功能(二)
- iOS_通过UIBezierPath实现简单的画板功能
- [ios开发]利用有道翻译API实现简单的翻译功能
- IOS开发简单登录LoginViewController、注册RegisterViewController、UcenterViewController功能实现方法