iOS在固定的label上動態(tài)顯示所有文字
更新時間:2016年10月14日 08:43:03 投稿:daisy
這篇文章給大家主要介紹了iOS中如何實現(xiàn),在固定的label上動態(tài)顯示所有文字的方法,文中給出了示例和思路,對大家的理解很有幫助,感興趣的朋友們下面來一起看看吧。
照例先看下效果圖:

思路
創(chuàng)建一個view 作為所有內(nèi)容的父控件, 并且添加到上面一個 label, 作為顯示文字的載體
UILabel* contentLabel = [[UILabel alloc] init]; [contentLabel sizeToFit]; contentLabel.backgroundColor = [UIColor clearColor]; _contentLabel = contentLabel; [self addSubview:self.contentLabel];
給內(nèi)容view的layer添加一個mask層, 并且設置其范圍為整個view的bounds, 這樣就讓超出view的內(nèi)容不會顯示出來
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];
使用方法
// 創(chuàng)建 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];
總結
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學習或者工作能帶來一定的幫助,如果有疑問大家可以留言交流。
相關文章
iOS中FMDB事務實現(xiàn)批量更新數(shù)據(jù)
這篇文章主要為大家詳細介紹了iOS中FMDB事務實現(xiàn)批量更新數(shù)據(jù),具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-11-11
一篇文章讓你看懂IOS中的block為何再也不需要WeakSelf弱引用
這篇文章主要給大家介紹了關于IOS中block為何再也不需要WeakSelf弱引用的相關資料,文中通過示例代碼介紹的非常詳細,對各位iOS開發(fā)者們具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧。2018-01-01
Objective-C的NSOperation多線程類基本使用指南
這篇文章主要介紹了Objective-C的NSOperation多線程類基本使用指南,談到了Operations的執(zhí)行順序和并發(fā)量等設置操作,需要的朋友可以參考下2016-02-02

