Giter VIP home page Giter VIP logo

cfdynamiclabel's Introduction

CFDynamicLabel

实现在有限label上 动态显示所有文字

效果如下:

image

思路

(1)创建一个view 作为所有内容的父控件, 并且添加到上面一个 label, 作为显示文字的载体

UILabel* contentLabel = [[UILabel alloc] init];
[contentLabel sizeToFit];

contentLabel.backgroundColor = [UIColor clearColor]; _contentLabel = contentLabel; [self addSubview:self.contentLabel];

(2)给内容view的layer添加一个mask层, 并且设置其范围为整个view的bounds, 这样就让超出view的内容不会显示出来

CAShapeLayer* maskLayer = [CAShapeLayer layer];
maskLayer.path = [UIBezierPath bezierPathWithRect:self.bounds].CGPath;
self.layer.mask = maskLayer;

(3)给label添加动画

CAKeyframeAnimation* keyFrame = [CAKeyframeAnimation animation];
keyFrame.keyPath = @"transform.translation.x";
keyFrame.values = @[@(0), @(-space), @(0)];
keyFrame.repeatCount = NSIntegerMax;
keyFrame.duration = self.speed * self.contentLabel.text.length;
keyFrame.timingFunctions = @[[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut], [CAMediaTimingFunction functionWithControlPoints:0 :0 :0.5 :0.5]];
keyFrame.delegate = self;

[self.contentLabel.layer addAnimation:keyFrame forKey:nil];

使用方法

// 创建
CFDynamicLabel* testLabel = [[CFDynamicLabel alloc] initWithFrame:CGRectMake(100, 300, 180, 21)];
// 设置滚动速度
testLabel.speed = 0.6;
[self.view addSubview:testLabel];
// 设置基本属性
testLabel.text = @"我不想说再见,不说再见,越长大越孤单";
testLabel.textColor = [UIColor yellowColor];
testLabel.font = [UIFont systemFontOfSize:23];
testLabel.backgroundColor = [UIColor grayColor];

cfdynamiclabel's People

Contributors

yuchuanfeng avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.