2013-04-09 135 views
2

所以我想根据Photoshop中的渐变设置UILabel的文字颜色。我具有渐变的{rgb值,{211,119,95}和{199,86,56}。这可能吗?我该怎么做?基于自定义线性渐变的UILabel文字颜色

+0

“基于渐变”是什么意思? – Bernat 2013-04-09 14:39:01

+0

你可以使用coretext并自己绘制文本。 – yunas 2013-04-09 14:40:56

+0

所以我的字体颜色是在Photoshop中使用渐变制作的,所以我使用这两组值作为用于获取特定颜色的rgb值。 – Carmichael 2013-04-09 14:41:31

回答

2

您可能需要使用这些定制的标签之一:

+0

嗯,我以为已经在使用它了,除此之外,还有其他什么? – Carmichael 2013-04-09 14:52:13

+1

是的,看看这个答案:http://stackoverflow.com/a/1391723/550177 – Felix 2013-04-09 14:56:50

+0

非常感谢! – Carmichael 2013-04-10 11:07:58

12

另一种方式,如果你想定位到iOS 6+,与类别到UIColor

您创建一个UIColor从梯度:

[attrString addAttribute:NSForegroundColorAttributeName value:[UIColor gradientFromColor:[UIColor greenColor] toColor:[UIColor redColor] withHeight:labelView.height] range:defaultRange]; 
labelView.attributedString = attrString; 

或简单地将文字颜色,如果你不也需要中风或其他造型效果

labelView.textColor = [UIColor gradientFromColor:[UIColor greenColor] toColor:[UIColor redColor] withHeight:labelView.height]; 

和:

+ (UIColor*) gradientFromColor:(UIColor*)c1 toColor:(UIColor*)c2 withHeight:(int)height 
{ 
    CGSize size = CGSizeMake(1, height); 
    UIGraphicsBeginImageContextWithOptions(size, NO, 0); 
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB(); 

    NSArray* colors = [NSArray arrayWithObjects:(id)c1.CGColor, (id)c2.CGColor, nil]; 
    CGGradientRef gradient = CGGradientCreateWithColors(colorspace, (CFArrayRef)colors, NULL); 
    CGContextDrawLinearGradient(context, gradient, CGPointMake(0, 0), CGPointMake(0, size.height), 0); 

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 

    CGGradientRelease(gradient); 
    CGColorSpaceRelease(colorspace); 
    UIGraphicsEndImageContext(); 

    return [UIColor colorWithPatternImage:image]; 
} 
与attrString为您NSMutableAttributeString

然后瞧,它在UILabel上的效果更好,否则你必须从你的字体(UIFont.leading)计算你的行高并将其传递给方法,背景将垂直重复。

+0

真的很酷....易于使用.... – milanpanchal 2013-12-18 06:31:47