美文网首页iOS
iOS - 实现在有限label上 动态显示所有文字

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

作者: violafa | 来源:发表于2017-01-04 23:39 被阅读31次

    专著:http://www.jianshu.com/p/a490131e00e7

    效果如下:

    001.gif

    点击下载查看demo

    思路

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

    UILabel* contentLabel = [[UILabelalloc] init];[contentLabel sizeToFit];contentLabel.backgroundColor= [UIColorclearColor]; _contentLabel = contentLabel; [selfaddSubview:self.contentLabel];

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

    CAShapeLayer* maskLayer = [CAShapeLayerlayer];maskLayer.path= [UIBezierPathbezierPathWithRect:self.bounds].CGPath;self.layer.mask= maskLayer;

    给label添加动画

    CAKeyframeAnimation* keyFrame = [CAKeyframeAnimationanimation];keyFrame.keyPath=@"transform.translation.x";keyFrame.values= @[@(0), @(-space), @(0)];keyFrame.repeatCount=NSIntegerMax;keyFrame.duration=self.speed*self.contentLabel.text.length;keyFrame.timingFunctions= @[[CAMediaTimingFunctionfunctionWithName:kCAMediaTimingFunctionEaseInEaseOut], [CAMediaTimingFunctionfunctionWithControlPoints:0:0:0.5:0.5]];keyFrame.delegate=self;[self.contentLabel.layeraddAnimation:keyFrame forKey:nil];

    使用方法

    // 创建CFDynamicLabel* testLabel = [[CFDynamicLabelalloc] initWithFrame:CGRectMake(100,300,180,21)];// 设置滚动速度testLabel.speed=0.6;[self.viewaddSubview:testLabel];// 设置基本属性testLabel.text=@"我不想说再见,不说再见,越长大越孤单";testLabel.textColor= [UIColoryellowColor];testLabel.font= [UIFontsystemFontOfSize:23];testLabel.backgroundColor= [UIColorgrayColor];

    相关文章

      网友评论

        本文标题:iOS - 实现在有限label上 动态显示所有文字

        本文链接:https://www.haomeiwen.com/subject/slvavttx.html