我想在核心图形中产生铬文字效果,我有一些麻烦让它看起来不错。铬文字效果与线性渐变给锯齿边缘
这是我想达到的效果是:
这是我设法得到:
正如你所看到的梯度边缘相当锯齿状。有没有办法达到这种效果而不诉诸静态PNG纹理填充?
这是我的代码:
- (void)setGradientFill:(UILabel*)label
{
CGSize textSize = [label.text sizeWithFont:label.font];
CGFloat width = textSize.width; // max 1024 due to Core Graphics limitations
CGFloat height = textSize.height; // max 1024 due to Core Graphics limitations
// create a new bitmap image context
UIGraphicsBeginImageContextWithOptions(CGSizeMake(width, height), YES, 0.0);
// get context
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetAllowsAntialiasing(context, true);
CGContextSetShouldAntialias(context, true);
// push context to make it current (need to do this manually because we are not drawing in a UIView)
UIGraphicsPushContext(context);
CGContextSetAllowsAntialiasing(context, YES);
CGContextSetShouldAntialias(context, YES);
//draw gradient
CGGradientRef glossGradient;
CGColorSpaceRef rgbColorspace;
size_t num_locations = 5;
CGFloat locations[5] = { 0.0, 0.5, 0.5, 0.65, 1};
CGFloat components[20] = {
1.0, 1.0, 1.0, 1.0,
1.0, 1.0, 1.0, 1.0,
0.878, 0.878, 0.878, 0.878,
0.78, 0.78, 0.78, 1.0,
1, 1, 1, 1.0
};
rgbColorspace = CGColorSpaceCreateDeviceRGB();
glossGradient = CGGradientCreateWithColorComponents(rgbColorspace, components, locations, num_locations);
CGPoint start = CGPointMake(0, 0);
CGPoint end = CGPointMake(width/4, height*1.2);
CGContextDrawLinearGradient(context, glossGradient, start, end, kCGGradientDrawsAfterEndLocation);
CGGradientRelease(glossGradient);
CGColorSpaceRelease(rgbColorspace);
// pop context
UIGraphicsPopContext();
// get a UIImage from the image context
UIImage *gradientImage = UIGraphicsGetImageFromCurrentImageContext();
// clean up drawing environment
UIGraphicsEndImageContext();
label.textColor = [UIColor colorWithPatternImage:gradientImage];
}
编辑的建议下lefteris:
我试过(下面的左图)所做的扩展。出于某种原因,我得到了一些模糊的文字,出现了边缘的人为因素。
我的UIImage的帧大小的图像,以避免内容压腿:
_test.frame = CGRectMake(_test.frame.origin.x, _test.frame.origin.y, image.size.width, image.size.height);
_test.image = image;
编辑最近的解决方案:
解决方案通过lefteris是最接近的,你可以”不过真的可以避免锯齿状的渐变边缘。如果你可以使用Photoshop中生成的静态png作为纹理(而不是文本只是填充),但你可能需要几个版本(像我一样)在每个屏幕上获得正确的效果,因为根据大小应用纹理框架不是包含的文本。动态地做它是可能的,但看起来有限。
你不需要调用'UIGraphicsPushContext'来使它成为最新的。看看你从哪里得到它:它已经*当前。 –
事实上,如果没有这个,它就无法正常工作。 – Imran
忘记删除相应的流行音乐,所以它不工作,但你是对的,它不需要。 – Imran