Question

I have a problem to make a text stroke to work on iOS7...Everything worked well for iOS4 iOS5 ans iOS6 but since I updated my devices for iOS7, I can't see the stroke color. Does anybody have an idea of how this can be possible ?

Here is my code :

UIGraphicsBeginImageContext(CGSizeMake(scale(line.position.width), scale(line.position.height)));

CGContextRef context = UIGraphicsGetCurrentContext();

CGContextSetLineWidth (context, scale(4.0)); //4.0

CGContextSetLineJoin(context, kCGLineJoinRound);
CGContextSetBlendMode(context, kCGBlendModeScreen);
CGContextSetTextDrawingMode(context, kCGTextFillStroke);
CGContextSetRGBFillColor(context, 1.0, 1.0, 1.0, 1.0);
CGContextSetRGBStrokeColor(context, 0.0, 0.0, 0.0, 1.0);
[label.text drawInRect:label.frame withFont:label.font];

self.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Was it helpful?

Solution 2

Noticed the same apparent regression with iOS7, which deprecates drawInRect withFont API. Using the recommended 'withAttributes' flavor works.

NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:largeFont,NSFontAttributeName, [UIColor whiteColor],NSForegroundColorAttributeName,[UIColor blackColor], NSStrokeColorAttributeName,nil];

[myString drawAtPoint:myPosition withAttributes:dictionary];

OTHER TIPS

I just ran into this. On iOS 7 it seems that CGContextSetRGBFillColor is used in place of CGContextSetRGBStrokeColor for setting the stroke color. Looks like a bug. This means that there is no way to use CGContextSetTextDrawingMode(context, kCGTextFillStroke); because the stroke color will be the same as the fill color.

I modified your code to work by adding a second drawInRect call. This solution is also backward compatible since it just re-strokes the stroke an extra time and should also be forward compatible if they fix the bug:

UIGraphicsBeginImageContext(CGSizeMake(scale(line.position.width), scale(line.position.height)));

CGContextRef context = UIGraphicsGetCurrentContext();

CGContextSetLineWidth (context, scale(4.0)); //4.0

CGContextSetLineJoin(context, kCGLineJoinRound);
CGContextSetBlendMode(context, kCGBlendModeScreen);
CGContextSetTextDrawingMode(context, kCGTextFillStroke);
CGContextSetRGBFillColor(context, 1.0, 1.0, 1.0, 1.0);
CGContextSetRGBStrokeColor(context, 0.0, 0.0, 0.0, 1.0);
[label.text drawInRect:label.frame withFont:label.font];


//New Code Start
CGContextSetTextDrawingMode(context, kCGTextStroke);
CGContextSetRGBFillColor(context, 0.0, 0.0, 0.0, 1.0);
[label.text drawInRect:label.frame withFont:label.font];
//New Code End

self.image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top