ChangeColorLabel

效果

源码

//
// ChangeColorLabel.h
// YXMWeather
//
// Created by XianMingYou on 15/6/22.
// Copyright (c) 2015年 XianMingYou. All rights reserved.
// #import <UIKit/UIKit.h> @interface ChangeColorLabel : UIView @property (nonatomic, strong) UIFont *font;
@property (nonatomic, strong) UIColor *textColor;
@property (nonatomic, strong) UIColor *changedColor; /**
* 文本
*/
@property (nonatomic, strong) NSString *text; /**
* 设定文本后将会重新更新控件
*/
- (void)updateLabelView; /**
* 颜色百分比
*
* @param percent 颜色的百分比
*/
- (void)colorPercent:(CGFloat)percent; @end
//
// ChangeColorLabel.m
// YXMWeather
//
// Created by XianMingYou on 15/6/22.
// Copyright (c) 2015年 XianMingYou. All rights reserved.
// #import "ChangeColorLabel.h"
#import "AlphaView.h"
#import "UIView+SetRect.h" @interface ChangeColorLabel ()
@property (nonatomic, strong) UILabel *showLabel;
@property (nonatomic, strong) UILabel *alphaLabel;
@property (nonatomic, strong) AlphaView *alphaView;
@end @implementation ChangeColorLabel - (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
[self initLabels];
}
return self;
} - (void)initLabels {
self.showLabel = [[UILabel alloc] initWithFrame:CGRectZero];
[self addSubview:self.showLabel]; self.alphaLabel = [[UILabel alloc] initWithFrame:CGRectZero];
[self addSubview:self.alphaLabel]; // 设定alphaView
self.alphaView = [[AlphaView alloc] initWithFrame:CGRectZero];
self.alphaView.colors = @[[UIColor clearColor],
[UIColor blackColor],
[UIColor blackColor],
[UIColor clearColor]];
self.alphaView.locations = @[@(.f), @(0.05), @(0.95), @(.f)];
self.alphaView.startPoint = CGPointMake(, );
self.alphaView.endPoint = CGPointMake(, );
self.alphaLabel.maskView = self.alphaView;
} /**
* 设定文本后将会重新更新控件
*/
- (void)updateLabelView {
// 字体
UIFont *font = (self.font == nil ? self.showLabel.font : self.font); // 设置文本 + 设置颜色 + 设置字体
self.showLabel.text = self.text;
self.alphaLabel.text = self.text;
self.showLabel.textColor = self.textColor;
self.alphaLabel.textColor = self.changedColor;
self.showLabel.font = font;
self.alphaLabel.font = font; // 重置文本位置
[self.showLabel sizeToFit];
[self.alphaLabel sizeToFit]; // 重新设置alphaView的frame值
self.alphaView.width = self.showLabel.width;
self.alphaView.height = self.showLabel.height;
} /**
* 文本颜色百分比
*
* @param percent 百分比
*/
- (void)colorPercent:(CGFloat)percent {
if (percent <= ) {
self.alphaView.x = -self.showLabel.width;
} else {
self.alphaView.x = -self.showLabel.width + percent * self.showLabel.width;
}
} #pragma mark - 私有方法 @end
//
// AlphaView.h
// YXMWeather
//
// Created by XianMingYou on 15/6/20.
// Copyright (c) 2015年 XianMingYou. All rights reserved.
// #import <UIKit/UIKit.h> @interface AlphaView : UIView @property (nonatomic, strong) NSArray *colors;
@property (nonatomic, strong) NSArray *locations;
@property (nonatomic) CGPoint startPoint;
@property (nonatomic) CGPoint endPoint; - (void)alphaType; @end
//
// AlphaView.m
// YXMWeather
//
// Created by XianMingYou on 15/6/20.
// Copyright (c) 2015年 XianMingYou. All rights reserved.
// #import "AlphaView.h" @interface AlphaView () {
CAGradientLayer *_gradientLayer;
} @end @implementation AlphaView /**
* 修改当前view的backupLayer为CAGradientLayer
*
* @return CAGradientLayer类名字
*/
+ (Class)layerClass {
return [CAGradientLayer class];
} - (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
_gradientLayer = (CAGradientLayer *)self.layer;
}
return self;
} - (void)alphaType {
self.colors = @[[UIColor clearColor], [UIColor blackColor], [UIColor clearColor]];
self.locations = @[@(0.25), @(0.5), @(0.75)];
self.startPoint = CGPointMake(, );
self.endPoint = CGPointMake(, );
} /**
* 重写setter,getter方法
*/
@synthesize colors = _colors;
- (void)setColors:(NSArray *)colors {
_colors = colors; // 将color转换成CGColor
NSMutableArray *cgColors = [NSMutableArray array];
for (UIColor *tmp in colors) {
id cgColor = (__bridge id)tmp.CGColor;
[cgColors addObject:cgColor];
} // 设置Colors
_gradientLayer.colors = cgColors;
}
- (NSArray *)colors {
return _colors;
} @synthesize locations = _locations;
- (void)setLocations:(NSArray *)locations {
_locations = locations;
_gradientLayer.locations = _locations;
}
- (NSArray *)locations {
return _locations;
} @synthesize startPoint = _startPoint;
- (void)setStartPoint:(CGPoint)startPoint {
_startPoint = startPoint;
_gradientLayer.startPoint = startPoint;
}
- (CGPoint)startPoint {
return _startPoint;
} @synthesize endPoint = _endPoint;
- (void)setEndPoint:(CGPoint)endPoint {
_endPoint = endPoint;
_gradientLayer.endPoint = endPoint;
}
- (CGPoint)endPoint {
return _endPoint;
} @end
//
// UIView+SetRect.h
// TestPch
//
// Created by YouXianMing on 14-9-26.
// Copyright (c) 2014年 YouXianMing. All rights reserved.
// #import <UIKit/UIKit.h> @interface UIView (SetRect) // Frame
@property (nonatomic) CGPoint viewOrigin;
@property (nonatomic) CGSize viewSize; // Frame Origin
@property (nonatomic) CGFloat x;
@property (nonatomic) CGFloat y; // Frame Size
@property (nonatomic) CGFloat width;
@property (nonatomic) CGFloat height; // Frame Borders
@property (nonatomic) CGFloat top;
@property (nonatomic) CGFloat left;
@property (nonatomic) CGFloat bottom;
@property (nonatomic) CGFloat right; // Center Point
#if !IS_IOS_DEVICE
@property (nonatomic) CGPoint center;
#endif
@property (nonatomic) CGFloat centerX;
@property (nonatomic) CGFloat centerY; // Middle Point
@property (nonatomic, readonly) CGPoint middlePoint;
@property (nonatomic, readonly) CGFloat middleX;
@property (nonatomic, readonly) CGFloat middleY;
@end
//
// UIView+SetRect.m
// TestPch
//
// Created by YouXianMing on 14-9-26.
// Copyright (c) 2014年 YouXianMing. All rights reserved.
// #import "UIView+SetRect.h" @implementation UIView (SetRect) #pragma mark Frame - (CGPoint)viewOrigin
{
return self.frame.origin;
} - (void)setViewOrigin:(CGPoint)newOrigin
{
CGRect newFrame = self.frame;
newFrame.origin = newOrigin;
self.frame = newFrame;
} - (CGSize)viewSize
{
return self.frame.size;
} - (void)setViewSize:(CGSize)newSize
{
CGRect newFrame = self.frame;
newFrame.size = newSize;
self.frame = newFrame;
} #pragma mark Frame Origin - (CGFloat)x
{
return self.frame.origin.x;
} - (void)setX:(CGFloat)newX
{
CGRect newFrame = self.frame;
newFrame.origin.x = newX;
self.frame = newFrame;
} - (CGFloat)y
{
return self.frame.origin.y;
} - (void)setY:(CGFloat)newY
{
CGRect newFrame = self.frame;
newFrame.origin.y = newY;
self.frame = newFrame;
} #pragma mark Frame Size - (CGFloat)height
{
return self.frame.size.height;
} - (void)setHeight:(CGFloat)newHeight
{
CGRect newFrame = self.frame;
newFrame.size.height = newHeight;
self.frame = newFrame;
} - (CGFloat)width
{
return self.frame.size.width;
} - (void)setWidth:(CGFloat)newWidth
{
CGRect newFrame = self.frame;
newFrame.size.width = newWidth;
self.frame = newFrame;
} #pragma mark Frame Borders - (CGFloat)left
{
return self.x;
} - (void)setLeft:(CGFloat)left
{
self.x = left;
} - (CGFloat)right
{
return self.frame.origin.x + self.frame.size.width;
} - (void)setRight:(CGFloat)right
{
self.x = right - self.width;
} - (CGFloat)top
{
return self.y;
} - (void)setTop:(CGFloat)top
{
self.y = top;
} - (CGFloat)bottom
{
return self.frame.origin.y + self.frame.size.height;
} - (void)setBottom:(CGFloat)bottom
{
self.y = bottom - self.height;
} #pragma mark Center Point #if !IS_IOS_DEVICE
- (CGPoint)center
{
return CGPointMake(self.left + self.middleX, self.top + self.middleY);
} - (void)setCenter:(CGPoint)newCenter
{
self.left = newCenter.x - self.middleX;
self.top = newCenter.y - self.middleY;
}
#endif - (CGFloat)centerX
{
return self.center.x;
} - (void)setCenterX:(CGFloat)newCenterX
{
self.center = CGPointMake(newCenterX, self.center.y);
} - (CGFloat)centerY
{
return self.center.y;
} - (void)setCenterY:(CGFloat)newCenterY
{
self.center = CGPointMake(self.center.x, newCenterY);
} #pragma mark Middle Point - (CGPoint)middlePoint
{
return CGPointMake(self.middleX, self.middleY);
} - (CGFloat)middleX
{
return self.width / ;
} - (CGFloat)middleY
{
return self.height / ;
} @end

使用

//
// ViewController.m
// ChangeColorLabel
//
// Created by YouXianMing on 15/6/25.
// Copyright (c) 2015年 YouXianMing. All rights reserved.
// #import "ViewController.h"
#import "ChangeColorLabel.h"
#import "UIView+SetRect.h" @interface ViewController () <UIScrollViewDelegate> @property (nonatomic, strong) ChangeColorLabel *label; @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad]; self.view.backgroundColor = [UIColor blackColor]; self.label = [[ChangeColorLabel alloc] initWithFrame:CGRectMake(, , , )];
self.label.font = [UIFont fontWithName:@"AppleSDGothicNeo-UltraLight" size:.f];
self.label.textColor = [UIColor redColor];
self.label.changedColor = [UIColor purpleColor];
self.label.text = @"YouXianMing";
self.label.center = self.view.center;
self.label.x += ;
[self.label updateLabelView];
[self.view addSubview:self.label];
[self.label colorPercent:]; UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
scrollView.contentSize = CGSizeMake(self.view.width * , self.view.height);
scrollView.delegate = self;
[self.view addSubview:scrollView]; } - (void)scrollViewDidScroll:(UIScrollView *)scrollView { CGFloat x = scrollView.contentOffset.x;
CGFloat percent = x / self.view.width; [self.label colorPercent:percent];
} @end

[控件] ChangeColorLabel的更多相关文章

  1. JS调用Android、Ios原生控件

    在上一篇博客中已经和大家聊了,关于JS与Android.Ios原生控件之间相互通信的详细代码实现,今天我们一起聊一下JS调用Android.Ios通信的相同点和不同点,以便帮助我们在进行混合式开发时, ...

  2. HTML5 progress和meter控件

    在HTML5中,新增了progress和meter控件.progress控件为进度条控件,可表示任务的进度,如Windows系统中软件的安装.文件的复制等场景的进度.meter控件为计量条控件,表示某 ...

  3. 百度 flash html5自切换 多文件异步上传控件webuploader基本用法

    双核浏览器下在chrome内核中使用uploadify总有302问题,也不知道如何修复,之所以喜欢360浏览器是因为帮客户控制渲染内核: 若页面需默认用极速核,增加标签:<meta name=& ...

  4. JS与APP原生控件交互

    "热更新"."热部署"相信对于混合式开发的童鞋一定不陌生,那么APP怎么避免每次升级都要在APP应用商店发布呢?这里就用到了混合式开发的概念,对于电商网站尤其显 ...

  5. UWP开发必备:常用数据列表控件汇总比较

    今天是想通过实例将UWP开发常用的数据列表做汇总比较,作为以后项目开发参考.UWP开发必备知识点总结请参照[UWP开发必备以及常用知识点总结]. 本次主要讨论以下控件: GridView:用于显示数据 ...

  6. 【踩坑速记】开源日历控件,顺便全面解析开源库打包发布到Bintray/Jcenter全过程(新),让开源更简单~

    一.写在前面 自使用android studio开始,就被它独特的依赖方式:compile 'com.android.support:appcompat-v7:25.0.1'所深深吸引,自从有了它,麻 ...

  7. 对百度WebUploader开源上传控件的二次封装,精简前端代码(两句代码搞定上传)

    前言 首先声明一下,我这个是对WebUploader开源上传控件的二次封装,底层还是WebUploader实现的,只是为了更简洁的使用他而已. 下面先介绍一下WebUploader 简介: WebUp ...

  8. Windows API 设置窗口下控件Enable属性

    参考页面: http://www.yuanjiaocheng.net/webapi/create-crud-api-1-put.html http://www.yuanjiaocheng.net/we ...

  9. VB.NET设置控件和窗体的显示级别

    前言:在用VB.NET开发射频检测系统ADS时,当激活已存在的目标MDI子窗体时,被其他子窗体遮住了,导致目标MDI子窗体不能显示. 这个问题怎么解决呢?网上看到一篇帖子VB.NET设置控件和窗体的显 ...

随机推荐

  1. 自然语言处理--TF-IDF(关键词提取)

    TF-IDF算法 TF-IDF(词频-逆文档频率)算法是一种统计方法,用以评估一字词对于一个文件集或一个语料库中的其中一份文件的重要程度.字词的重要性随着它在文件中出现的次数成正比增加,但同时会随着它 ...

  2. MySQL的各种join

    常用的是这5个join 首先join = inner join   这里有说明:点击打开链接 这里我就不用表来说明了,例子看这里 简单的说就是 inner join:不以谁为基准,只有符合关系的才会选 ...

  3. css3的overflow-anchor

    overflow-anchor属性使我们能够选择退出滚动锚定,这是一个浏览器特性,旨在允许内容在用户当前的DOM位置上加载,而不需要在内容完全加载后更改用户的位置. 为何要有这个属性? 滚动锚定是一种 ...

  4. [转][C#] 对List<T>取交集、连集及差集

    本文转自:http://www.cnblogs.com/shuibin/archive/2012/04/19/2457867.html 最近在專案中,剛好遇到這個需求, 需要比對兩個List,進行一些 ...

  5. 关于在浏览器中测试cordova plugin的注意事项。

    本文介绍有关Ionic Native能力的注意事项: 1)按官方文档安装对应的cordova插件,比如:ionic cordova plugin add cordova-plugin-datepick ...

  6. ASP.NET 之 MVC框架及搭建

    一.MVC简介 MVC:Model-View-Controller(模型-视图-控制器),MVC是一种软件开发架构模式. 1.模型(Model) 模型对象是实现应用程序数据域逻辑的应用程序部件. 通常 ...

  7. Deep Q-Network 学习笔记(六)—— 改进④:dueling dqn

    这篇同样是完全没看懂 Orz,这里只做实现记录.. 要改动的地方只是在神经网络的最后一层做下调整即可. def create(self): neuro_layer_1 = 3 w_init = tf. ...

  8. SQL 修改表字段失败 解决方法

    OK  大功告成 !!!

  9. Html dom 赋值

    1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="U ...

  10. Java利用反射取得类的所有信息

    Java中可以利用反射获取类的名称.构造函数.属性.方法.也就是说可以通过反射可以取得类的所有信息(不管该成员是否封装为private). 如有下面的Dept类定义: package org.lyk. ...