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

来源:互联网 发布:math js w3c 编辑:程序博客网 时间:2024/06/06 16:35


sds



github连接https://github.com/yuchuanfeng/CFDynamicLabel

思路

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];

0 0
原创粉丝点击