我是靠谱客的博主 任性皮皮虾,这篇文章主要介绍iOS在固定的label上动态显示所有文字,现在分享给大家,希望可以做个参考。

照例先看下效果图:

思路

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

复制代码
1
2
3
UILabel* contentLabel = [[UILabel alloc] init]; [contentLabel sizeToFit]; contentLabel.backgroundColor = [UIColor clearColor]; _contentLabel = contentLabel; [self addSubview:self.contentLabel];

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

复制代码
1
2
3
CAShapeLayer* maskLayer = [CAShapeLayer layer]; maskLayer.path = [UIBezierPath bezierPathWithRect:self.bounds].CGPath; self.layer.mask = maskLayer;

label添加动画

复制代码
1
2
3
4
5
6
7
8
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];

使用方法

复制代码
1
2
3
4
5
6
7
8
9
10
// 创建 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];

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流。

最后

以上就是任性皮皮虾最近收集整理的关于iOS在固定的label上动态显示所有文字的全部内容,更多相关iOS在固定内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(112)

评论列表共有 0 条评论

立即
投稿
返回
顶部