Label文字漸變色

之前公司有需求做文字的的漸變色,自己當(dāng)時(shí)也是網(wǎng)上看了一些,自己寫了兩個(gè)方法,實(shí)現(xiàn)了需求,寫了很久了,只是現(xiàn)在才想起來,就當(dāng)繼續(xù)學(xué)習(xí)了。

先看看簡(jiǎn)單的:

- (void)addGradientRampWithColors:(NSArray *)colors text:(NSString *)text {
   //label在父視圖上的(x,y)的值不是中心點(diǎn)
   CGPoint point = CGPointMake(30, 500);
   UILabel *label = [[UILabel alloc]init];
   label.text = text;
   label.font = [UIFont systemFontOfSize:20];
  // label.textAlignment = NSTextAlignmentCenter;
   [label sizeToFit];
   //label的中心和想象的一樣啦!!
   label.center = CGPointMake(point.x + CGRectGetWidth(label.bounds)/2, point.y -  CGRectGetHeight(label.bounds)/2);
   [self.view addSubview:label];
   //這個(gè)label是和上面的label是對(duì)齊的哦,之前都不好對(duì)齊,用這樣的方法設(shè)置frame就好了
//    UILabel *infoTextLabel = [[UILabel alloc] init];
//    infoTextLabel.frame = CGRectMake(label.center.x - CGRectGetWidth(label.bounds)/2 ,point.y + 30, 220, 50);
//    infoTextLabel.text = @"你說的是哦";
//    infoTextLabel.font = [UIFont systemFontOfSize:20];
//    infoTextLabel.backgroundColor  =[UIColor redColor];
//    infoTextLabel.numberOfLines = 0;
//    infoTextLabel.textAlignment = NSTextAlignmentLeft;
//    infoTextLabel.textColor = [UIColor blueColor];
//    [infoTextLabel sizeToFit];
//    [self.view addSubview:infoTextLabel];
   //在后面添加漸變圖層
   CAGradientLayer *gradientLayer = [CAGradientLayer layer];
   gradientLayer.frame  = label.frame;
   gradientLayer.colors = colors;
   //漸變的方向(0,0) (0,1) (1,0)(1,1)為四個(gè)頂點(diǎn)方向
   //(I.e. [0,0] is the bottom-left
   // corner of the layer, [1,1] is the top-right corner.) The default values
   // are [.5,0] and [.5,1]
   gradientLayer.startPoint = CGPointMake(0, 1);
   gradientLayer.endPoint = CGPointMake(1, 1);
   [self.view.layer addSublayer:gradientLayer];
   gradientLayer.mask = label.layer;
   label.frame = gradientLayer.bounds;
}```
自己覺得這樣的方法用起來不是很方便,所以接下來是另一種方法:
.m文件

@implementation CFGradientLabel

  • (void)drawRect:(CGRect)rect {

    CGSize textSize = [self.text sizeWithAttributes:@{NSFontAttributeName : self.font}];
    CGRect textRect = (CGRect){0, 0, textSize};

    // 畫文字(不做顯示用, 主要作用是設(shè)置 layer 的 mask)
    CGContextRef context = UIGraphicsGetCurrentContext();
    [self.textColor set];
    [self.text drawWithRect:rect options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : self.font} context:NULL];

    // 坐標(biāo)(只對(duì)設(shè)置后的畫到 context 起作用, 之前畫的文字不起作用)
    CGContextTranslateCTM(context, 0.0f, rect.size.height - (rect.size.height - textSize.height) * 0.5);
    CGContextScaleCTM(context, 1.0f, -1.0f);

    CGImageRef alphaMask = CGBitmapContextCreateImage(context);
    CGContextClearRect(context, rect); // 清除之前畫的文字

    // 設(shè)置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);

    // 釋放內(nèi)存
    CGColorSpaceRelease(colorSpace);
    CGGradientRelease(gradient);
    CFRelease(alphaMask);
    }

.h 文件

@interface CFGradientLabel : UILabel

@property(nonatomic, strong) NSArray* colors;

@end

接下來是調(diào)用的方法,修改了一下的
  • (void)addGradientLabelWithFrame:(CGPoint)point gradientText:(NSString *)text infoText:(NSString *)infoText colors:(NSArray *)colors font:(UIFont *)font {

    static NSInteger labelTag = 200;
    CFGradientLabel *lable = [[CFGradientLabel alloc] init];
    lable.text = text;
    lable.font = font;
    lable.tag = labelTag;
    lable.textAlignment = NSTextAlignmentCenter;
    [lable sizeToFit];
    //之前項(xiàng)目的時(shí)候設(shè)置了為0,忘了注釋,所以有的小伙伴用的時(shí)候就不顯示了……(-)
    // lable.alpha = 0;
    lable.center = point;
    lable.colors = colors;
    [self.view addSubview:lable];
    }

[做了一個(gè)demo](https://github.com/wangtaoHappy/Label-)
做的是引導(dǎo)頁(yè),看看效果圖如下:

![1080-1920(中文2).jpg](http://upload-images.jianshu.io/upload_images/2775355-66325b5ec1b03d15.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

![1080-1920(英文4).jpg](http://upload-images.jianshu.io/upload_images/2775355-64de3167a9ba5618.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)


那先就這樣啦,歡迎指教!!
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 173,422評(píng)論 25 708
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫(kù)、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 12,246評(píng)論 4 61
  • 妖妖z閱讀 336評(píng)論 5 8
  • 今天是我出國(guó)滿一年的日子。沒什么特別的感想,換了個(gè)地方吃飯睡覺自拍罷了。當(dāng)然,能在雪地里照相,我是很高興的。 這1...
    肥杰西閱讀 1,870評(píng)論 6 24
  • 最近看畫板,就自己對(duì)照sketchmaster做了一個(gè),效果還可以,發(fā)出來共享下,git地址:AnyDraw。 純...
    小小棒棒糖閱讀 627評(píng)論 6 5