Search code examples
textcolorsborderios7stroke

Why CGContextSetRGBStrokeColor isn't working on ios7?


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();

Solution

  • 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];