iOS 动态变色的Label

iOS 动态变色的Label_第1张图片
BlendColor.gif

实现原理##

@interface WYColorLabel : UILabel

///  变色比例  0~1
@property (nonatomic, assign) CGFloat colorRatio;
///  混合颜色
@property (nonatomic, strong) UIColor *blendColor;

@end
#import "WYColorLabel.h"

@implementation WYColorLabel

- (void)drawRect:(CGRect)rect {
    // 1.绘制文字
    [super drawRect:rect];
    
    rect.size.width *= self.colorRatio;
    // 2.设置颜色
    [self.blendColor set];
    
    UIRectFillUsingBlendMode(rect, kCGBlendModeSourceIn);
}

- (void)setColorRatio:(CGFloat)colorRatio {
    _colorRatio = colorRatio;
    
    [self setNeedsDisplay];
}

@end

github Demo:https://github.com/wangyansnow/WYBlendColor
使用的时候直接参照Demo把WYColorLabel拖入到项目中即可

你可能感兴趣的:(iOS 动态变色的Label)