关于文字:为什么CGContext tRG StrokeColor无法在ios 7上运行?

Why CGContextSetRGBStrokeColor isn't working on ios7?

我在使文本笔划在iOS7上工作时遇到问题...在iOS4 iOS5和iOS6上一切正常,但是由于我将设备更新为iOS7,所以看不到笔划颜色。有谁知道这怎么可能?

这是我的代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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();

我刚遇到这个。在iOS 7上,似乎可以使用CGContextSetRGBFillColor代替CGContextSetRGBStrokeColor来设置笔触颜色。看起来像个虫子。这意味着无法使用CGContextSetTextDrawingMode(context,kCGTextFillStroke);因为描边颜色将与填充颜色相同。

我通过添加第二个drawInRect调用来修改您的代码以使其正常工作。此解决方案也是向后兼容的,因为它只是将笔划重新冲程了额外的时间,并且如果他们修复了错误,也应该是向前兼容的:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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();

使用iOS7对相同的表观回归进行了注释,iOS7不赞成使用FontAPI绘制drawInRect。使用推荐的\\'withAttributes \\'风格有效。

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

[myString drawAtPoint:myPosition withAttributes:dictionary];