The effect is as follows:
Click Download to view demoIdeas(1) Create a view as the parent control for all content, and add to the label above as a vector for displaying text
UILabel* contentLabel = [[UILabel alloc] init];[contentLabel sizeToFit];
Contentlabel.backgroundcolor = [Uicolor Clearcolor]; _contentlabel = Contentlabel; [Self AddSubview:self.contentLabel];
(2) Add a mask layer to the layer of the content view and set its range to the bounds of the entire view so that the content beyond the view is not displayed
CAShapeLayer* maskLayer = [CAShapeLayer layer];maskLayer.path = [UIBezierPath bezierPathWithRect:self.bounds].CGPath;self.layer.mask = maskLayer;
(3) Adding animations to a 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];
How to use
// 创建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-Enables dynamic display of all text on a limited label