iOS 动态变色的Label

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拖入到项目中即可

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容