2013-04-09 62 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