kiran
kiran

Reputation: 4409

How To Set UIColor Value To CGContextSetRGBStrokeColor

How to set UIColor value into CGContextSetRGBStrokeColor

// This is red color which i set to the CGContextSetRGBStrokeColor CGContextSetRGBStrokeColor(contextRef, 1, 0, 0,1.00f);

Now i do have UIColor which has RGB value i need to set this value to CGContextSetRGBStrokeColor.

Any one suggest me how to set UIColor value's CGContextSetRGBStrokeColor

Upvotes: 7

Views: 5545

Answers (4)

nabu
nabu

Reputation: 572

In swift 3.0

let context = UIGraphicsGetCurrentContext()
context!.setStrokeColor(red: 1,green: 1,blue: 1,alpha: 1)

Or you can ser color as below

context!.setStrokeColor(UIColor.red.cgColor)//or any UIColor

Upvotes: 0

Matthieu Riegler
Matthieu Riegler

Reputation: 54953

For the lazy :

let myUIColor = UIColor.purpleColor()
var r:CGFloat, g:CGFloat, b:CGFloat, a:CGFloat = 0
myUIColor.getRed(&r, green: &g, blue: &b, alpha: &a)
CGContextSetRGBStrokeColor(c, r, g, b, a)

// or 
CGContextSetStrokeColorWithColor(myUIColor.CGColor);

#SwiftStyle

Upvotes: 1

Roger
Roger

Reputation: 7612

Alternative:

CGContextSetStrokeColorWithColor(context, [[UIColor greenColor] CGColor]);

// or
[[UIColor colorWithRed:1.0f green:0.0f blue:0.0f alpha:1.0f] CGColor];

Upvotes: 14

Jody Hagins
Jody Hagins

Reputation: 28409

CGFloat red, green, blue, alpha;
[color getRed:&red green:&green blue:&blue alpha:&alpha];
CGContextSetRGBStrokeColor(contextRef, red, green, blue, alpha);

Upvotes: 9

Related Questions