CGContext Reference
The CGContextRef opaque type represents a Quartz 2D drawing destination. A graphics context contains drawing parameters and all device-specific information needed to render the paint on a page to the destination, whether the destination is a window in an application, a bitmap image, a PDF document, or a printer. You can obtain a graphics context by using Quartz graphics context creation functions or by using higher-level functions provided in the Carbon, Cocoa, or Printing frameworks. Quartz provides creation functions for various flavors of Quartz graphics contexts including bitmap images and PDF. The Cocoa framework provides functions for obtaining window graphics contexts. The Printing framework provides functions that obtain a graphics context appropriate for the destination printer.
-
Forces all pending drawing operations in a window context to be rendered immediately to the destination device.
Declaration
Swift
func CGContextFlush(_c: CGContext?)Objective-C
void CGContextFlush ( CGContextRef c );Parameters
cThe window context to flush. If you pass a PDF context or a bitmap context, this function does nothing.
Discussion
When you call this function, Quartz immediately flushes the current drawing to the destination device (for example, a screen). Because the system software flushes a context automatically at the appropriate times, calling this function could have an adverse effect on performance. Under normal conditions, you do not need to call this function.
Availability
Available in iOS 2.0 and later.
-
Returns the type identifier for Quartz graphics contexts.
Declaration
Swift
func CGContextGetTypeID() -> CFTypeIDObjective-C
CFTypeID CGContextGetTypeID ( void );Return Value
The identifier for the opaque type
CGContextRef.Availability
Available in iOS 2.0 and later.
-
Decrements the retain count of a graphics context.
Declaration
Objective-C
void CGContextRelease ( CGContextRef c );Parameters
cThe graphics context to release.
Discussion
This function is equivalent to
CFRelease, except it does not cause an error ifcisNULL.Availability
Available in iOS 2.0 and later.
-
Increments the retain count of a graphics context.
Declaration
Objective-C
CGContextRef CGContextRetain ( CGContextRef c );Parameters
cThe graphics context to retain.
Return Value
The same graphics context you passed in as the
contextparameter.Discussion
This function is equivalent to
CFRetain, except it does not cause an error ifcisNULL.Availability
Available in iOS 2.0 and later.
-
Marks a window context for update.
Declaration
Swift
func CGContextSynchronize(_c: CGContext?)Objective-C
void CGContextSynchronize ( CGContextRef c );Parameters
cThe window context to synchronize. If you pass a PDF context or a bitmap context, this function does nothing.
Discussion
When you call this function, all drawing operations since the last update are flushed at the next regular opportunity. Under normal conditions, you do not need to call this function.
Availability
Available in iOS 2.0 and later.
-
Pushes a copy of the current graphics state onto the graphics state stack for the context.
Declaration
Swift
func CGContextSaveGState(_c: CGContext?)Objective-C
void CGContextSaveGState ( CGContextRef c );Parameters
cThe graphics context whose current graphics state you want to save.
Discussion
Each graphics context maintains a stack of graphics states. Note that not all aspects of the current drawing environment are elements of the graphics state. For example, the current path is not considered part of the graphics state and is therefore not saved when you call the
CGContextSaveGStatefunction. The graphics state parameters that are saved are:CTM (current transformation matrix)
clip region
image interpolation quality
line width
line join
miter limit
line cap
line dash
flatness
should anti-alias
rendering intent
fill color space
stroke color space
fill color
stroke color
alpha value
font
font size
character spacing
text drawing mode
shadow parameters
the pattern phase
the font smoothing parameter
blend mode
To restore your drawing environment to a previously saved state, you can use
CGContextRestoreGState.Availability
Available in iOS 2.0 and later.
-
Sets the current graphics state to the state most recently saved.
Declaration
Swift
func CGContextRestoreGState(_c: CGContext?)Objective-C
void CGContextRestoreGState ( CGContextRef c );Parameters
cThe graphics context whose state you want to modify.
Discussion
Quartz removes the graphics state that is at the top of the stack so that the most recently saved state becomes the current graphics state.
Availability
Available in iOS 2.0 and later.
See Also
-
Returns the current level of interpolation quality for a graphics context.
Declaration
Swift
func CGContextGetInterpolationQuality(_c: CGContext?) -> CGInterpolationQualityObjective-C
CGInterpolationQuality CGContextGetInterpolationQuality ( CGContextRef c );Parameters
cThe graphics context to examine.
Return Value
The current level of interpolation quality.
Discussion
Interpolation quality is a graphics state parameter that provides a hint for the level of quality to use for image interpolation (for example, when scaling the image). Not all contexts support all interpolation quality levels.
Availability
Available in iOS 2.0 and later.
See Also
-
Sets the accuracy of curved paths in a graphics context.
Declaration
Swift
func CGContextSetFlatness(_c: CGContext?, _flatness: CGFloat)Objective-C
void CGContextSetFlatness ( CGContextRef c, CGFloat flatness );Parameters
cThe graphics context to modify.
flatnessThe largest permissible distance, measured in device pixels, between a point on the true curve and a point on the approximated curve.
Discussion
This function controls how accurately curved paths are rendered. Setting the flatness value to less than
1.0renders highly accurate curves, but lengthens rendering times.In most cases, you should not change the flatness value. Customizing the flatness value for the capabilities of a particular output device impairs the ability of your application to render to other devices.
Availability
Available in iOS 2.0 and later.
-
Sets the level of interpolation quality for a graphics context.
Declaration
Swift
func CGContextSetInterpolationQuality(_c: CGContext?, _quality: CGInterpolationQuality)Objective-C
void CGContextSetInterpolationQuality ( CGContextRef c, CGInterpolationQuality quality );Parameters
cThe graphics context to modify.
qualityA
CGInterpolationQualityconstant that specifies the required level of interpolation quality. For possible values, see“CGInterpolationQuality”.Discussion
Interpolation quality is merely a hint to the context—not all contexts support all interpolation quality levels.
Availability
Available in iOS 2.0 and later.
See Also
-
Sets the style for the endpoints of lines drawn in a graphics context.
Declaration
Swift
func CGContextSetLineCap(_c: CGContext?, _cap: CGLineCap)Objective-C
void CGContextSetLineCap ( CGContextRef c, CGLineCap cap );Parameters
cThe graphics context to modify.
capA line cap style constant—
kCGLineCapButt(the default),kCGLineCapRound, orkCGLineCapSquare. See CGPath Reference.Availability
Available in iOS 2.0 and later.
-
Sets the pattern for dashed lines in a graphics context.
Declaration
Swift
func CGContextSetLineDash(_c: CGContext?, _phase: CGFloat, _lengths: UnsafePointer<CGFloat>, _count: Int)Objective-C
void CGContextSetLineDash ( CGContextRef c, CGFloat phase, const CGFloat *lengths, size_t count );Parameters
cThe graphics context to modify.
phaseA value that specifies how far into the dash pattern the line starts, in units of the user space. For example, passing a value of
3means the line is drawn with the dash pattern starting at three units from its beginning. Passing a value of0draws a line starting with the beginning of a dash pattern.lengthsAn array of values that specify the lengths of the painted segments and unpainted segments, respectively, of the dash pattern—or
NULLfor no dash pattern.For example, passing an array with the values
[2,3]sets a dash pattern that alternates between a 2-user-space-unit-long painted segment and a 3-user-space-unit-long unpainted segment. Passing the values[1,3,4,2]sets the pattern to a 1-unit painted segment, a 3-unit unpainted segment, a 4-unit painted segment, and a 2-unit unpainted segment.countIf the
lengthsparameter specifies an array, pass the number of elements in the array. Otherwise, pass0.Availability
Available in iOS 2.0 and later.
-
Sets the style for the joins of connected lines in a graphics context.
Declaration
Swift
func CGContextSetLineJoin(_c: CGContext?, _join: CGLineJoin)Objective-C
void CGContextSetLineJoin ( CGContextRef c, CGLineJoin join );Parameters
cThe graphics context to modify.
joinA line join value—
kCGLineJoinMiter(the default),kCGLineJoinRound, orkCGLineJoinBevel. See CGPath Reference.Availability
Available in iOS 2.0 and later.
-
Sets the line width for a graphics context.
Declaration
Swift
func CGContextSetLineWidth(_c: CGContext?, _width: CGFloat)Objective-C
void CGContextSetLineWidth ( CGContextRef c, CGFloat width );Parameters
cThe graphics context to modify.
widthThe new line width to use, in user space units. The value must be greater than
0.Discussion
The default line width is
1unit. When stroked, the line straddles the path, with half of the total width on either side.Availability
Available in iOS 2.0 and later.
-
Sets the miter limit for the joins of connected lines in a graphics context.
Declaration
Swift
func CGContextSetMiterLimit(_c: CGContext?, _limit: CGFloat)Objective-C
void CGContextSetMiterLimit ( CGContextRef c, CGFloat limit );Parameters
cThe graphics context to modify.
limitThe miter limit to use.
Discussion
If the current line join style is set to
kCGLineJoinMiter(seeCGContextSetLineJoin), Quartz uses the miter limit to determine whether the lines should be joined with a bevel instead of a miter. Quartz divides the length of the miter by the line width. If the result is greater than the miter limit, Quartz converts the style to a bevel.Availability
Available in iOS 2.0 and later.
-
Sets the pattern phase of a context.
Declaration
Swift
func CGContextSetPatternPhase(_c: CGContext?, _phase: CGSize)Objective-C
void CGContextSetPatternPhase ( CGContextRef c, CGSize phase );Parameters
cThe graphics context to modify.
phaseA pattern phase, specified in user space.
Discussion
The pattern phase is a translation that Quartz applies prior to drawing a pattern in the context. The pattern phase is part of the graphics state of a context, and the default pattern phase is
(0,0). Setting the pattern phase has the effect of temporarily changing the pattern matrix of any pattern you draw. For example, setting the context’s pattern phase to(2,3)has the effect of moving the start of pattern cell tiling to the point(2,3)in default user space.Availability
Available in iOS 2.0 and later.
-
Sets the fill pattern in the specified graphics context.
Declaration
Swift
func CGContextSetFillPattern(_c: CGContext?, _pattern: CGPattern?, _components: UnsafePointer<CGFloat>)Objective-C
void CGContextSetFillPattern ( CGContextRef c, CGPatternRef pattern, const CGFloat *components );Parameters
cThe graphics context to modify.
patternA fill pattern. Quartz retains this object; upon return, you may safely release it.
componentsIf the pattern is an uncolored (or a masking) pattern, pass an array of intensity values that specify the color to use when the pattern is painted. The number of array elements must equal the number of components in the base space of the fill pattern color space, plus an additional component for the alpha value.
If the pattern is a colored pattern, pass an alpha value.
Discussion
The current fill color space must be a pattern color space. Otherwise, the result of calling this function is undefined. If you want to set a fill color, not a pattern, use
CGContextSetFillColorWithColor.Availability
Available in iOS 2.0 and later.
-
Sets the rendering intent in the current graphics state.
Declaration
Swift
func CGContextSetRenderingIntent(_c: CGContext?, _intent: CGColorRenderingIntent)Objective-C
void CGContextSetRenderingIntent ( CGContextRef c, CGColorRenderingIntent intent );Parameters
cThe graphics context to modify.
intentA rendering intent constant—
kCGRenderingIntentDefault,kCGRenderingIntentAbsoluteColorimetric,kCGRenderingIntentRelativeColorimetric,kCGRenderingIntentPerceptual, orkCGRenderingIntentSaturation. For a discussion of these constants, see CGColorSpace Reference.Discussion
The rendering intent specifies how Quartz should handle colors that are not located within the gamut of the destination color space of a graphics context. If you do not explicitly set the rendering intent, Quartz uses perceptual rendering intent for drawing sampled images and relative colorimetric rendering intent for all other drawing.
Availability
Available in iOS 2.0 and later.
-
Sets anti-aliasing on or off for a graphics context.
Declaration
Swift
func CGContextSetShouldAntialias(_c: CGContext?, _shouldAntialias: Bool)Objective-C
void CGContextSetShouldAntialias ( CGContextRef c, bool shouldAntialias );Parameters
cThe graphics context to modify.
shouldAntialiasA Boolean value that specifies whether anti-aliasing should be turned on. Anti-aliasing is turned on by default when a window or bitmap context is created. It is turned off for other types of contexts.
Discussion
Anti-aliasing is a graphics state parameter.
Availability
Available in iOS 2.0 and later.
-
Sets the stroke pattern in the specified graphics context.
Declaration
Swift
func CGContextSetStrokePattern(_c: CGContext?, _pattern: CGPattern?, _components: UnsafePointer<CGFloat>)Objective-C
void CGContextSetStrokePattern ( CGContextRef c, CGPatternRef pattern, const CGFloat *components );Parameters
cThe graphics context to modify.
patternA pattern for stroking. Quartz retains this object; upon return, you may safely release it.
componentsIf the specified pattern is an uncolored (or masking) pattern, pass an array of intensity values that specify the color to use when the pattern is painted. The number of array elements must equal the number of components in the base space of the stroke pattern color space, plus an additional component for the alpha value.
If the specified pattern is a colored pattern, pass an alpha value.
Discussion
The current stroke color space must be a pattern color space. Otherwise, the result of calling this function is undefined. If you want to set a stroke color, not a stroke pattern, use
CGContextSetStrokeColorWithColor.Availability
Available in iOS 2.0 and later.
-
Sets how Quartz composites sample values for a graphics context.
Declaration
Swift
func CGContextSetBlendMode(_c: CGContext?, _mode: CGBlendMode)Objective-C
void CGContextSetBlendMode ( CGContextRef c, CGBlendMode mode );Parameters
contextThe graphics context to modify.
modeA blend mode. See
“CGBlendMode”for a list of the constants you can supply.Availability
Available in iOS 2.0 and later.
-
Sets whether or not to allow anti-aliasing for a graphics context.
Declaration
Swift
func CGContextSetAllowsAntialiasing(_c: CGContext?, _allowsAntialiasing: Bool)Objective-C
void CGContextSetAllowsAntialiasing ( CGContextRef c, bool allowsAntialiasing );Parameters
contextA graphics context.
allowsAntialiasingA Boolean value that specifies whether or not to allow antialiasing. Pass
trueto allow antialiasing;falseotherwise. This parameter is not part of the graphics state.Discussion
Quartz performs antialiasing for a graphics context if both the
allowsAntialiasingparameter and the graphics state parametershouldAntialiasaretrue.This parameter is not part of the graphics state.
Availability
Available in iOS 2.0 and later.
-
Sets whether or not to allow font smoothing for a graphics context.
Declaration
Swift
func CGContextSetAllowsFontSmoothing(_c: CGContext?, _allowsFontSmoothing: Bool)Objective-C
void CGContextSetAllowsFontSmoothing ( CGContextRef c, bool allowsFontSmoothing );Parameters
contextA graphics context.
allowsFontSmoothingA Boolean value that specifies whether font smoothing is allowed in the specified context.
Discussion
Font are smoothed if they are antialiased when drawn and if font smoothing is both allowed and enabled. For information on how to enable font smoothing, see the
CGContextSetShouldSmoothFontsfunction. It is not usually necessary to make changes to both parameters at the same time; either can be used to disable font smoothing.This parameter is not part of the graphics state.
Availability
Available in iOS 2.0 and later.
-
Enables or disables font smoothing in a graphics context.
Declaration
Swift
func CGContextSetShouldSmoothFonts(_c: CGContext?, _shouldSmoothFonts: Bool)Objective-C
void CGContextSetShouldSmoothFonts ( CGContextRef c, bool shouldSmoothFonts );Parameters
cThe graphics context to modify.
shouldSmoothFontsA Boolean value that specifies whether to enable font smoothing.
Discussion
There are cases, such as rendering to a bitmap, when font smoothing is not appropriate and should be disabled. Note that some contexts (such as PostScript contexts) do not support font smoothing.
This parameter is part of the graphics state. Because of this, you use this when you want to temporarily override this setting in a drawing method.
Availability
Available in iOS 2.0 and later.
-
Sets whether or not to allow subpixel positioning for a graphics context
Declaration
Swift
func CGContextSetAllowsFontSubpixelPositioning(_c: CGContext?, _allowsFontSubpixelPositioning: Bool)Objective-C
void CGContextSetAllowsFontSubpixelPositioning ( CGContextRef c, bool allowsFontSubpixelPositioning );Parameters
contextA graphics context.
allowsFontSubpixelPositioningA Boolean value that specifies whether subpixel positioning of glyphs is allowed in the specified context.
Discussion
Sub-pixel positioning is used by the graphics context if it is allowed, enabled, and if the font itself is antialiased when drawn. For information on how to enable subpixel positioning, see the
CGContextSetShouldSubpixelPositionFontsfunction.This parameter is not part of the graphics state.
Availability
Available in iOS 2.0 and later.
-
Enables or disables subpixel positioning in a graphics context.
Declaration
Swift
func CGContextSetShouldSubpixelPositionFonts(_c: CGContext?, _shouldSubpixelPositionFonts: Bool)Objective-C
void CGContextSetShouldSubpixelPositionFonts ( CGContextRef c, bool shouldSubpixelPositionFonts );Parameters
contextA graphics context.
shouldSubpixelPositionFontsA Boolean value that specifies whether to enable subpixel positioning.
Discussion
When enabled, the graphics context may position glyphs on nonintegral pixel boundaries. When disabled, the position of glyphs are always forced to integral pixel boundaries.
This parameter is part of the graphics state.
Availability
Available in iOS 2.0 and later.
-
Sets whether or not to allow subpixel quantization for a graphics context
Declaration
Swift
func CGContextSetAllowsFontSubpixelQuantization(_c: CGContext?, _allowsFontSubpixelQuantization: Bool)Objective-C
void CGContextSetAllowsFontSubpixelQuantization ( CGContextRef c, bool allowsFontSubpixelQuantization );Parameters
contextA graphics context.
allowsFontSubpixelQuantizationA Boolean value that specifies whether subpixel quantization of glyphs is allowed in the specified context.
Discussion
Sub-pixel quantization is used by the graphics context if it is allowed, enabled, and if glyphs would be drawn at subpixel positions. For information on how to enable subpixel quantization, see the
CGContextSetShouldSubpixelQuantizeFontsfunction.This parameter is not part of the graphics state.
Availability
Available in iOS 2.0 and later.
-
Enables or disables subpixel quantization in a graphics context.
Declaration
Swift
func CGContextSetShouldSubpixelQuantizeFonts(_c: CGContext?, _shouldSubpixelQuantizeFonts: Bool)Objective-C
void CGContextSetShouldSubpixelQuantizeFonts ( CGContextRef c, bool shouldSubpixelQuantizeFonts );Parameters
contextA graphics context.
shouldSubpixelQuantizeFontsA Boolean value that specifies whether to enable subpixel quantization.
Discussion
When enabled, the graphics context may quantize the subpixel positions of glyphs.
This parameter is part of the graphics state.
Availability
Available in iOS 2.0 and later.
These functions are used to define the geometry of the current path. For more information on how paths are defined, see CGPath Reference.
-
Adds an arc of a circle to the current path, possibly preceded by a straight line segment
Declaration
Swift
func CGContextAddArc(_c: CGContext?, _x: CGFloat, _y: CGFloat, _radius: CGFloat, _startAngle: CGFloat, _endAngle: CGFloat, _clockwise: Int32)Parameters
cA graphics context.
xThe x-value, in user space coordinates, for the center of the arc.
yThe y-value, in user space coordinates, for the center of the arc.
radiusThe radius of the arc, in user space coordinates.
startAngleThe angle to the starting point of the arc, measured in radians from the positive x-axis.
endAngleThe angle to the end point of the arc, measured in radians from the positive x-axis.
clockwiseSpecify
1to create a clockwise arc or0to create a counterclockwise arc.Discussion
An arc is a segment of a circle with radius r centered at a point
(x,y). When you call this function, you provide the center point, radius, and two angles in radians. Quartz uses this information to determine the end points of the arc, and then approximates the new arc using a sequence of cubic Bézier curves. Theclockwiseparameter determines the direction in which the arc is created; the actual direction of the final path is dependent on the current transformation matrix of the graphics context. For example, on iOS, aUIViewflips the Y-coordinate by scaling the Y values by-1. In a flipped coordinate system, specifying a clockwise arc results in a counterclockwise arc after the transformation is applied.If the current path already contains a subpath, Quartz adds a line connecting the current point to the starting point of the arc. If the current path is empty, Quartz creates a new new subpath with a starting point set to the starting point of the arc.
The ending point of the arc becomes the new current point of the path.
Availability
Available in iOS 2.0 and later.
See Also
-
Adds an arc of a circle to the current path, using a radius and tangent points.
Declaration
Swift
func CGContextAddArcToPoint(_c: CGContext?, _x1: CGFloat, _y1: CGFloat, _x2: CGFloat, _y2: CGFloat, _radius: CGFloat)Parameters
cA graphics context whose current path is not empty.
x1The x-value, in user space coordinates, for the end point of the first tangent line. The first tangent line is drawn from the current point to (x1,y1).
y1The y-value, in user space coordinates, for the end point of the first tangent line. The first tangent line is drawn from the current point to (x1,y1).
x2The x-value, in user space coordinates, for the end point of the second tangent line. The second tangent line is drawn from (x1,y1) to (x2,y2).
y2The y-value, in user space coordinates, for the end point of the second tangent line. The second tangent line is drawn from (x1,y1) to (x2,y2).
radiusThe radius of the arc, in user space coordinates.
Discussion
This function uses a sequence of cubic Bézier curves to create an arc that is tangent to the line from the current point to (x1,y1) and to the line from (x1,y1) to (x2,y2). The start and end points of the arc are located on the first and second tangent lines, respectively. The start and end points of the arc are also the “tangent points” of the lines.
If the current point and the first tangent point of the arc (the starting point) are not equal, Quartz appends a straight line segment from the current point to the first tangent point.
The ending point of the arc becomes the new current point of the path.
Availability
Available in iOS 2.0 and later.
-
Appends a cubic Bézier curve from the current point, using the provided control points and end point .
Declaration
Swift
func CGContextAddCurveToPoint(_c: CGContext?, _cp1x: CGFloat, _cp1y: CGFloat, _cp2x: CGFloat, _cp2y: CGFloat, _x: CGFloat, _y: CGFloat)Parameters
cA graphics context whose current path is not empty.
cp1xThe x-value, in user space coordinates, for the first control point of the curve.
cp1yThe y-value, in user space coordinates, for the first control point of the curve.
cp2xThe x-value, in user space coordinates, for the second control point of the curve.
cp2yThe y-value, in user space coordinates, for the second control point of the curve.
xThe x-value, in user space coordinates, at which to end the curve.
yThe y-value, in user space coordinates, at which to end the curve.
Discussion
This function appends a cubic curve to the current path. On return, the current point is set to the end point of that segment.
Availability
Available in iOS 2.0 and later.
-
Adds a sequence of connected straight-line segments to the current path.
Declaration
Swift
func CGContextAddLines(_c: CGContext?, _points: UnsafePointer<CGPoint>, _count: Int)Objective-C
void CGContextAddLines ( CGContextRef c, const CGPoint *points, size_t count );Parameters
cA graphics context .
pointsAn array of values that specify the start and end points of the line segments to draw. Each point in the array specifies a position in user space. The first point in the array specifies the initial starting point.
countThe number of elements in the
pointsarray.Discussion
This is a convenience function that adds a sequence of connected line segments to a path, using the following operation:
CGContextMoveToPoint (c, points[0].x, points[0].y);for (k = 1; k < count; k++) {CGContextAddLineToPoint (c, points[k].x, points[k].y);}
Availability
Available in iOS 2.0 and later.
See Also
-
Appends a straight line segment from the current point to the provided point .
Declaration
Swift
func CGContextAddLineToPoint(_c: CGContext?, _x: CGFloat, _y: CGFloat)Objective-C
void CGContextAddLineToPoint ( CGContextRef c, CGFloat x, CGFloat y );Parameters
cA graphics context whose current path is not empty.
xThe x-value, in user space coordinates, for the end of the line segment.
yThe y-value, in user space coordinates, for the end of the line segment.
Discussion
After adding the line segment, the current point is set to the endpoint of the line segment.
Availability
Available in iOS 2.0 and later.
See Also
-
Adds a previously created Quartz path object to the current path in a graphics context.
Declaration
Swift
func CGContextAddPath(_c: CGContext?, _path: CGPath?)Objective-C
void CGContextAddPath ( CGContextRef c, CGPathRef path );Parameters
contextA graphics context .
pathA previously created Quartz path object. See CGPath Reference.
Discussion
If the source path is non-empty, then its path elements are appended in order onto the current path. Quartz applies the current transformation matrix (CTM) to the points before adding them to the path.
After the call completes, the start point and current point of the path are those of the last subpath in
path.Availability
Available in iOS 2.0 and later.
-
Returns a Quartz path object built from the current path information in a graphics context.
Declaration
Swift
func CGContextCopyPath(_c: CGContext?) -> CGPath?Objective-C
CGPathRef CGContextCopyPath ( CGContextRef c );Parameters
contextA graphics context whose current path is not empty.
Return Value
A Quartz path object containing the current path data.
Availability
Available in iOS 2.0 and later.
-
Appends a quadratic Bézier curve from the current point, using a control point and an end point you specify.
Declaration
Swift
func CGContextAddQuadCurveToPoint(_c: CGContext?, _cpx: CGFloat, _cpy: CGFloat, _x: CGFloat, _y: CGFloat)Objective-C
void CGContextAddQuadCurveToPoint ( CGContextRef c, CGFloat cpx, CGFloat cpy, CGFloat x, CGFloat y );Parameters
cA graphics context whose current path is not empty.
cpxThe x-coordinate of the user space for the control point of the curve.
cpyThe y-coordinate of the user space for the control point of the curve.
xThe x-coordinate of the user space at which to end the curve.
yThe y-coordinate of the user space at which to end the curve.
Discussion
This function appends a quadratic curve to the current subpath. After adding the segment, the current point is set to the end point of the curve.
Availability
Available in iOS 2.0 and later.
-
Adds a rectangular path to the current path.
Declaration
Swift
func CGContextAddRect(_c: CGContext?, _rect: CGRect)Objective-C
void CGContextAddRect ( CGContextRef c, CGRect rect );Parameters
cA graphics context.
rectA rectangle, specified in user space coordinates.
Discussion
This is a convenience function that adds a rectangle to a path, using the following sequence of operations:
// start at originCGContextMoveToPoint (c, CGRectGetMinX(rect), CGRectGetMinY(rect));// add bottom edgeCGContextAddLineToPoint (c, CGRectGetMaxX(rect), CGRectGetMinY(rect));// add right edgeCGContextAddLineToPoint (c, CGRectGetMaxX(rect), CGRectGetMaxY(rect);// add top edgeCGContextAddLineToPoint (c, CGRectGetMinX(rect), CGRectGetMaxY(rect));// add left edge and closeCGContextClosePath (c);
Availability
Available in iOS 2.0 and later.
See Also
-
Adds a set rectangular paths to the current path.
Declaration
Swift
func CGContextAddRects(_c: CGContext?, _rects: UnsafePointer<CGRect>, _count: Int)Objective-C
void CGContextAddRects ( CGContextRef c, const CGRect *rects, size_t count );Parameters
cA graphics context.
rectsAn array of rectangles, specified in user space coordinates.
countThe number of rectangles in the
rectsarray.Discussion
This is a convenience function that adds an array of rectangles to a path, using the following operation:
for (k = 0; k < count; k++) {CGContextAddRect (c, m, rects[k]);}
Availability
Available in iOS 2.0 and later.
See Also
-
Creates a new empty path in a graphics context.
Declaration
Swift
func CGContextBeginPath(_c: CGContext?)Objective-C
void CGContextBeginPath ( CGContextRef c );Parameters
cA graphics context.
Discussion
A graphics context can have only a single path in use at any time. If the specified context already contains a current path when you call this function, Quartz discards the old path and any data associated with it.
The current path is not part of the graphics state. Consequently, saving and restoring the graphics state has no effect on the current path.
Availability
Available in iOS 2.0 and later.
See Also
-
Closes and terminates the current path’s subpath.
Declaration
Swift
func CGContextClosePath(_c: CGContext?)Objective-C
void CGContextClosePath ( CGContextRef c );Parameters
cA graphics context.
Discussion
Appends a line from the current point to the starting point of the current subpath and ends the subpath.
After closing the subpath, your application can begin a new subpath without first calling
CGContextMoveToPoint. In this case, a new subpath is implicitly created with a starting and current point equal to the previous subpath’s starting point.If the current path is empty or the current subpath is already closed, this function does nothing.
Availability
Available in iOS 2.0 and later.
See Also
-
Begins a new subpath at the point you specify.
Declaration
Swift
func CGContextMoveToPoint(_c: CGContext?, _x: CGFloat, _y: CGFloat)Objective-C
void CGContextMoveToPoint ( CGContextRef c, CGFloat x, CGFloat y );Parameters
cA graphics context.
xThe x-value, in user space coordinates, for the point.
yThe y-value, in user space coordinates, for the point.
Discussion
This point you specify becomes the start point of a new subpath. The current point is set to this start point.
Availability
Available in iOS 2.0 and later.
-
Adds an ellipse that fits inside the specified rectangle.
Declaration
Swift
func CGContextAddEllipseInRect(_c: CGContext?, _rect: CGRect)Objective-C
void CGContextAddEllipseInRect ( CGContextRef c, CGRect rect );Parameters
contextA graphics context.
rectA rectangle that defines the area for the ellipse to fit in.
Discussion
The ellipse is approximated by a sequence of Bézier curves. Its center is the midpoint of the rectangle defined by the
rectparameter. If the rectangle is square, then the ellipse is circular with a radius equal to one-half the width (or height) of the rectangle. If therectparameter specifies a rectangular shape, then the major and minor axes of the ellipse are defined by the width and height of the rectangle.The ellipse forms a complete subpath of the path—that is, the ellipse drawing starts with a move-to operation and ends with a close-subpath operation, with all moves oriented in the clockwise direction.
Availability
Available in iOS 2.0 and later.
These functions are used to stroke along or fill in the current path.
-
Paints a transparent rectangle.
Declaration
Swift
func CGContextClearRect(_c: CGContext?, _rect: CGRect)Objective-C
void CGContextClearRect ( CGContextRef c, CGRect rect );Parameters
cThe graphics context in which to paint the rectangle.
rectThe rectangle, in user space coordinates.
Discussion
If the provided context is a window or bitmap context, Quartz effectively clears the rectangle. For other context types, Quartz fills the rectangle in a device-dependent manner. However, you should not use this function in contexts other than window or bitmap contexts.
Availability
Available in iOS 2.0 and later.
-
Draws the current path using the provided drawing mode.
Declaration
Swift
func CGContextDrawPath(_c: CGContext?, _mode: CGPathDrawingMode)Objective-C
void CGContextDrawPath ( CGContextRef c, CGPathDrawingMode mode );Parameters
cA graphics context that contains a path to paint.
modeA path drawing mode constant—
kCGPathFill,kCGPathEOFill,kCGPathStroke,kCGPathFillStroke, orkCGPathEOFillStroke. For a discussion of these constants, see CGPath Reference.Discussion
As a side effect when you call this function, Quartz clears the current path.
Availability
Available in iOS 2.0 and later.
-
Paints the area within the current path, using the even-odd fill rule.
Declaration
Swift
func CGContextEOFillPath(_c: CGContext?)Objective-C
void CGContextEOFillPath ( CGContextRef c );Parameters
cA graphics context that contains a path to fill.
Discussion
Quartz treats each subpath as if it were closed by calling
CGContextClosePath. The even-odd rule is described in Filling a Path in Quartz 2D Programming Guide. As a side effect when you call this function, Quartz clears the current path.Availability
Available in iOS 2.0 and later.
-
Paints the area within the current path, using the nonzero winding number rule.
Declaration
Swift
func CGContextFillPath(_c: CGContext?)Objective-C
void CGContextFillPath ( CGContextRef c );Parameters
cA graphics context that contains a path to fill.
Discussion
Quartz treats each subpath as if it were closed by calling
CGContextClosePath. The nonzero winding number rule is described in Filling a Path in Quartz 2D Programming Guide. As a side effect when you call this function, Quartz clears the current path.Availability
Available in iOS 2.0 and later.
-
Paints the area contained within the provided rectangle, using the fill color in the current graphics state.
Declaration
Swift
func CGContextFillRect(_c: CGContext?, _rect: CGRect)Objective-C
void CGContextFillRect ( CGContextRef c, CGRect rect );Parameters
cA graphics context.
rectA rectangle, in user space coordinates.
Discussion
As a side effect when you call this function, Quartz clears the current path.
Availability
Available in iOS 2.0 and later.
See Also
-
Paints the areas contained within the provided rectangles, using the fill color in the current graphics state.
Declaration
Swift
func CGContextFillRects(_c: CGContext?, _rects: UnsafePointer<CGRect>, _count: Int)Objective-C
void CGContextFillRects ( CGContextRef c, const CGRect *rects, size_t count );Parameters
cA graphics context .
rectsAn array of rectangles, in user space coordinates.
countThe number rectangles in the
rectsarray.Discussion
As a side effect when you call this function, Quartz clears the current path.
Availability
Available in iOS 2.0 and later.
See Also
-
Paints the area of the ellipse that fits inside the provided rectangle, using the fill color in the current graphics state.
Declaration
Swift
func CGContextFillEllipseInRect(_c: CGContext?, _rect: CGRect)Objective-C
void CGContextFillEllipseInRect ( CGContextRef c, CGRect rect );Parameters
contextA graphics context.
rectA rectangle that defines the area for the ellipse to fit in.
Discussion
As a side effect when you call this function, Quartz clears the current path.
Availability
Available in iOS 2.0 and later.
-
Paints a line along the current path.
Declaration
Swift
func CGContextStrokePath(_c: CGContext?)Objective-C
void CGContextStrokePath ( CGContextRef c );Parameters
cA graphics context.
Discussion
Quartz uses the line width and stroke color of the graphics state to paint the path. As a side effect when you call this function, Quartz clears the current path.
Availability
Available in iOS 2.0 and later.
-
Paints a rectangular path.
Declaration
Swift
func CGContextStrokeRect(_c: CGContext?, _rect: CGRect)Objective-C
void CGContextStrokeRect ( CGContextRef c, CGRect rect );Parameters
cA graphics context .
rectA rectangle, specified in user space coordinates.
Discussion
Quartz uses the line width and stroke color of the graphics state to paint the path. As a side effect when you call this function, Quartz clears the current path.
Availability
Available in iOS 2.0 and later.
See Also
-
Paints a rectangular path, using the specified line width.
Declaration
Swift
func CGContextStrokeRectWithWidth(_c: CGContext?, _rect: CGRect, _width: CGFloat)Objective-C
void CGContextStrokeRectWithWidth ( CGContextRef c, CGRect rect, CGFloat width );Parameters
cA graphics context.
rectA rectangle, in user space coordinates.
widthA value, in user space units, that is greater than zero. This value does not affect the line width values in the current graphics state.
Discussion
Aside from the line width value, Quartz uses the current attributes of the graphics state (such as stroke color) to paint the line. The line straddles the path, with half of the total width on either side.
As a side effect when you call this function, Quartz clears the current path.
Availability
Available in iOS 2.0 and later.
See Also
-
Replaces the path in the graphics context with the stroked version of the path.
Declaration
Swift
func CGContextReplacePathWithStrokedPath(_c: CGContext?)Objective-C
void CGContextReplacePathWithStrokedPath ( CGContextRef c );Parameters
cA graphics context.
Discussion
Quartz creates a stroked path using the parameters of the current graphics context. The new path is created so that filling it draws the same pixels as stroking the original path.You can use this path in the same way you use the path of any context. For example, you can clip to the stroked version of a path by calling this function followed by a call to the function
CGContextClip.Availability
Available in iOS 2.0 and later.
-
Strokes an ellipse that fits inside the specified rectangle.
Declaration
Swift
func CGContextStrokeEllipseInRect(_c: CGContext?, _rect: CGRect)Objective-C
void CGContextStrokeEllipseInRect ( CGContextRef c, CGRect rect );Parameters
contextA graphics context.
rectA rectangle that defines the area for the ellipse to fit in.
Discussion
As a side effect when you call this function, Quartz clears the current path.
Availability
Available in iOS 2.0 and later.
-
Strokes a sequence of line segments.
Declaration
Swift
func CGContextStrokeLineSegments(_c: CGContext?, _points: UnsafePointer<CGPoint>, _count: Int)Objective-C
void CGContextStrokeLineSegments ( CGContextRef c, const CGPoint *points, size_t count );Parameters
cA graphics context.
pointsAn array of points, organized as pairs—the starting point of a line segment followed by the ending point of a line segment. For example, the first point in the array specifies the starting position of the first line, the second point specifies the ending position of the first line, the third point specifies the starting position of the second line, and so forth.
countThe number of points in the
pointsarray.Discussion
This function is equivalent to the following code:
CGContextBeginPath (context);for (k = 0; k < count; k += 2) {CGContextMoveToPoint(context, s[k].x, s[k].y);CGContextAddLineToPoint(context, s[k+1].x, s[k+1].y);}CGContextStrokePath(context);
As a side effect when you call this function, Quartz clears the current path.
Availability
Available in iOS 2.0 and later.
-
Indicates whether the current path contains any subpaths.
Declaration
Swift
func CGContextIsPathEmpty(_c: CGContext?) -> BoolObjective-C
bool CGContextIsPathEmpty ( CGContextRef c );Parameters
cThe graphics context containing the path to examine.
Return Value
Returns
1if the context’s path contains no subpaths, otherwise returns0.Availability
Available in iOS 2.0 and later.
-
Returns the current point in a non-empty path.
Declaration
Swift
func CGContextGetPathCurrentPoint(_c: CGContext?) -> CGPointObjective-C
CGPoint CGContextGetPathCurrentPoint ( CGContextRef c );Parameters
cThe graphics context containing the path to examine.
Return Value
A
CGPointvalue that specifies the location, in user space, of current point in the context’s path. If there is no path, the function returnsCGPointZero.Availability
Available in iOS 2.0 and later.
-
Returns the smallest rectangle that contains the current path.
Declaration
Swift
func CGContextGetPathBoundingBox(_c: CGContext?) -> CGRectObjective-C
CGRect CGContextGetPathBoundingBox ( CGContextRef c );Parameters
cThe graphics context, containing a path, to examine.
Return Value
A
CGRectvalue that specifies the dimensions and location, in user space, of the bounding box of the path. If there is no path, the function returnsCGRectNull.Discussion
The bounding box is the smallest rectangle completely enclosing all points in a path, including control points for Bézier cubic and quadratic curves.
Availability
Available in iOS 2.0 and later.
-
Checks to see whether the specified point is contained in the current path.
Declaration
Swift
func CGContextPathContainsPoint(_c: CGContext?, _point: CGPoint, _mode: CGPathDrawingMode) -> BoolObjective-C
bool CGContextPathContainsPoint ( CGContextRef c, CGPoint point, CGPathDrawingMode mode );Parameters
contextA graphics context.
pointThe point to check, specified in user space units.
modeA path drawing mode—
kCGPathFill,kCGPathEOFill,kCGPathStroke,kCGPathFillStroke, orkCGPathEOFillStroke. See CGPath Reference.Return Value
Returns
trueifpointis inside the current path of the graphics context;falseotherwise.Discussion
A point is contained within the path of a graphics context if the point is inside the painted region when the path is stroked or filled with opaque colors using the specified path drawing mode. A point can be inside a path only if the path is explicitly closed by calling the function
CGContextClosePath, for paths drawn directly to the current context, orCGPathCloseSubpath, for paths first created asCGPathobjects and then drawn to the current context.Availability
Available in iOS 2.0 and later.
-
Modifies the current clipping path, using the nonzero winding number rule.
Declaration
Swift
func CGContextClip(_c: CGContext?)Objective-C
void CGContextClip ( CGContextRef c );Parameters
cA graphics context that contains a path. If the context does not have a current path, the function does nothing.
Discussion
The function uses the nonzero winding number rule to calculate the intersection of the current path with the current clipping path. Quartz then uses the path resulting from the intersection as the new current clipping path for subsequent painting operations.
If the current path includes any open subpaths, Quartz treats them as if they were closed by calling
CGContextClosePath.Unlike the current path, the current clipping path is part of the graphics state. Therefore, to re-enlarge the paintable area by restoring the clipping path to a prior state, you must save the graphics state before you clip and restore the graphics state after you’ve completed any clipped drawing.
After determining the new clipping path, the function resets the context’s current path to an empty path.
Availability
Available in iOS 2.0 and later.
See Also
-
Modifies the current clipping path, using the even-odd rule.
Declaration
Swift
func CGContextEOClip(_c: CGContext?)Objective-C
void CGContextEOClip ( CGContextRef c );Parameters
cA graphics context containing a path. If the context does not have a current path, the function does nothing.
Discussion
The function uses the even-odd rule to calculate the intersection of the current path with the current clipping path. Quartz then uses the path resulting from the intersection as the new current clipping path for subsequent painting operations.
If the current path includes any open subpaths, Quartz treats them as if they were closed by calling
CGContextClosePath.Unlike the current path, the current clipping path is part of the graphics state. Therefore, to re-enlarge the paintable area by restoring the clipping path to a prior state, you must save the graphics state before you clip and restore the graphics state after you’ve completed any clipped drawing.
After determining the new clipping path, the function resets the context’s current path to an empty path.
Availability
Available in iOS 2.0 and later.
See Also
-
Sets the clipping path to the intersection of the current clipping path with the area defined by the specified rectangle.
Declaration
Swift
func CGContextClipToRect(_c: CGContext?, _rect: CGRect)Objective-C
void CGContextClipToRect ( CGContextRef c, CGRect rect );Parameters
cThe graphics context for which to set the clipping path.
rectA
CGRectvalue that specifies, in the user space, the location and dimensions of the rectangle to be used in determining the new clipping path.Discussion
This function sets the specified graphics context’s clipping region to the area which intersects both the current clipping path and the specified rectangle.
After determining the new clipping path, the
CGContextClipToRectfunction resets the context’s current path to an empty path.Availability
Available in iOS 2.0 and later.
See Also
-
Sets the clipping path to the intersection of the current clipping path with the region defined by an array of rectangles.
Declaration
Swift
func CGContextClipToRects(_c: CGContext?, _rects: UnsafePointer<CGRect>, _count: Int)Objective-C
void CGContextClipToRects ( CGContextRef c, const CGRect *rects, size_t count );Parameters
cThe graphics context for which to set the clipping path.
rectsAn array of rectangles. The locations and dimensions of the rectangles are specified in the user space coordinate system.
countThe total number of array entries in the
rectsparameter.Discussion
This function sets the clipping path to the intersection of the current clipping path and the region within the specified rectangles.
After determining the new clipping path, the function resets the context’s current path to an empty path.
Availability
Available in iOS 2.0 and later.
See Also
-
Returns the bounding box of a clipping path.
Declaration
Swift
func CGContextGetClipBoundingBox(_c: CGContext?) -> CGRectObjective-C
CGRect CGContextGetClipBoundingBox ( CGContextRef c );Parameters
cThe graphics context to modify.
Return Value
The bounding box of the clipping path, specified in user space.
Discussion
The bounding box is the smallest rectangle completely enclosing all points in the clipping path, including control points for any Bezier curves in the path.
Availability
Available in iOS 2.0 and later.
-
Maps a mask into the specified rectangle and intersects it with the current clipping area of the graphics context.
Declaration
Swift
func CGContextClipToMask(_c: CGContext?, _rect: CGRect, _mask: CGImage?)Objective-C
void CGContextClipToMask ( CGContextRef c, CGRect rect, CGImageRef mask );Parameters
cA graphics context.
rectThe rectangle to map the
maskparameter to.maskAn image or an image mask. If
maskis an image, then it must be in the DeviceGray color space, may not have an alpha component, and may not be masked by an image mask or masking color.Discussion
If the
maskparameter is an image mask, then Quartz clips in a manner identical to the behavior seen with the functionCGContextDrawImage—the mask indicates an area to be left unchanged when drawing. The source samples of the image mask determine which points of the clipping area are changed, acting as an "inverse alpha" value. If the value of a source sample in the image mask isS, then the corresponding point in the current clipping area is multiplied by an alpha value of (1–S). For example, ifSis1then the point in the clipping area becomes transparent. IfSis0, the point in the clipping area is unchanged.If the
maskparameter is an image, thenmaskacts like an alpha mask and is blended with the current clipping area. The source samples of mask determine which points of the clipping area are changed. If the value of the source sample in mask isS, then the corresponding point in the current clipping area is multiplied by an alpha ofS. For example, ifSis0, then the point in the clipping area becomes transparent. IfSis1, the point in the clipping area is unchanged.Availability
Available in iOS 2.0 and later.
-
Sets the opacity level for objects drawn in a graphics context.
Declaration
Swift
func CGContextSetAlpha(_c: CGContext?, _alpha: CGFloat)Objective-C
void CGContextSetAlpha ( CGContextRef c, CGFloat alpha );Parameters
cThe graphics context for which to set the current graphics state’s alpha value parameter.
alphaA value that specifies the opacity level. Values can range from
0.0(transparent) to1.0(opaque). Values outside this range are clipped to0.0or1.0.Discussion
This function sets the alpha value parameter for the specified graphics context. To clear the contents of the drawing canvas, use
CGContextClearRect.Availability
Available in iOS 2.0 and later.
-
Sets the current fill color to a value in the DeviceCMYK color space.
Declaration
Swift
func CGContextSetCMYKFillColor(_c: CGContext?, _cyan: CGFloat, _magenta: CGFloat, _yellow: CGFloat, _black: CGFloat, _alpha: CGFloat)Parameters
cThe graphics context for which to set the current fill color.
cyanThe cyan intensity value for the color to set. The DeviceCMYK color space permits the specification of a value ranging from 0.0 (does not absorb the secondary color) to 1.0 (fully absorbs the secondary color).
magentaThe magenta intensity value for the color to set. The DeviceCMYK color space permits the specification of a value ranging from 0.0 (does not absorb the secondary color) to 1.0 (fully absorbs the secondary color).
yellowThe yellow intensity value for the color to set. The DeviceCMYK color space permits the specification of a value ranging from 0.0 (does not absorb the secondary color) to 1.0 (fully absorbs the secondary color).
blackThe black intensity value for the color to set. The DeviceCMYK color space permits the specification of a value ranging from 0.0 (does not absorb the secondary color) to 1.0 (fully absorbs the secondary color).
alphaA value that specifies the opacity level. Values can range from
0.0(transparent) to1.0(opaque). Values outside this range are clipped to0.0or1.0.Discussion
Quartz provides convenience functions for each of the device color spaces that allow you to set the fill or stroke color space and the fill or stroke color with one function call.
When you call this function, two things happen:
Quartz sets the current fill color space to DeviceCMYK.
Quartz sets the current fill color to the value specified by the
cyan,magenta,yellow,black, andalphaparameters.
Availability
Available in iOS 2.0 and later.
See Also
-
Sets the current fill color.
Declaration
Swift
func CGContextSetFillColor(_c: CGContext?, _components: UnsafePointer<CGFloat>)Objective-C
void CGContextSetFillColor ( CGContextRef c, const CGFloat *components );Parameters
cThe graphics context for which to set the current fill color.
componentsAn array of intensity values describing the color to set. The number of array elements must equal the number of components in the current fill color space, plus an additional component for the alpha value.
Discussion
The current fill color space must not be a pattern color space. For information on setting the fill color when using a pattern color space, see
CGContextSetFillPattern. Note that the preferred API to use is nowCGContextSetFillColorWithColor.Availability
Available in iOS 2.0 and later.
-
Sets the current stroke color to a value in the DeviceCMYK color space.
Declaration
Swift
func CGContextSetCMYKStrokeColor(_c: CGContext?, _cyan: CGFloat, _magenta: CGFloat, _yellow: CGFloat, _black: CGFloat, _alpha: CGFloat)Parameters
cThe graphics context for which to set the current stroke color.
cyanThe cyan intensity value for the color to set. The DeviceCMYK color space permits the specification of a value ranging from
0.0(does not absorb the secondary color) to1.0(fully absorbs the secondary color).magentaThe magenta intensity value for the color to set. The DeviceCMYK color space permits the specification of a value ranging from
0.0(does not absorb the secondary color) to1.0(fully absorbs the secondary color).yellowThe yellow intensity value for the color to set. The DeviceCMYK color space permits the specification of a value ranging from
0.0(does not absorb the secondary color) to1.0(fully absorbs the secondary color).blackThe black intensity value for the color to set. The DeviceCMYK color space permits the specification of a value ranging from
0.0(does not absorb the secondary color) to1.0(fully absorbs the secondary color).alphaA value that specifies the opacity level. Values can range from
0.0(transparent) to1.0(opaque). Values outside this range are clipped to0.0or1.0.Discussion
When you call this function, two things happen:
Quartz sets the current stroke color space to DeviceCMYK.
Quartz sets the current stroke color to the value specified by the
cyan,magenta,yellow,black, andalphaparameters.
Availability
Available in iOS 2.0 and later.
See Also
-
Sets the fill color space in a graphics context.
Declaration
Swift
func CGContextSetFillColorSpace(_c: CGContext?, _space: CGColorSpace?)Objective-C
void CGContextSetFillColorSpace ( CGContextRef c, CGColorSpaceRef space );Parameters
cThe graphics context for which to set the fill color space.
colorspaceThe new fill color space. Quartz retains this object; upon return, you may safely release it.
Discussion
As a side effect of this function, Quartz assigns an appropriate initial value to the fill color, based on the specified color space. To change this value, call
CGContextSetFillColor. Note that the preferred API to use is nowCGContextSetFillColorWithColor.Availability
Available in iOS 2.0 and later.
-
Sets the current fill color in a graphics context, using a Quartz color.
Declaration
Swift
func CGContextSetFillColorWithColor(_c: CGContext?, _color: CGColor?)Objective-C
void CGContextSetFillColorWithColor ( CGContextRef c, CGColorRef color );Parameters
cThe graphics context for which to set the fill color.
colorThe new fill color.
Availability
Available in iOS 2.0 and later.
See Also
-
Sets the current fill color to a value in the DeviceGray color space.
Declaration
Swift
func CGContextSetGrayFillColor(_c: CGContext?, _gray: CGFloat, _alpha: CGFloat)Objective-C
void CGContextSetGrayFillColor ( CGContextRef c, CGFloat gray, CGFloat alpha );Parameters
cThe graphics context for which to set the current fill color.
grayA value that specifies the desired gray level. The DeviceGray color space permits the specification of a value ranging from
0.0(absolute black) to1.0(absolute white). Values outside this range are clamped to0.0or1.0.alphaA value that specifies the opacity level. Values can range from
0.0(transparent) to1.0(opaque). Values outside this range are clipped to0.0or1.0.Discussion
When you call this function, two things happen:
Quartz sets the current fill color space to DeviceGray.
Quartz sets the current fill color to the value you specify in the
grayandalphaparameters.
Availability
Available in iOS 2.0 and later.
See Also
-
Sets the current stroke color to a value in the DeviceGray color space.
Declaration
Swift
func CGContextSetGrayStrokeColor(_c: CGContext?, _gray: CGFloat, _alpha: CGFloat)Objective-C
void CGContextSetGrayStrokeColor ( CGContextRef c, CGFloat gray, CGFloat alpha );Parameters
cThe graphics context for which to set the current stroke color.
grayA value that specifies the desired gray level. The DeviceGray color space permits the specification of a value ranging from
0.0(absolute black) to1.0(absolute white). Values outside this range are clamped to0.0or1.0.alphaA value that specifies the opacity level. Values can range from
0.0(transparent) to1.0(opaque). Values outside this range are clipped to0.0or1.0.Discussion
When you call this function, two things happen:
Quartz sets the current stroke color space to DeviceGray. The DeviceGray color space is a single-dimension space in which color values are specified solely by the intensity of a gray value (from absolute black to absolute white).
Quartz sets the current stroke color to the value you specify in the
grayandalphaparameters.
Availability
Available in iOS 2.0 and later.
See Also
-
Sets the current fill color to a value in the DeviceRGB color space.
Declaration
Swift
func CGContextSetRGBFillColor(_c: CGContext?, _red: CGFloat, _green: CGFloat, _blue: CGFloat, _alpha: CGFloat)Objective-C
void CGContextSetRGBFillColor ( CGContextRef c, CGFloat red, CGFloat green, CGFloat blue, CGFloat alpha );Parameters
cThe graphics context for which to set the current fill color.
redThe red intensity value for the color to set. The DeviceRGB color space permits the specification of a value ranging from
0.0(zero intensity) to1.0(full intensity).greenThe green intensity value for the color to set. The DeviceRGB color space permits the specification of a value ranging from
0.0(zero intensity) to1.0(full intensity).blueThe blue intensity value for the color to set. The DeviceRGB color space permits the specification of a value ranging from
0.0(zero intensity) to1.0(full intensity).alphaA value that specifies the opacity level. Values can range from
0.0(transparent) to1.0(opaque). Values outside this range are clipped to0.0or1.0.Discussion
When you call this function, two things happen:
Quartz sets the current fill color space to DeviceRGB.
Quartz sets the current fill color to the value specified by the
red,green,blue, andalphaparameters.
Availability
Available in iOS 2.0 and later.
See Also
-
Sets the current stroke color to a value in the DeviceRGB color space.
Declaration
Swift
func CGContextSetRGBStrokeColor(_c: CGContext?, _red: CGFloat, _green: CGFloat, _blue: CGFloat, _alpha: CGFloat)Objective-C
void CGContextSetRGBStrokeColor ( CGContextRef c, CGFloat red, CGFloat green, CGFloat blue, CGFloat alpha );Parameters
cThe graphics context for which to set the current stroke color.
redThe red intensity value for the color to set. The DeviceRGB color space permits the specification of a value ranging from
0.0(zero intensity) to1.0(full intensity).greenThe green intensity value for the color to set. The DeviceRGB color space permits the specification of a value ranging from
0.0(zero intensity) to1.0(full intensity).blueThe blue intensity value for the color to set. The DeviceRGB color space permits the specification of a value ranging from
0.0(zero intensity) to1.0(full intensity).alphaA value that specifies the opacity level. Values can range from
0.0(transparent) to1.0(opaque). Values outside this range are clipped to0.0or1.0.Discussion
When you call this function, two things happen:
Quartz sets the current stroke color space to DeviceRGB.
Quartz sets the current stroke color to the value specified by the
red,green,blue, andalphaparameters.
Availability
Available in iOS 2.0 and later.
See Also
-
Enables shadowing in a graphics context.
Declaration
Swift
func CGContextSetShadow(_c: CGContext?, _offset: CGSize, _blur: CGFloat)Objective-C
void CGContextSetShadow ( CGContextRef c, CGSize offset, CGFloat blur );Parameters
contextA graphics context.
offsetSpecifies a translation of the context’s coordinate system, to establish an offset for the shadow (
{0,0}specifies a light source immediately above the screen).blurA non-negative number specifying the amount of blur.
Discussion
Shadow parameters are part of the graphics state in a context. After shadowing is set, all objects drawn are shadowed using a black color with 1/3 alpha (i.e., RGBA =
{0, 0, 0, 1.0/3.0}) in the DeviceRGB color space.To turn off shadowing:
Use the standard save/restore mechanism for the graphics state.
Use
CGContextSetShadowWithColorto set the shadow color to a fully transparent color (or passNULLas the color).
Availability
Available in iOS 2.0 and later.
-
Enables shadowing with color a graphics context.
Declaration
Swift
func CGContextSetShadowWithColor(_c: CGContext?, _offset: CGSize, _blur: CGFloat, _color: CGColor?)Objective-C
void CGContextSetShadowWithColor ( CGContextRef c, CGSize offset, CGFloat blur, CGColorRef color );Parameters
contextThe graphics context to modify.
offsetSpecifies a translation in base-space.
blurA non-negative number specifying the amount of blur.
colorSpecifies the color of the shadow, which may contain a non-opaque alpha value. If
NULL, then shadowing is disabled.Availability
Available in iOS 2.0 and later.
See Also
-
Sets the current stroke color.
Declaration
Swift
func CGContextSetStrokeColor(_c: CGContext?, _components: UnsafePointer<CGFloat>)Objective-C
void CGContextSetStrokeColor ( CGContextRef c, const CGFloat *components );Parameters
cThe graphics context for which to set the current stroke color.
componentsAn array of intensity values describing the color to set. The number of array elements must equal the number of components in the current stroke color space, plus an additional component for the alpha value.
Discussion
The current stroke color space must not be a pattern color space. For information on setting the stroke color when using a pattern color space, see
CGContextSetStrokePattern. Note that the preferred API is nowCGContextSetStrokeColorWithColor.Availability
Available in iOS 2.0 and later.
-
Sets the stroke color space in a graphics context.
Declaration
Swift
func CGContextSetStrokeColorSpace(_c: CGContext?, _space: CGColorSpace?)Objective-C
void CGContextSetStrokeColorSpace ( CGContextRef c, CGColorSpaceRef space );Parameters
cThe graphics context for the new stroke color space.
colorspaceThe new stroke color space. Quartz retains this object; upon return, you may safely release it.
Discussion
As a side effect when you call this function, Quartz assigns an appropriate initial value to the stroke color, based on the color space you specify. To change this value, call
CGContextSetStrokeColor. Note that the preferred API is nowCGContextSetStrokeColorWithColor.Availability
Available in iOS 2.0 and later.
-
Sets the current stroke color in a context, using a Quartz color.
Declaration
Swift
func CGContextSetStrokeColorWithColor(_c: CGContext?, _color: CGColor?)Objective-C
void CGContextSetStrokeColorWithColor ( CGContextRef c, CGColorRef color );Parameters
cThe graphics context to modify.
colorThe new stroke color.
Availability
Available in iOS 2.0 and later.
See Also
These functions allow you to examine and change the current transformation matrix (CTM) in a graphics context.
-
Transforms the user coordinate system in a context using a specified matrix.
Declaration
Swift
func CGContextConcatCTM(_c: CGContext?, _transform: CGAffineTransform)Objective-C
void CGContextConcatCTM ( CGContextRef c, CGAffineTransform transform );Parameters
cA graphics context.
transformThe transformation matrix to apply to the specified context’s current transformation matrix.
Discussion
When you call the function
CGContextConcatCTM, it concatenates (that is, it combines) two matrices, by multiplying them together. The order in which matrices are concatenated is important, as the operations are not commutative. When you callCGContextConcatCTM, the resulting CTM in the context is:CTMnew = transform * CTMcontext.Availability
Available in iOS 2.0 and later.
-
Returns the current transformation matrix.
Declaration
Swift
func CGContextGetCTM(_c: CGContext?) -> CGAffineTransformObjective-C
CGAffineTransform CGContextGetCTM ( CGContextRef c );Parameters
cA graphics context.
Return Value
The transformation matrix for the current graphics state of the specified context.
Availability
Available in iOS 2.0 and later.
-
Rotates the user coordinate system in a context.
Declaration
Swift
func CGContextRotateCTM(_c: CGContext?, _angle: CGFloat)Objective-C
void CGContextRotateCTM ( CGContextRef c, CGFloat angle );Parameters
cA graphics context.
angleThe angle, in radians, by which to rotate the coordinate space of the specified context. Positive values rotate counterclockwise and negative values rotate clockwise.)
Discussion
The direction that the context is rotated may appear to be altered by the state of the current transformation matrix prior to executing this function. For example, on iOS, a
UIViewapplies a transformation to the graphics context that inverts the Y-axis (by multiplying it by-1). Rotating the user coordinate system on coordinate system that was previously flipped results in a rotation in the opposite direction (that is, positive values appear to rotate the coordinate system in the clockwise direction).Availability
Available in iOS 2.0 and later.
-
Changes the scale of the user coordinate system in a context.
Declaration
Swift
func CGContextScaleCTM(_c: CGContext?, _sx: CGFloat, _sy: CGFloat)Objective-C
void CGContextScaleCTM ( CGContextRef c, CGFloat sx, CGFloat sy );Parameters
cA graphics context.
sxThe factor by which to scale the x-axis of the coordinate space of the specified context.
syThe factor by which to scale the y-axis of the coordinate space of the specified context.
Availability
Available in iOS 2.0 and later.
-
Changes the origin of the user coordinate system in a context.
Declaration
Swift
func CGContextTranslateCTM(_c: CGContext?, _tx: CGFloat, _ty: CGFloat)Objective-C
void CGContextTranslateCTM ( CGContextRef c, CGFloat tx, CGFloat ty );Parameters
cA graphics context.
txThe amount to displace the x-axis of the coordinate space, in units of the user space, of the specified context.
tyThe amount to displace the y-axis of the coordinate space, in units of the user space, of the specified context.
Availability
Available in iOS 2.0 and later.
-
Begins a transparency layer.
Declaration
Swift
func CGContextBeginTransparencyLayer(_c: CGContext?, _auxiliaryInfo: CFDictionary?)Objective-C
void CGContextBeginTransparencyLayer ( CGContextRef c, CFDictionaryRef auxiliaryInfo );Parameters
contextA graphics context.
auxiliaryInfoA dictionary that specifies any additional information, or
NULL.Discussion
Until a corresponding call to
CGContextEndTransparencyLayer, all subsequent drawing operations in the specified context are composited into a fully transparent backdrop (which is treated as a separate destination buffer from the context).After a call to
CGContextEndTransparencyLayer, the result is composited into the context using the global alpha and shadow state of the context. This operation respects the clipping region of the context.After a call to this function, all of the parameters in the graphics state remain unchanged with the exception of the following:
The global alpha is set to
1.The shadow is turned off.
Ending the transparency layer restores these parameters to their previous values. Quartz maintains a transparency layer stack for each context, and transparency layers may be nested.
Availability
Available in iOS 2.0 and later.
-
Begins a transparency layer whose contents are bounded by the specified rectangle.
Declaration
Swift
func CGContextBeginTransparencyLayerWithRect(_c: CGContext?, _rect: CGRect, _auxInfo: CFDictionary?)Objective-C
void CGContextBeginTransparencyLayerWithRect ( CGContextRef c, CGRect rect, CFDictionaryRef auxInfo );Parameters
contextA graphics context.
rectThe rectangle, specified in user space, that bounds the transparency layer.
auxiliaryInfoA dictionary that specifies any additional information, or
NULL.Discussion
This function is identical to
CGContextBeginTransparencyLayerexcept that the content of the transparency layer is within the bounds of the provided rectangle.Availability
Available in iOS 2.0 and later.
-
Ends a transparency layer.
Declaration
Swift
func CGContextEndTransparencyLayer(_c: CGContext?)Objective-C
void CGContextEndTransparencyLayer ( CGContextRef c );Parameters
contextA graphics context.
Availability
Available in iOS 2.0 and later.
See Also
-
Repeatedly draws an image, scaled to the provided rectangle, to fill the current clip region.
Declaration
Swift
func CGContextDrawTiledImage(_c: CGContext?, _rect: CGRect, _image: CGImage?)Objective-C
void CGContextDrawTiledImage ( CGContextRef c, CGRect rect, CGImageRef image );Parameters
contextThe graphics context in which to draw the image.
rectA rectangle that specifies the origin and size of the destination tile. Quartz scales the image—disproportionately, if necessary—to fit the bounds specified by the
rectparameter.imageThe image to draw.
Discussion
Quartz draws the scaled image starting at the origin of the rectangle in user space, then moves to a new point (horizontally by the width of the tile and/or vertically by the height of the tile), draws the scaled image, moves again, draws again, and so on, until the current clip region is tiled with copies of the image. Unlike patterns, the image is tiled in user space, so transformations applied to the CTM affect the final result.
Availability
Available in iOS 2.0 and later.
-
Draws an image into a graphics context.
Declaration
Swift
func CGContextDrawImage(_c: CGContext?, _rect: CGRect, _image: CGImage?)Objective-C
void CGContextDrawImage ( CGContextRef c, CGRect rect, CGImageRef image );Parameters
cThe graphics context in which to draw the image.
rectThe location and dimensions in user space of the bounding box in which to draw the image.
imageThe image to draw.
Discussion
Quartz scales the image—disproportionately, if necessary—to fit the bounds specified by the
rectparameter.Availability
Available in iOS 2.0 and later.
-
Draws a page in the current user space of a PDF context.
Declaration
Swift
func CGContextDrawPDFPage(_c: CGContext?, _page: CGPDFPage?)Objective-C
void CGContextDrawPDFPage ( CGContextRef c, CGPDFPageRef page );Parameters
cThe graphics context in which to draw the PDF page.
pageA Quartz PDF page.
Discussion
This function works in conjunction with the opaque type
CGPDFPageRefto draw individual pages into a PDF context.Availability
Available in iOS 2.0 and later.
-
Paints a gradient fill that varies along the line defined by the provided starting and ending points.
Declaration
Swift
func CGContextDrawLinearGradient(_c: CGContext?, _gradient: CGGradient?, _startPoint: CGPoint, _endPoint: CGPoint, _options: CGGradientDrawingOptions)Objective-C
void CGContextDrawLinearGradient ( CGContextRef c, CGGradientRef gradient, CGPoint startPoint, CGPoint endPoint, CGGradientDrawingOptions options );Parameters
contextA Quartz graphics context.
gradientA
CGGradientobject.startPointThe coordinate that defines the starting point of the gradient.
endPointThe coordinate that defines the ending point of the gradient.
optionsOption flags (
kCGGradientDrawsBeforeStartLocationorkCGGradientDrawsAfterEndLocation) that control whether the fill is extended beyond the starting or ending point.Discussion
The color at location 0 in the CGGradient object is mapped to the starting point. The color at location 1 in the CGGradient object is mapped to the ending point. Colors are linearly interpolated between these two points based on the location values of the gradient. The option flags control whether the gradient is drawn before the start point or after the end point.
Availability
Available in iOS 2.0 and later.
-
Paints a gradient fill that varies along the area defined by the provided starting and ending circles.
Declaration
Swift
func CGContextDrawRadialGradient(_c: CGContext?, _gradient: CGGradient?, _startCenter: CGPoint, _startRadius: CGFloat, _endCenter: CGPoint, _endRadius: CGFloat, _options: CGGradientDrawingOptions)Objective-C
void CGContextDrawRadialGradient ( CGContextRef c, CGGradientRef gradient, CGPoint startCenter, CGFloat startRadius, CGPoint endCenter, CGFloat endRadius, CGGradientDrawingOptions options );Parameters
contextA Quartz graphics context.
gradientA CGGradient object.
startCenterThe coordinate that defines the center of the starting circle.
startRadiusThe radius of the starting circle.
endCenterThe coordinate that defines the center of the ending circle.
endRadiusThe radius of the ending circle.
optionsOption flags (
kCGGradientDrawsBeforeStartLocationorkCGGradientDrawsAfterEndLocation) that control whether the gradient is drawn before the starting circle or after the ending circle.Discussion
The color at location 0 in the CGGradient object is mapped to the circle defined by
startCenterandstartRadius. The color at location 1 in the CGGradient object is mapped to the circle defined byendCenterandendRadius. Colors are linearly interpolated between the starting and ending circles based on the location values of the gradient. The option flags control whether the gradient is drawn before the start point or after the end point.Availability
Available in iOS 2.0 and later.
-
Fills the clipping path of a context with the specified shading.
Declaration
Swift
func CGContextDrawShading(_c: CGContext?, _shading: CGShading?)Objective-C
void CGContextDrawShading ( CGContextRef c, CGShadingRef shading );Parameters
cThe graphics context in which to draw the shading.
shadingA Quartz shading. Quartz retains this object; upon return, you may safely release it.
Discussion
In OS X v10.5 and later, the preferred way to draw gradients is to use a CGGradient object. See CGGradient Reference.
Availability
Available in iOS 2.0 and later.
-
Starts a new page in a page-based graphics context.
Declaration
Swift
func CGContextBeginPage(_c: CGContext?, _mediaBox: UnsafePointer<CGRect>)Objective-C
void CGContextBeginPage ( CGContextRef c, const CGRect *mediaBox );Parameters
cA page-based graphics context such as a PDF context. If you specify a context that does not support multiple pages, this function does nothing.
mediaBoxA Quartz rectangle defining the bounds of the new page, expressed in units of the default user space, or
NULL. These bounds supersede any supplied for the media box when you created the context. If you passNULL, Quartz uses the rectangle you supplied for the media box when the graphics context was created.Discussion
When using a graphics context that supports multiple pages, you should call this function together with
CGContextEndPageto delineate the page boundaries in the output. In other words, each page should be bracketed by calls toCGContextBeginPageandCGContextEndPage. Quartz ignores all drawing operations performed outside a page boundary in a page-based context.Availability
Available in iOS 2.0 and later.
-
Ends the current page in a page-based graphics context.
Declaration
Swift
func CGContextEndPage(_c: CGContext?)Objective-C
void CGContextEndPage ( CGContextRef c );Parameters
cA page-based graphics context.
Discussion
When using a graphics context that supports multiple pages, you should call this function to terminate drawing in the current page.
Availability
Available in iOS 2.0 and later.
See Also
-
CGContextShowGlyphs(iOS 7.0)Displays an array of glyphs at the current text position.
Deprecation Statement
Use Core Text instead.
Declaration
Objective-C
void CGContextShowGlyphs ( CGContextRef c, const CGGlyph *g, size_t count );Parameters
cThe graphics context in which to display the glyphs.
glyphsAn array of glyphs to display.
countThe total number of glyphs passed in the
gparameter.Discussion
This function displays an array of glyphs at the current text position, a point specified by the current text matrix.
Availability
Available in iOS 2.0 and later.
Deprecated in iOS 7.0.
-
CGContextShowGlyphsAtPoint(iOS 7.0)Displays an array of glyphs at a position you specify.
Deprecation Statement
Use Core Text instead.
Declaration
Objective-C
void CGContextShowGlyphsAtPoint ( CGContextRef c, CGFloat x, CGFloat y, const CGGlyph *glyphs, size_t count );Parameters
cThe graphics context in which to display the glyphs.
xA value for the x-coordinate of the user space at which to display the glyphs.
yA value for the y-coordinate of the user space at which to display the glyphs.
glyphsAn array of glyphs to display.
countThe total number of glyphs passed in the
glyphsparameter.Discussion
This function displays an array of glyphs at the specified position in the user space.
Availability
Available in iOS 2.0 and later.
Deprecated in iOS 7.0.
-
CGContextShowGlyphsWithAdvances(iOS 7.0)Draws an array of glyphs with varying offsets.
Deprecation Statement
Use Core Text instead.
Declaration
Objective-C
void CGContextShowGlyphsWithAdvances ( CGContextRef c, const CGGlyph *glyphs, const CGSize *advances, size_t count );Parameters
cThe graphics context in which to display the glyphs.
glyphsAn array of Quartz glyphs.
advancesAn array of offset values associated with each glyph in the array. Each value specifies the offset from the previous glyph's origin to the origin of the corresponding glyph. Offsets are specified in user space.
countThe number of glyphs in the specified array.
Discussion
This function draws an array of glyphs at the current point specified by the text matrix.
Availability
Available in iOS 2.0 and later.
Deprecated in iOS 7.0.
-
Draws glyphs at the provided position.
Declaration
Swift
func CGContextShowGlyphsAtPositions(_c: CGContext?, _glyphs: UnsafePointer<CGGlyph>, _Lpositions: UnsafePointer<CGPoint>, _count: Int)Objective-C
void CGContextShowGlyphsAtPositions ( CGContextRef c, const CGGlyph *glyphs, const CGPoint *Lpositions, size_t count );Parameters
cThe graphics context in which to display the glyphs.
glyphsAn array of Quartz glyphs.
positionsThe positions for the glyphs. Each item in this array matches with the glyph at the corresponding index in the
glyphsarray. The position of each glyph is specified in text space, and, as a consequence, is transformed through the text matrix to user space.countThe number of items in the
glyphsarray.Availability
Available in iOS 2.0 and later.
-
Returns the current text matrix.
Declaration
Swift
func CGContextGetTextMatrix(_c: CGContext?) -> CGAffineTransformObjective-C
CGAffineTransform CGContextGetTextMatrix ( CGContextRef c );Parameters
cThe graphics context for which to obtain the text matrix.
Return Value
The current text matrix.
Availability
Available in iOS 2.0 and later.
-
Returns the location at which text is drawn.
Declaration
Swift
func CGContextGetTextPosition(_c: CGContext?) -> CGPointObjective-C
CGPoint CGContextGetTextPosition ( CGContextRef c );Parameters
cThe graphics context from which to obtain the current text position.
Return Value
Returns a
CGPointvalue that specifies the x and y values at which text is to be drawn, in user space coordinates.Availability
Available in iOS 2.0 and later.
-
CGContextSelectFont(iOS 7.0)Sets the font and font size in a graphics context.
Deprecation Statement
Use Core Text instead.
Declaration
Objective-C
void CGContextSelectFont ( CGContextRef c, const char *name, CGFloat size, CGTextEncoding textEncoding );Parameters
cThe graphics context for which to set the font and font size.
nameA null-terminated string that contains the PostScript name of the font to set.
sizeA value that specifies the font size to set, in text space units.
textEncodingA
CGTextEncodingvalue that specifies the encoding used for the font. For a description of the available values, see“CGTextEncoding”.Availability
Available in iOS 2.0 and later.
Deprecated in iOS 7.0.
-
Sets the current character spacing.
Declaration
Swift
func CGContextSetCharacterSpacing(_c: CGContext?, _spacing: CGFloat)Objective-C
void CGContextSetCharacterSpacing ( CGContextRef c, CGFloat spacing );Parameters
cThe graphics context for which to set the character spacing.
spacingA value that represents the amount of additional space to place between glyphs, in text space coordinates.
Discussion
Quartz adds the additional space to the advance between the origin of one character and the origin of the next character. For information about the text coordinate system, see
CGContextSetTextMatrix.Availability
Available in iOS 2.0 and later.
-
Sets the platform font in a graphics context.
Declaration
Swift
func CGContextSetFont(_c: CGContext?, _font: CGFont?)Objective-C
void CGContextSetFont ( CGContextRef c, CGFontRef font );Parameters
cThe graphics context for which to set the font.
fontA Quartz font.
Availability
Available in iOS 2.0 and later.
-
Sets the current font size.
Declaration
Swift
func CGContextSetFontSize(_c: CGContext?, _size: CGFloat)Objective-C
void CGContextSetFontSize ( CGContextRef c, CGFloat size );Parameters
cA graphics context.
sizeA font size, expressed in text space units.
Availability
Available in iOS 2.0 and later.
-
Sets the current text drawing mode.
Declaration
Swift
func CGContextSetTextDrawingMode(_c: CGContext?, _mode: CGTextDrawingMode)Objective-C
void CGContextSetTextDrawingMode ( CGContextRef c, CGTextDrawingMode mode );Parameters
cA graphics context.
modeA text drawing mode (such as
kCGTextFillorkCGTextStroke) that specifies how Quartz renders individual glyphs in a graphics context. See“CGTextDrawingMode”for a complete list.Availability
Available in iOS 2.0 and later.
-
Sets the current text matrix.
Declaration
Swift
func CGContextSetTextMatrix(_c: CGContext?, _t: CGAffineTransform)Objective-C
void CGContextSetTextMatrix ( CGContextRef c, CGAffineTransform t );Parameters
cA graphics context.
transformThe text matrix to set.
Discussion
The text matrix specifies the transform from text space to user space. To produce the final text rendering matrix that is used to actually draw the text on the page, Quartz concatenates the text matrix with the current transformation matrix and other parameters from the graphics state.
Note that the text matrix is not a part of the graphics state—saving or restoring the graphics state has no effect on the text matrix. The text matrix is an attribute of the graphics context, not of the current font.
Availability
Available in iOS 2.0 and later.
-
Sets the location at which text is drawn.
Declaration
Swift
func CGContextSetTextPosition(_c: CGContext?, _x: CGFloat, _y: CGFloat)Objective-C
void CGContextSetTextPosition ( CGContextRef c, CGFloat x, CGFloat y );Parameters
cA graphics context.
xA value for the x-coordinate at which to draw the text, in user space coordinates.
yA value for the y-coordinate at which to draw the text, in user space coordinates.
Availability
Available in iOS 2.0 and later.
-
CGContextShowText(iOS 7.0)Displays a character array at the current text position, a point specified by the current text matrix.
Deprecation Statement
Use Core Text instead.
Declaration
Objective-C
void CGContextShowText ( CGContextRef c, const char *string, size_t length );Parameters
cA graphics context.
stringAn array of characters to draw.
lengthThe length of the array specified in the
bytesparameter.Discussion
Quartz uses font data provided by the system to map each byte of the array through the encoding vector of the current font to obtain the glyph to display. Note that the font must have been set using
CGContextSelectFont. Don’t useCGContextShowTextin conjunction withCGContextSetFont.Availability
Available in iOS 2.0 and later.
Deprecated in iOS 7.0.
-
CGContextShowTextAtPoint(iOS 7.0)Displays a character string at a position you specify.
Deprecation Statement
Use Core Text instead.
Declaration
Objective-C
void CGContextShowTextAtPoint ( CGContextRef c, CGFloat x, CGFloat y, const char *string, size_t length );Parameters
cA graphics context .
xA value for the x-coordinate (in user space) at which to display the text.
yA value for the y-coordinate (in user space) at which to display the text.
stringAn array of characters to draw.
lengthThe length of the array specified in the
stringparameter.Discussion
Quartz uses font data provided by the system to map each byte of the array through the encoding vector of the current font to obtain the glyph to display. Note that the font must have been set using
CGContextSelectFont. Don’t useCGContextShowTextAtPointin conjunction withCGContextSetFont.Availability
Available in iOS 2.0 and later.
Deprecated in iOS 7.0.
-
Returns an affine transform that maps user space coordinates to device space coordinates.
Declaration
Swift
func CGContextGetUserSpaceToDeviceSpaceTransform(_c: CGContext?) -> CGAffineTransformObjective-C
CGAffineTransform CGContextGetUserSpaceToDeviceSpaceTransform ( CGContextRef c );Parameters
cA graphics context.
Return Value
The affine transforms that maps the user space of the graphics context to the device space.
Availability
Available in iOS 2.0 and later.
-
Returns a point that is transformed from user space coordinates to device space coordinates.
Declaration
Objective-C
CGPoint CGContextConvertPointToDeviceSpace ( CGContextRef c, CGPoint point );Parameters
cA graphics context.
pointThe point, in user space coordinates, to transform.
Return Value
The coordinates of the point in device space coordinates.
Availability
Available in iOS 2.0 and later.
See Also
-
Returns a point that is transformed from device space coordinates to user space coordinates.
Declaration
Objective-C
CGPoint CGContextConvertPointToUserSpace ( CGContextRef c, CGPoint point );Parameters
cA graphics context.
pointThe point, in device space coordinates, to transform.
Return Value
The coordinates of the point in user space coordinates.
Availability
Available in iOS 2.0 and later.
See Also
-
Returns a size that is transformed from user space coordinates to device space coordinates.
Declaration
Objective-C
CGSize CGContextConvertSizeToDeviceSpace ( CGContextRef c, CGSize size );Parameters
cA graphics context.
sizeThe size, in user space coordinates, to transform.
Return Value
The size in device space coordinates.
Availability
Available in iOS 2.0 and later.
See Also
-
Returns a size that is transformed from device space coordinates to user space coordinates
Declaration
Objective-C
CGSize CGContextConvertSizeToUserSpace ( CGContextRef c, CGSize size );Parameters
cA graphics context.
sizeThe size, in device space coordinates, to transform.
Return Value
The size in user space coordinates.
Availability
Available in iOS 2.0 and later.
See Also
-
Returns a rectangle that is transformed from user space coordinate to device space coordinates.
Declaration
Objective-C
CGRect CGContextConvertRectToDeviceSpace ( CGContextRef c, CGRect rect );Parameters
cA graphics context.
rectThe rectangle, in user space coordinates, to transform.
Return Value
The rectangle in device space coordinates.
Discussion
In general affine transforms do not preserve rectangles. As a result, this function returns the smallest rectangle that contains the transformed corner points of the rectangle.
Availability
Available in iOS 2.0 and later.
See Also
-
Returns a rectangle that is transformed from device space coordinate to user space coordinates.
Declaration
Objective-C
CGRect CGContextConvertRectToUserSpace ( CGContextRef c, CGRect rect );Parameters
cA graphics context.
rectThe rectangle, in device space coordinates, to transform.
Return Value
The rectangle in user space coordinates.
Discussion
In general, affine transforms do not preserve rectangles. As a result, this function returns the smallest rectangle that contains the transformed corner points of the rectangle.
Availability
Available in iOS 2.0 and later.
See Also
-
An opaque type that represents a Quartz 2D drawing environment.
Declaration
Swift
typealias CGContextRef = CGContextObjective-C
typedef struct CGContext * CGContextRef;Import Statement
Objective-C
@import CoreGraphics;Swift
import CoreGraphicsAvailability
Available in iOS 2.0 and later.
-
Compositing operations for images.
Declaration
Swift
enum CGBlendMode : Int32 { case Normal case Multiply case Screen case Overlay case Darken case Lighten case ColorDodge case ColorBurn case SoftLight case HardLight case Difference case Exclusion case Hue case Saturation case Color case Luminosity case Clear case Copy case SourceIn case SourceOut case SourceAtop case DestinationOver case DestinationIn case DestinationOut case DestinationAtop case XOR case PlusDarker case PlusLighter }Objective-C
enum CGBlendMode { kCGBlendModeNormal, kCGBlendModeMultiply, kCGBlendModeScreen, kCGBlendModeOverlay, kCGBlendModeDarken, kCGBlendModeLighten, kCGBlendModeColorDodge, kCGBlendModeColorBurn, kCGBlendModeSoftLight, kCGBlendModeHardLight, kCGBlendModeDifference, kCGBlendModeExclusion, kCGBlendModeHue, kCGBlendModeSaturation, kCGBlendModeColor, kCGBlendModeLuminosity, kCGBlendModeClear, kCGBlendModeCopy, kCGBlendModeSourceIn, kCGBlendModeSourceOut, kCGBlendModeSourceAtop, kCGBlendModeDestinationOver, kCGBlendModeDestinationIn, kCGBlendModeDestinationOut, kCGBlendModeDestinationAtop, kCGBlendModeXOR, kCGBlendModePlusDarker, kCGBlendModePlusLighter }; typedef enum CGBlendMode CGBlendMode;Constants
-
NormalkCGBlendModeNormalPaints the source image samples over the background image samples.
Available in iOS 2.0 and later.
-
MultiplykCGBlendModeMultiplyMultiplies the source image samples with the background image samples. This results in colors that are at least as dark as either of the two contributing sample colors.
Available in iOS 2.0 and later.
-
ScreenkCGBlendModeScreenMultiplies the inverse of the source image samples with the inverse of the background image samples. This results in colors that are at least as light as either of the two contributing sample colors.
Available in iOS 2.0 and later.
-
OverlaykCGBlendModeOverlayEither multiplies or screens the source image samples with the background image samples, depending on the background color. The result is to overlay the existing image samples while preserving the highlights and shadows of the background. The background color mixes with the source image to reflect the lightness or darkness of the background.
Available in iOS 2.0 and later.
-
DarkenkCGBlendModeDarkenCreates the composite image samples by choosing the darker samples (either from the source image or the background). The result is that the background image samples are replaced by any source image samples that are darker. Otherwise, the background image samples are left unchanged.
Available in iOS 2.0 and later.
-
LightenkCGBlendModeLightenCreates the composite image samples by choosing the lighter samples (either from the source image or the background). The result is that the background image samples are replaced by any source image samples that are lighter. Otherwise, the background image samples are left unchanged.
Available in iOS 2.0 and later.
-
ColorDodgekCGBlendModeColorDodgeBrightens the background image samples to reflect the source image samples. Source image sample values that specify black do not produce a change.
Available in iOS 2.0 and later.
-
ColorBurnkCGBlendModeColorBurnDarkens the background image samples to reflect the source image samples. Source image sample values that specify white do not produce a change.
Available in iOS 2.0 and later.
-
SoftLightkCGBlendModeSoftLightEither darkens or lightens colors, depending on the source image sample color. If the source image sample color is lighter than 50% gray, the background is lightened, similar to dodging. If the source image sample color is darker than 50% gray, the background is darkened, similar to burning. If the source image sample color is equal to 50% gray, the background is not changed. Image samples that are equal to pure black or pure white produce darker or lighter areas, but do not result in pure black or white. The overall effect is similar to what you’d achieve by shining a diffuse spotlight on the source image. Use this to add highlights to a scene.
Available in iOS 2.0 and later.
-
HardLightkCGBlendModeHardLightEither multiplies or screens colors, depending on the source image sample color. If the source image sample color is lighter than 50% gray, the background is lightened, similar to screening. If the source image sample color is darker than 50% gray, the background is darkened, similar to multiplying. If the source image sample color is equal to 50% gray, the source image is not changed. Image samples that are equal to pure black or pure white result in pure black or white. The overall effect is similar to what you’d achieve by shining a harsh spotlight on the source image. Use this to add highlights to a scene.
Available in iOS 2.0 and later.
-
DifferencekCGBlendModeDifferenceSubtracts either the source image sample color from the background image sample color, or the reverse, depending on which sample has the greater brightness value. Source image sample values that are black produce no change; white inverts the background color values.
Available in iOS 2.0 and later.
-
ExclusionkCGBlendModeExclusionProduces an effect similar to that produced by
kCGBlendModeDifference, but with lower contrast. Source image sample values that are black don’t produce a change; white inverts the background color values.Available in iOS 2.0 and later.
-
HuekCGBlendModeHueUses the luminance and saturation values of the background with the hue of the source image.
Available in iOS 2.0 and later.
-
SaturationkCGBlendModeSaturationUses the luminance and hue values of the background with the saturation of the source image. Areas of the background that have no saturation (that is, pure gray areas) don’t produce a change.
Available in iOS 2.0 and later.
-
ColorkCGBlendModeColorUses the luminance values of the background with the hue and saturation values of the source image. This mode preserves the gray levels in the image. You can use this mode to color monochrome images or to tint color images.
Available in iOS 2.0 and later.
-
LuminositykCGBlendModeLuminosityUses the hue and saturation of the background with the luminance of the source image. This mode creates an effect that is inverse to the effect created by
kCGBlendModeColor.Available in iOS 2.0 and later.
-
ClearkCGBlendModeClearR = 0Available in iOS 2.0 and later.
-
CopykCGBlendModeCopyR = SAvailable in iOS 2.0 and later.
-
SourceInkCGBlendModeSourceInR = S*DaAvailable in iOS 2.0 and later.
-
SourceOutkCGBlendModeSourceOutR = S*(1 - Da)Available in iOS 2.0 and later.
-
SourceAtopkCGBlendModeSourceAtopR = S*Da + D*(1 - Sa)Available in iOS 2.0 and later.
-
DestinationOverkCGBlendModeDestinationOverR = S*(1 - Da) + DAvailable in iOS 2.0 and later.
-
DestinationInkCGBlendModeDestinationInR = D*SaAvailable in iOS 2.0 and later.
-
DestinationOutkCGBlendModeDestinationOutR = D*(1 - Sa)Available in iOS 2.0 and later.
-
DestinationAtopkCGBlendModeDestinationAtopR = S*(1 - Da) + D*SaAvailable in iOS 2.0 and later.
-
XORkCGBlendModeXORR = S*(1 - Da) + D*(1 - Sa). This XOR mode is only nominally related to the classical bitmap XOR operation, which is not supported by Quartz 2D.Available in iOS 2.0 and later.
-
PlusDarkerkCGBlendModePlusDarkerR = MAX(0, 1 - ((1 - D) + (1 - S)))Available in iOS 2.0 and later.
-
PlusLighterkCGBlendModePlusLighterR = MIN(1, S + D)Available in iOS 2.0 and later.
Discussion
The blend mode constants introduced in OS X v10.5 represent the Porter-Duff blend modes. The symbols in the equations for these blend modes are:
R is the premultiplied result
S is the source color, and includes alpha
D is the destination color, and includes alpha
Ra, Sa, and Da are the alpha components of R, S, and D
You can find more information on blend modes, including examples of images produced using them, and many mathematical descriptions of the modes, in PDF Reference, Fourth Edition, Version 1.5, Adobe Systems, Inc. If you are a former QuickDraw developer, it may be helpful for you to think of blend modes as an alternative to transfer modes
For examples of using blend modes see "Setting Blend Modes" and "Using Blend Modes With Images" in Quartz 2D Programming Guide.
Import Statement
Objective-C
@import CoreGraphics;Swift
import CoreGraphicsAvailability
Available in iOS 2.0 and later.
-
-
Levels of interpolation quality for rendering an image.
Declaration
Swift
enum CGInterpolationQuality : Int32 { case Default case None case Low case Medium case High }Objective-C
enum CGInterpolationQuality { kCGInterpolationDefault = 0, kCGInterpolationNone = 1, kCGInterpolationLow = 2, kCGInterpolationMedium = 4, kCGInterpolationHigh = 3 }; typedef enum CGInterpolationQuality CGInterpolationQuality;Constants
-
DefaultkCGInterpolationDefaultThe default level of quality.
Available in iOS 2.0 and later.
-
NonekCGInterpolationNoneNo interpolation.
Available in iOS 2.0 and later.
-
LowkCGInterpolationLowA low level of interpolation quality. This setting may speed up image rendering.
Available in iOS 2.0 and later.
-
MediumkCGInterpolationMediumA medium level of interpolation quality. This setting is slower than the low setting but faster than the high setting.
Available in iOS 4.0 and later.
-
HighkCGInterpolationHighA high level of interpolation quality. This setting may slow down image rendering.
Available in iOS 2.0 and later.
Discussion
You use the function
CGContextSetInterpolationQualityto set the interpolation quality in a graphics context.Import Statement
Objective-C
@import CoreGraphics;Swift
import CoreGraphicsAvailability
Available in iOS 2.0 and later.
-
-
Modes for rendering text.
Declaration
Swift
enum CGTextDrawingMode : Int32 { case Fill case Stroke case FillStroke case Invisible case FillClip case StrokeClip case FillStrokeClip case Clip }Objective-C
enum CGTextDrawingMode { kCGTextFill, kCGTextStroke, kCGTextFillStroke, kCGTextInvisible, kCGTextFillClip, kCGTextStrokeClip, kCGTextFillStrokeClip, kCGTextClip }; typedef enum CGTextDrawingMode CGTextDrawingMode;Constants
-
FillkCGTextFillPerform a fill operation on the text.
Available in iOS 2.0 and later.
-
StrokekCGTextStrokePerform a stroke operation on the text.
Available in iOS 2.0 and later.
-
FillStrokekCGTextFillStrokePerform fill, then stroke operations on the text.
Available in iOS 2.0 and later.
-
InvisiblekCGTextInvisibleDo not draw the text, but do update the text position.
Available in iOS 2.0 and later.
-
FillClipkCGTextFillClipPerform a fill operation, then intersect the text with the current clipping path.
Available in iOS 2.0 and later.
-
StrokeClipkCGTextStrokeClipPerform a stroke operation, then intersect the text with the current clipping path.
Available in iOS 2.0 and later.
-
FillStrokeClipkCGTextFillStrokeClipPerform fill then stroke operations, then intersect the text with the current clipping path.
Available in iOS 2.0 and later.
-
ClipkCGTextClipSpecifies to intersect the text with the current clipping path. This mode does not paint the text.
Available in iOS 2.0 and later.
Discussion
You provide a text drawing mode constant to the function
CGContextSetTextDrawingModeto set the current text drawing mode for a graphics context. Text drawing modes determine how Quartz renders individual glyphs onscreen. For example, you can set a text drawing mode to draw text filled in or outlined (stroked) or both. You can also create special effects with the text clipping drawing modes, such as clipping an image to a glyph shape.Import Statement
Objective-C
@import CoreGraphics;Swift
import CoreGraphicsAvailability
Available in iOS 2.0 and later.
-
-
Text encodings for fonts.
Declaration
Objective-C
enum CGTextEncoding { kCGEncodingFontSpecific, kCGEncodingMacRoman }; typedef enum CGTextEncoding CGTextEncoding;Constants
-
kCGEncodingFontSpecificThe built-in encoding of the font.
Available in iOS 2.0 and later.
Deprecated in iOS 7.0.
-
kCGEncodingMacRomanThe MacRoman encoding. MacRoman is an ASCII variant originally created for use in the Mac OS, in which characters 127 and lower are ASCII, and characters 128 and higher are non-English characters and symbols.
Available in iOS 2.0 and later.
Deprecated in iOS 7.0.
Discussion
For more information on setting the font in a graphics context, see
CGContextSelectFont.Import Statement
Objective-C
@import CoreGraphics;Availability
Available in iOS 2.0 and later.
-
Copyright © 2015 Apple Inc. All rights reserved. Terms of Use | Privacy Policy | Updated: 2013-09-18
