【发布时间】: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