【问题标题】:CGContextSetShadowWithColor puts shadows on top of the text casting the shadowCGContextSetShadowWithColor 将阴影放在投射阴影的文本之上
【发布时间】:2015-05-24 10:01:46
【问题描述】:

我正在使用CGContextSetShadowWithColor 和核心图形来绘制阴影文本。阴影出现了,但它似乎也“混淆”了正在投射阴影的实际文本本身(应该是纯白色的)。就好像它在文本顶部投射阴影(但不完全)。

像这样:

如果我在相同位置重绘文本并关闭阴影,我可以用干净的白色文本覆盖泥泞的文本,所以这是一种解决方法,但我想知道:

是我做错了什么,还是这是一个错误?

    let shadowOffset : CGSize = CGSize (width: 4, height: 4)

    UIGraphicsBeginImageContextWithOptions(CGSize(width: 800, height: 200), false, 1.0)
    let ctx = UIGraphicsGetCurrentContext()

    CGContextTranslateCTM(ctx, 0, 200);
    CGContextScaleCTM(ctx, 1.0, -1.0);

    CGContextSetAlpha(ctx, 1.0)
    CGContextSetShadowWithColor(ctx, shadowOffset, 5, UIColor(red: 0, green: 0, blue: 0, alpha: 0.5).CGColor)

    CGContextSetAllowsAntialiasing (ctx, true)

    let attr:CFDictionaryRef = [
        NSFontAttributeName : UIFont(name: fontName, size: fontSize)!,
        NSForegroundColorAttributeName:UIColor.whiteColor()]

    let line = CTLineCreateWithAttributedString(CFAttributedStringCreate(nil, "1234567890", attr))

    let bounds = CTLineGetBoundsWithOptions(line, CTLineBoundsOptions.UseOpticalBounds)

    CGContextSetLineWidth(ctx, 1)
    CGContextSetTextDrawingMode(ctx, kCGTextFillStroke)

    CGContextSetTextPosition(ctx, 100.0, 100.0)
    CTLineDraw(line, ctx)

    //Uncomment to clean-up text
    //CGContextSetShadowWithColor(ctx, shadowOffset, 0, nil)
    //CGContextSetTextPosition(ctx, 100.0, 100.0)
    //CTLineDraw(line, ctx)
    let image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    return image

【问题讨论】:

    标签: ios text core-graphics shadow


    【解决方案1】:

    调用CGContextSetTextDrawingMode时,设置绘图模式为kCGTextFill。据我了解,您看到的阴影是由文本的笔划投射的。

    【讨论】:

    • 就是这样。我的描边和填充颜色是一样的,所以没关系。尝试了不同的颜色,但似乎还是不行。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-20
    • 1970-01-01
    • 2016-06-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多