这篇文章给大家主要介绍了iOS中如何实现,在固定的label上动态显示所有文字的方法,文中给出了示例和思路,对大家的理解很有帮助,感兴趣的朋友们下面来一起看看吧。
照例先看下效果图:
思路
创建一个view
作为所有内容的父控件, 并且添加到上面一个 label
, 作为显示文字的载体
UILabel* contentLabel = [[UILabel alloc] init];
[contentLabel sizeToFit];
contentLabel.backgroundColor = [UIColor clearColor]; _contentLabel = contentLabel; [self addSubview:self.contentLabel];
给内容view
的layer
添加一个mask
层, 并且设置其范围为整个view
的bounds
, 这样就让超出view
的内容不会显示出来
CAShapeLayer* maskLayer = [CAShapeLayer layer];
maskLayer.path = [UIBezierPath bezierPathWithRect:self.bounds].CGPath;
self.layer.mask = maskLayer;
给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];
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流。
织梦狗教程
本文标题为:iOS在固定的label上动态显示所有文字


基础教程推荐
猜你喜欢
- Android开发使用RecyclerView添加点击事件实例详解 2023-06-15
- Flutter手势密码的实现示例(附demo) 2023-04-11
- Android多返回栈技术 2023-04-15
- Android中的webview监听每次URL变化实例 2023-01-23
- android studio按钮监听的5种方法实例详解 2023-01-12
- iOS开发教程之XLForm的基本使用方法 2023-05-01
- IOS 播放系统提示音使用总结(AudioToolbox) 2023-03-01
- Flutter绘图组件之CustomPaint使用详解 2023-05-12
- 解决Android Studio突然不显示logcat日志的问题 2023-02-04
- IOS应用内跳转系统设置相关界面的方法 2022-11-20