iOS学习笔记iOS开发点滴Swift学习笔记

UILabel文字颜色渐变

2023-04-23  本文已影响0人  香橙柚子

网络整合的两种方案,经验证有效:
效果图如下:


效果图

方法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);
    
    // 创建渐变层
    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:
先创建一个基于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};
   
    // 画文字(不做显示用 主要作用是设置layer的mask)
    CGContextRef context = UIGraphicsGetCurrentContext();
    [self.textColor set];
    [self.text drawWithRect:rect options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : self.font} context:NULL];
    
    // 坐标 (只对设置后的画到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);// 清除之前画的文字
   
     // 设置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);
    
    // 释放内存
    CGColorSpaceRelease(colorSpace);
    CGGradientRelease(gradient);
    CFRelease(alphaMask);

}

@end

调用方法:

- (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];
}

效果图如上图。

上一篇 下一篇

猜你喜欢

热点阅读