UILabel文字顏色漸變

網(wǎng)絡(luò)整合的兩種方案,經(jīng)驗證有效:
效果圖如下:


效果圖

方法1:

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    NSArray *colors = @[(id)[UIColor redColor].CGColor, (id)[UIColor yellowColor].CGColor];
    
    [self functionGradientLayerWithColors:colors];
}

- (void)functionGradientLayerWithColors:(NSArray *)colors{
    
    UILabel* lbl = [[UILabel alloc] init];
    lbl.text = @"我是有漸變色的Label,快來看啊";
    lbl.font = [UIFont systemFontOfSize:23];
    [lbl sizeToFit];
    
    [self.view addSubview:lbl];
    lbl.center = CGPointMake(self.view.bounds.size.width * 0.5, self.view.bounds.size.height * 0.3);
    
    // 創(chuàng)建漸變層
    CAGradientLayer* gradientLayer = [CAGradientLayer layer];
    gradientLayer.frame = lbl.frame;
    gradientLayer.colors = colors;
    gradientLayer.startPoint = CGPointMake(0, 1);
    gradientLayer.endPoint = CGPointMake(1, 1);
    [self.view.layer addSublayer:gradientLayer];
    
    gradientLayer.mask = lbl.layer;
    lbl.frame = gradientLayer.bounds;
    
}

方法2:
先創(chuàng)建一個基于UILabel的類:JJGradientLabel
JJGradientLabel.h

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface JJGradientLabel : UILabel

@property(nonatomic, strong) NSArray* colors;

@end

JJGradientLabel.m


#import "JJGradientLabel.h"

@implementation JJGradientLabel


- (void)drawRect:(CGRect)rect
{
    CGSize textSize = [self.text sizeWithAttributes:@{NSFontAttributeName : self.font}];
    CGRect textRect = (CGRect){0, 0, textSize};
   
    // 畫文字(不做顯示用 主要作用是設(shè)置layer的mask)
    CGContextRef context = UIGraphicsGetCurrentContext();
    [self.textColor set];
    [self.text drawWithRect:rect options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : self.font} context:NULL];
    
    // 坐標(biāo) (只對設(shè)置后的畫到context起作用 之前畫的文字不起作用)
    CGContextTranslateCTM(context, 0.0f, rect.size.height- (rect.size.height - textSize.height)*0.5);
    CGContextScaleCTM(context, 1.0f, -1.0f);
    
    CGImageRef alphaMask = NULL;
    alphaMask = CGBitmapContextCreateImage(context);
    CGContextClearRect(context, rect);// 清除之前畫的文字
   
     // 設(shè)置mask
    CGContextClipToMask(context, rect, alphaMask);
    
    // 畫漸變色

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef)self.colors, NULL);
    CGPoint startPoint = CGPointMake(textRect.origin.x,
                                     textRect.origin.y);
    CGPoint endPoint = CGPointMake(textRect.origin.x + textRect.size.width,
                                   textRect.origin.y + textRect.size.height);
    CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, kCGGradientDrawsBeforeStartLocation | kCGGradientDrawsAfterEndLocation);
    
    // 釋放內(nèi)存
    CGColorSpaceRelease(colorSpace);
    CGGradientRelease(gradient);
    CFRelease(alphaMask);

}

@end

調(diào)用方法:

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
        
    NSArray *colors = @[(id)[UIColor redColor].CGColor, (id)[UIColor yellowColor].CGColor];
    
    [self functionGradientLabelWithColors:colors];
}


- (void)functionGradientLabelWithColors:(NSArray *)colors{
    
    JJGradientLabel* lbl = [[JJGradientLabel alloc] init];
    lbl.text = @"我是漸變色的呀呀呀呀--label";
    lbl.font = [UIFont systemFontOfSize:23];
    [lbl sizeToFit];
    lbl.colors = colors;
    
    [self.view addSubview:lbl];
    lbl.center = CGPointMake(self.view.bounds.size.width * 0.5, self.view.bounds.size.height * 0.4);
    
}

效果圖如上圖。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容