tags:

views:

1696

answers:

2

I'd like to make a glowing text that looks like if it was shining or emmitting light. Actually this would be done with some blurryness in the background. But I've found nothing in the iPhone SDK that would do it.

+3  A: 

How about rendering the text into an initially transparent buffer using the desired glow colour, then apply a blur to it, and then render the text again over the top in the desired text colour.

Then you can draw the buffer wherever it needs to be.

See also the answers to "How do I create blurred text in an iPhone view?" which would suggest Cocoa Touch lacks any built in blur filters. You can write your own of course.

Paul Dixon
Yes that's a good idea. I thought maybe there's a library that does exactly this.
HelloMoon
+8  A: 

You can do this using pure Quartz drawing. Within -drawRect: for a UIView or -renderInContext: for a CALayer, the following code will draw text and apply a glow around it:

CGContextSetShadowWithColor( context, CGSizeMake( 0.0, 0.0 ), 20.0f, glowColor );   

[text drawAtPoint:CGPointMake(0.5f, 0.5f) withFont:[UIFont fontWithName:@"Helvetica" size:16.0f]];

where text is an NSString, and glowColor is a CGColorRef for the glow you want to apply. This example will draw a glow that extends out 20 pixels from the 16-point Helvetica text, and is centered on the text.

You can easily convert this into a shadow by providing a different displacement (second argument in the first function) and by using a black CGColor for the shadow.

Brad Larson
Sounds good! That's pretty much the way I would try in a vector graphics program. So when I get that right, my text should not be too long because the glow is basically a scaled version of the text, where the most central pixels are above eachother and the most left / right ones differ mostly from their clones underneeth, right?
HelloMoon
btw: could this technique also be used to apply glow on any content of an view? afaik a UIView has an cached image as content where an effect could be applied to make everythig appear blurry...i'd say some kind of gaussian algorithm is needed for that. but that's probably a different question.
HelloMoon
This should apply a glow to the outline of whatever Quartz vector content you draw after the CGContextSetShadowWithColor() call. For image content, it will probably just do a glow around the borders of the image. To blur content, you'd probably need to draw it as vectors, but leave the stroke color as a completely clear color. That way, only the shadow would show.
Brad Larson
what is the context variable? how to set it ?
Shivan Raptor
context is the current drawing context, where all drawing operations output their results. Within the -drawRect: method of UIView you can obtain the current context using code like `CGContextRef context = UIGraphicsGetCurrentContext();` For CALayers, -renderInContext: passes in the drawing context to use.
Brad Larson