IOS-Enables dynamic display of all text on a limited label

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.