iOS陆哥开发笔记(十六) (Quartz2D简单使用二)
2016-04-14 17:54
477 查看
一、画文字
#import "MMTextView.h"
@implementation MMTextView
- (void)drawRect:(CGRect)rect
{
// 画文字
NSString *str = @"的额搜风搜分手了粉色发俄双方说法offFF瓦房你F回复F入会费WFH;飞;FN返回WFH;哦发货;F回复;FHISFHSIFH我皮肤好APIFRHi分红AWFHIOF威锋网i";
// 1.获取上下文
// CGContextRef ctx = UIGraphicsGetCurrentContext();
// 2.绘图
// 不推荐使用C语言的方法绘制文字, 因为quraz2d中的坐标系和UIkit中的坐标系不一致, 绘制出来的文字是颠倒的, 而且通过C语言的方法绘制文字相当麻烦
// CGContextSelectFont(<#CGContextRef c#>, <#const char *name#>, <#CGFloat size#>, <#CGTextEncoding textEncoding#>)
// CGContextShowText(ctx, <#const char *string#>, <#size_t length#>)
// 绘制矩形
// 1.获取上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
// 2.绘图
CGContextAddRect(ctx, CGRectMake(50, 50, 100, 100));
// 3.渲染
CGContextStrokePath(ctx);
// NSMutableDictionary *md = [NSMutableDictionary dictionary];
// 设置文字颜色
// md[NSForegroundColorAttributeName] =[UIColor redColor];
// 设置文字背景颜色
// md[NSBackgroundColorAttributeName] = [UIColor greenColor];
// 设置文字大小
// md[NSFontAttributeName] = [UIFont systemFontOfSize:20];
// 将文字绘制到指点的位置
// [str drawAtPoint:CGPointMake(10, 10) withAttributes:md];
// 将文字绘制到指定的范围内, 如果一行装不下会自动换行, 当文字超出范围后就不显示
[str drawInRect:CGRectMake(50, 50, 100, 100)
withAttributes:nil];
}
@end
二、图片
(1、)
#import "MMImage.h"
@implementation MMImage
- (void)drawRect:(CGRect)rect
{
// 1.加载图片到内存中
UIImage *image = [UIImage imageNamed:@"me"];
// 利用drawAsPatternInRec方法绘制图片到layer, 是通过平铺原有图片
[image drawAsPatternInRect:CGRectMake(0, 0, 320, 480)];
}
@end
效果(平铺):
(2、)
#import "MMImage.h"
@implementation MMImage
- (void)drawRect:(CGRect)rect
{
// 1.加载图片到内存中
UIImage *image = [UIImage imageNamed:@"me"];
// 利用OC方法将图片绘制到layer上
// 利用drawInRect方法绘制图片到layer, 是通过拉伸原有图片
[image drawInRect:CGRectMake(0, 0, 200, 200)];
// 利用drawAsPatternInRec方法绘制图片到layer, 是通过平铺原有图片
// [image drawAsPatternInRect:CGRectMake(0, 0, 320, 480)];
}
@end
(3、)
#import "MMImage.h"
@implementation MMImage
- (void)drawRect:(CGRect)rect
{
// 1.加载图片到内存中
UIImage *image = [UIImage imageNamed:@"me"];
// 利用OC方法将图片绘制到layer上
// 将图片绘制到指定的位置
[image drawAtPoint:CGPointMake(100, 100)];
}
#import "MMTextView.h"
@implementation MMTextView
- (void)drawRect:(CGRect)rect
{
// 画文字
NSString *str = @"的额搜风搜分手了粉色发俄双方说法offFF瓦房你F回复F入会费WFH;飞;FN返回WFH;哦发货;F回复;FHISFHSIFH我皮肤好APIFRHi分红AWFHIOF威锋网i";
// 1.获取上下文
// CGContextRef ctx = UIGraphicsGetCurrentContext();
// 2.绘图
// 不推荐使用C语言的方法绘制文字, 因为quraz2d中的坐标系和UIkit中的坐标系不一致, 绘制出来的文字是颠倒的, 而且通过C语言的方法绘制文字相当麻烦
// CGContextSelectFont(<#CGContextRef c#>, <#const char *name#>, <#CGFloat size#>, <#CGTextEncoding textEncoding#>)
// CGContextShowText(ctx, <#const char *string#>, <#size_t length#>)
// 绘制矩形
// 1.获取上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
// 2.绘图
CGContextAddRect(ctx, CGRectMake(50, 50, 100, 100));
// 3.渲染
CGContextStrokePath(ctx);
// NSMutableDictionary *md = [NSMutableDictionary dictionary];
// 设置文字颜色
// md[NSForegroundColorAttributeName] =[UIColor redColor];
// 设置文字背景颜色
// md[NSBackgroundColorAttributeName] = [UIColor greenColor];
// 设置文字大小
// md[NSFontAttributeName] = [UIFont systemFontOfSize:20];
// 将文字绘制到指点的位置
// [str drawAtPoint:CGPointMake(10, 10) withAttributes:md];
// 将文字绘制到指定的范围内, 如果一行装不下会自动换行, 当文字超出范围后就不显示
[str drawInRect:CGRectMake(50, 50, 100, 100)
withAttributes:nil];
}
@end
二、图片
(1、)
#import "MMImage.h"
@implementation MMImage
- (void)drawRect:(CGRect)rect
{
// 1.加载图片到内存中
UIImage *image = [UIImage imageNamed:@"me"];
// 利用drawAsPatternInRec方法绘制图片到layer, 是通过平铺原有图片
[image drawAsPatternInRect:CGRectMake(0, 0, 320, 480)];
}
@end
效果(平铺):
(2、)
#import "MMImage.h"
@implementation MMImage
- (void)drawRect:(CGRect)rect
{
// 1.加载图片到内存中
UIImage *image = [UIImage imageNamed:@"me"];
// 利用OC方法将图片绘制到layer上
// 利用drawInRect方法绘制图片到layer, 是通过拉伸原有图片
[image drawInRect:CGRectMake(0, 0, 200, 200)];
// 利用drawAsPatternInRec方法绘制图片到layer, 是通过平铺原有图片
// [image drawAsPatternInRect:CGRectMake(0, 0, 320, 480)];
}
@end
(3、)
#import "MMImage.h"
@implementation MMImage
- (void)drawRect:(CGRect)rect
{
// 1.加载图片到内存中
UIImage *image = [UIImage imageNamed:@"me"];
// 利用OC方法将图片绘制到layer上
// 将图片绘制到指定的位置
[image drawAtPoint:CGPointMake(100, 100)];
}
相关文章推荐
- iOS陆哥开发笔记(十五) (Quartz2D简单使用一)
- iOS陆哥开发笔记(十四) (Quartz2D简单介绍)
- iOS陆哥开发笔记(十三) (Xcode统计代码行数)
- iOS开发小技巧--高斯模糊框架的应用
- nagios云监控
- Xamarin.iOS - Handing Keyboard
- iOS陆哥开发笔记(十二) (tableViewCell侧滑显示多个按钮)
- iOS开发之MBProgressHUD的使用
- iOS 项目审核悲剧原因
- iOS陆哥开发笔记(十一) (容易造成循环引用的三种场景)
- iOS陆哥开发笔记(十) (iOS设计模式—单例模式)
- ios7之后的动画
- iOS开发小技巧--键盘处理以及解决block造成循环引用的小技巧
- iOS陆哥开发笔记(九) (SDWebImage简单使用)
- iOS - 加速计(重力感应)
- iOS陆哥开发笔记(八) (GCD死锁及解决方案)
- iOS陆哥开发笔记(七) (AVFoundation简介)
- IOS加速审核 24小时通过 100% 成功
- iOS陆哥开发笔记(六) (cocoaPod的简单使用)
- iOS彩票项目--第六天,运用MVC思想搭建设置界面(非storyboard方法)