文字渐变

2017-06-01  本文已影响8人  ghost__

文章参考:http://www.jianshu.com/p/fe06704e11a0

方式一

自定义label

.h
#import <UIKit/UIKit.h>

@interface YHGradientLabel : UILabel

@property (nonatomic, strong) NSArray<UIColor *> *colors;
@property (nonatomic, strong) NSArray<NSNumber *> *locations;
@end

.m
#import "YHGradientLabel.h"
#import <CoreImage/CoreImage.h>

@implementation YHGradientLabel

- (void)drawRect:(CGRect)rect {
    //计算文字size
    CGSize textSize = [self.text sizeWithAttributes:@{NSFontAttributeName : self.font}];
    
    //计算文字绘制位置
    CGRect inRect = CGRectMake(0, (rect.size.height - textSize.height) / 2, textSize.width, textSize.height);
    switch (self.textAlignment) {
        case NSTextAlignmentLeft:
            break;
        case NSTextAlignmentCenter:
            inRect = CGRectMake((rect.size.width - textSize.width) / 2, (rect.size.height - textSize.height) / 2, textSize.width, textSize.height);
            break;
        case NSTextAlignmentRight:
            inRect = CGRectMake(rect.size.width - textSize.width, (rect.size.height - textSize.height) / 2, textSize.width, textSize.height);
            break;
        default:
            break;
    }
    
    //绘制
    CGContextRef ref = UIGraphicsGetCurrentContext();
    [self.text drawInRect:inRect withAttributes:@{NSFontAttributeName : self.font}];
//    [self.text drawAtPoint:CGPointMake(0, (rect.size.height - textSize.height) / 2) withAttributes:@{NSFontAttributeName : self.font}];
    CGImageRef imageRef = CGBitmapContextCreateImage(ref);//获取mask
    
    //坐标转换
    CGContextTranslateCTM(ref, 0, rect.size.height);
    CGContextScaleCTM(ref, 1.f, -1.f);
    
    //清空画布
    CGContextClearRect(ref, rect);
    
    //设置mask
    CGContextClipToMask(ref, rect, imageRef);
    
    //设置渐变
    CGColorSpaceRef spaceRef = CGColorSpaceCreateDeviceRGB();
    //--->locations
    int locationSum = (int)self.locations.count;
    CGFloat locationArr[locationSum];
    for (int i = 0; i < locationSum; i ++) {
        locationArr[i] = [self.locations[i] floatValue];
    }
    //--->colors
    int colorSum = (int)self.colors.count * 4;
    CGFloat colorArr[colorSum];
    for (int i = 0; i < colorSum; i ++) {
        switch (i % 4) {
            case 0:
                colorArr[i] = [[[CIColor alloc]initWithColor:(UIColor *)self.colors[i / 4]] red];
                break;
            case 1:
                colorArr[i] = [[[CIColor alloc]initWithColor:(UIColor *)self.colors[i / 4]] green];
                break;
            case 2:
                colorArr[i] = [[[CIColor alloc]initWithColor:(UIColor *)self.colors[i / 4]] blue];
                break;
            case 3:
                colorArr[i] = [[[CIColor alloc]initWithColor:(UIColor *)self.colors[i / 4]] alpha];
                break;
            default:
                break;
        }
    }
    CGGradientRef gradientRef = CGGradientCreateWithColorComponents(spaceRef, colorArr, locationArr, self.colors.count);
    
    CGFloat minX = (rect.size.width - textSize.width) / 2;
    CGFloat minY = (rect.size.height - textSize.height) / 2;
    CGFloat maxX = (rect.size.width - textSize.width) / 2 + textSize.width;
    CGFloat maxY = (rect.size.height - textSize.height) / 2 + textSize.height;
    CGContextDrawLinearGradient(ref, gradientRef, CGPointMake(minX, minY), CGPointMake(maxX, minY), kCGGradientDrawsBeforeStartLocation | kCGGradientDrawsAfterEndLocation);
    
    //清理内存
    CGImageRelease(imageRef);
    CGColorSpaceRelease(spaceRef);
    CGGradientRelease(gradientRef);
}

@end

使用
- (YHGradientLabel *)drawL {
    if (!_drawL) {
        _drawL = [[YHGradientLabel alloc]init];
        _drawL.textAlignment = NSTextAlignmentCenter;
        _drawL.text = @"开始渐变人后一直编下去开始渐变人后一直编";
        _drawL.font = [UIFont systemFontOfSize:18];
        _drawL.colors = @[[UIColor blueColor],[UIColor redColor],[UIColor orangeColor],[UIColor blackColor]];
        _drawL.locations = @[@0,@0.1,@0.7,@1];
    }
    return _drawL;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.view addSubview:self.drawL];
    self.drawL.center = self.view.center;
    self.drawL.bounds = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 100);
}
方式二
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    gradientLayer.colors = @[(id)[UIColor redColor].CGColor, (id)[UIColor greenColor].CGColor];
    gradientLayer.locations = @[@0, @1];// 默认就是均匀分布
    gradientLayer.startPoint = CGPointMake(0, 0);
    gradientLayer.endPoint = CGPointMake(0, 1);
    gradientLayer.frame = self.label.frame;
    gradientLayer.mask = self.label.layer;
    self.label.layer.frame = gradientLayer.bounds;
    [self.view.layer addSublayer:gradientLayer];
}

- (UILabel *)label {
    if (!_label) {
        _label = [[UILabel alloc]init];
        _label.text = @"开始渐变人后一直编下去";
        _label.font = [UIFont systemFontOfSize:25];
    }
    return _label;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.label];
    CGSize size = [self.label.text sizeWithAttributes:@{NSFontAttributeName : self.label.font}];
    self.label.frame = CGRectMake(50, 100, size.width, size.height);
}
上一篇下一篇

猜你喜欢

热点阅读