为什么CGContextSetRGBStrokeColor在iOS7上无法工作?

3

我在 iOS7 上遇到了文本描边的问题,虽然在 iOS4、iOS5 和 iOS6 上都能正常工作。自从我将设备升级至 iOS7 以后,我就看不到描边颜色了。有人知道为什么吗?

以下是我的代码:

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();
2个回答

3
我刚遇到了这个问题。在iOS 7上,似乎CGContextSetRGBFillColor用于设置描边颜色,而非CGContextSetRGBStrokeColor。看起来像是一个错误。这意味着无法使用CGContextSetTextDrawingMode(context, kCGTextFillStroke);因为描边颜色将与填充颜色相同。
我修改了你的代码,通过添加第二个drawInRect调用来解决这个问题。由于它只是额外再描一下边缘,所以这个解决方案也向后兼容,并且如果他们修复了该错误,也应该向前兼容。
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();

2

注意到iOS7同样存在明显的倒退,因为它废弃了使用字体API进行drawInRect操作。使用推荐的'withAttributes'参数可以解决问题。

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

[myString drawAtPoint:myPosition withAttributes:dictionary];

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接