ios 学习之你画我话绘图一 绘制文本和图像
来源:互联网 发布:戴维斯数据 编辑:程序博客网 时间:2024/04/30 17:43
ios sdk 给我们提供了丰富的字体,我们通过枚举可以打印出字体的名字。
-(void)enumerateFont
{
for (NSString *familyNamein [UIFontfamilyNames])
{
NSLog(@"font family = %@",familyName );
for (NSString *fontNamein [UIFontfontNamesForFamilyName:familyName]) {
NSLog(@"\t %@",fontName );
}
}
下面我们绘制一个褐色的hello文本!在view上
- (void)drawRect:(CGRect)rect
{
[super drawRect:rect];
NSString *attrString =@"hello";
UIColor *stringColor = [UIColorcolorWithRed:0.5fgreen:0.0fblue:0.5falpha:1.0]; //设置文本的颜色
NSDictionary* attrs =@{NSForegroundColorAttributeName:stringColor,
NSFontAttributeName:[UIFontfontWithName:@"AmericanTypewriter"size:18],
}; //在词典中加入文本的颜色 字体 大小
// [attrString drawAtPoint:CGPointMake(100, 100) withAttributes:attrs ]; //根据坐标点画在view上
[attrString drawInRect:CGRectMake(150,120,100,200)withAttributes:attrs]; //给文本限制个矩形边界,防止矩形拉伸;
}
绘制图像和文本用的一个函数
UIImage *image = [UIImageimageNamed:@"see.jpg"];
//[image drawAtPoint:CGPointMake(20, 100)];
[image drawInRect:CGRectMake(40,60,80,80)];
、、、、、、、、、、、、、、、、、、、、、、、、、、、下面是官方代码。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
#pragma mark -
@implementation QuartzTextView
#define kTextString "Hello From Quartz"
#define kTextStringLength strlen(kTextString)
-(void)drawInContext:(CGContextRef)context
{
CGContextSetRGBFillColor(context,1.0,1.0,1.0,1.0);
CGContextSetRGBStrokeColor(context,1.0,0.0,0.0,1.0);
// Some initial setup for our text drawing needs.
// First, we will be doing our drawing in Helvetica-36pt with the MacRoman encoding.
// This is an 8-bit encoding that can reference standard ASCII characters
// and many common characters used in the Americas and Western Europe.
CGContextSelectFont(context,"Helvetica",36.0,kCGEncodingMacRoman);
// Next we set the text matrix to flip our text upside down. We do this because the context itself
// is flipped upside down relative to the expected orientation for drawing text (much like the case for drawing Images & PDF).
CGContextSetTextMatrix(context,CGAffineTransformMakeScale(1.0, -1.0));
// And now we actually draw some text. This screen will demonstrate the typical drawing modes used.
CGContextSetTextDrawingMode(context,kCGTextFill);
CGContextShowTextAtPoint(context,10.0,30.0,kTextString,kTextStringLength);
CGContextSetTextDrawingMode(context,kCGTextStroke);
CGContextShowTextAtPoint(context,10.0,70.0,kTextString,kTextStringLength);
CGContextSetTextDrawingMode(context,kCGTextFillStroke);
CGContextShowTextAtPoint(context,10.0,110.0,kTextString,kTextStringLength);
// Now lets try the more complex Glyph functions. These functions allow you to draw any glyph available in a font,
// but provide no assistance with converting characters to glyphs or layout, and as such require considerably more knowledge
// of text to use correctly. Specifically, you need to understand Unicode encoding and how to interpret the information
// present in the font itself, such as the cmap table.
// To get you started, we are going to do the minimum necessary to draw a glyphs into the current context.
CGFontRef helvetica = CGFontCreateWithFontName((CFStringRef)@"Helvetica");
CGContextSetFont(context, helvetica);
CGContextSetFontSize(context,12.0);
CGContextSetTextDrawingMode(context,kCGTextFill);
// Next we'll display lots of glyphs from the font.
CGGlyph start = 0;
for(int y =0; y <20; ++y)
{
CGGlyph glyphs[32];
for(int i =0; i <32; ++i)
{
glyphs[i] = start + i;
}
start += 32;
CGContextShowGlyphsAtPoint(context,10.0,150.0 + 12 * y, glyphs,32);
}
CGFontRelease(helvetica);
}
@end
-(void)drawInContext:(CGContextRef)context
{
CGRect imageRect;
imageRect.origin = CGPointMake(8.0, 8.0);
imageRect.size = CGSizeMake(64.0, 64.0);
// Note: The images are actually drawn upside down because Quartz image drawing expects
// the coordinate system to have the origin in the lower-left corner, but a UIView
// puts the origin in the upper-left corner. For the sake of brevity (and because
// it likely would go unnoticed for the image used) this is not addressed here.
// For the demonstration of PDF drawing however, it is addressed, as it would definately
// be noticed, and one method of addressing it is shown there.
// Draw the image in the upper left corner (0,0) with size 64x64
CGContextDrawImage(context, imageRect,self.image);
// Tile the same image across the bottom of the view
// CGContextDrawTiledImage() will fill the entire clipping area with the image, so to avoid
// filling the entire view, we'll clip the view to the rect below. This rect extends
// past the region of the view, but since the view's rectangle has already been applied as a clip
// to our drawing area, it will be intersected with this rect to form the final clipping area
CGContextClipToRect(context,CGRectMake(0.0,80.0, self.bounds.size.width,self.bounds.size.height));
// The origin of the image rect works similarly to the phase parameter for SetLineDash and
// SetPatternPhase and specifies where in the coordinate system the "first" image is drawn.
// The size (previously set to 64x64) specifies the size the image is scaled to before being tiled.
imageRect.origin = CGPointMake(32.0, 112.0);
CGContextDrawTiledImage(context, imageRect,self.image);
// Highlight the "first" image from the DrawTiledImage call.
CGContextSetRGBFillColor(context,1.0, 0.0,0.0, 0.5);
CGContextFillRect(context, imageRect);
// And stroke the clipped area
CGContextSetLineWidth(context,3.0);
CGContextSetRGBStrokeColor(context,1.0, 0.0,0.0, 1.0);
CGContextStrokeRect(context,CGContextGetClipBoundingBox(context));
}
- ios 学习之你画我话绘图一 绘制文本和图像
- ios 学习之你画我话绘图七 椭圆形
- ios 学习之你画我话绘图三 画线
- ios 学习之你画我话绘图六 矩形
- ios 学习之你画我话绘图二 熟悉下CGContextRef
- ios 学习之你画我话绘图四 读取pdf (官方代码)
- ios 学习之你画我话绘图五 构造路径基础知识
- Quartz2D绘图之绘制文本
- iOS图像处理(2)绘制文本
- iOS绘图系列四:绘制文字和图像CGContextDrawImage,drawInRect:,drawAtPoint:, UIGraphicsBeginImageContext
- iOS绘图系列六:利用CAShapeLayer和UIBezierPath或者CGPath绘制想要的图像
- 绘图-图像绘制相关
- 【IOS学习】之三、图像视图&文本字段
- IOS学习——图像视图 和 文本字段
- 【OpenCV学习笔记】一、装载、 显示和存储图像及在图像上绘图
- GC绘图:绘制透明文本
- HTML5之Canvas 2D入门1 - Canvas绘制文本和图像
- ios绘图:绘制图形
- Solr开发文档
- Unity3D之MonoBehaviour的可重写函数整理
- 线程的挂起join
- 变量标识、数据类型、有无符号、是否静态
- android 截屏 和屏幕录像
- ios 学习之你画我话绘图一 绘制文本和图像
- php 解析xml 的四种方法(转)
- SSL证书申请流程
- 验证控件知多少
- 修改 Moodle 的文章字体大小及行距 标签的字体待修改
- 实战OpenCV库之 旋转图像
- Unity3D脚本学习——运行时类
- 关键字static的作用与关键字const有什么含意?
- Eclipse中设置在创建新类时自动生成注释