ios 学习之你画我话绘图一 绘制文本和图像


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,绘图)