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

2 回答 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();
于 2013-10-20T20:27:22.147 回答
2

注意到与 iOS7 相同的明显回归,它弃用了 drawInRect withFont API。使用推荐的“withAttributes”风味有效。

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

[myString drawAtPoint:myPosition withAttributes:dictionary];
于 2013-10-02T19:50:13.737 回答