2015-05-24 193 views
1

我使用CGContextSetShadowWithColor和核心圖形繪製陰影文本的頂部陰影。陰影出現了,但它似乎也「渾濁」了鑄造陰影的實際文本本身(應該是純白色的)。就好像它在文本上面投射陰影(但不完全)。CGContextSetShadowWithColor把上的文字投射陰影

像這樣:

Muddy text created with 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 

回答

2

當調用到CGContextSetTextDrawingMode,繪製模式設置爲kCGTextFill。就我的理解而言,你看到的影子是由文字的中風鑄造的。

+0

就是這樣。我的筆觸和填充是相同的顏色,所以沒關係。嘗試了不同的顏色,它似乎並沒有工作。 – rghome