【问题标题】:Why CGContextSetRGBStrokeColor isn't working on ios7?为什么 CGContextSetRGBStrokeColor 在 ios7 上不起作用?
【发布时间】:2013-09-24 13:13:54
【问题描述】:

我无法在 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();

【问题讨论】:

    标签: text colors border ios7 stroke


    【解决方案1】:

    我刚碰到这个。在 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 相同的明显回归,它弃用了 drawInRect withFont API。使用推荐的 'withAttributes' 风格很有效。

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

      【讨论】:

        猜你喜欢
        • 2013-10-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-12-02
        • 1970-01-01
        • 1970-01-01
        • 2016-03-22
        • 1970-01-01
        相关资源
        最近更新 更多