iOS 跑馬燈(支持NSAttributedString)

跑馬燈控件網(wǎng)上也有很多Demo,但都是傳NSString類型的字符串,無法滿足不同格式顯示的需求,所以決定自己寫一個同時支持NSString和NSAttributedString的跑馬燈控件以方便自己使用。

跑馬燈.gif

gif速度有點快。。。

頭文件如下所示(非常簡單,像平時使用UILabel一樣就OK了):
@interface SLMarqueeControl : UIView

@property (nonatomic, readonly) UILabel *marqueeLabel;

@end
核心代碼:

監(jiān)控app從后臺進入前臺時的通知(解決從后臺回來時停止?jié)L動問題)

[[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(appBecomeActive:)
                                                     name:UIApplicationDidBecomeActiveNotification
                                                   object:nil
 ];

利用KVO監(jiān)控text變化,完成滾動過程。

[self.marqueeLabel addObserver:self
                            forKeyPath:@"text"
                               options:NSKeyValueObservingOptionNew
                               context:nil
 ];

利用KVO監(jiān)控attributedText變化,完成滾動過程。

[self.marqueeLabel addObserver:self
                            forKeyPath:@"attributedText"
                               options:NSKeyValueObservingOptionNew
                               context:nil
 ];

完成動畫過程。

- (void)startAnimation {
    if (0 == CGRectGetWidth(self.bounds)
        || 0 == CGRectGetHeight(self.bounds)) {
        return;
    }
    
    [self.innerContainer.layer removeAnimationForKey:@"Marquee"];
    [self bringSubviewToFront:self.innerContainer];
    CGSize size = [self evaluateMarqueeLabelContentSize];
    
    for (UIView *view in self.innerContainer.subviews) {
        if ([view isKindOfClass:[UILabel class]]) {
            [view removeFromSuperview];
        }
    }
    
    CGRect rect = CGRectMake(0.f, 0.f, size.width + kLabelOffset, CGRectGetHeight(self.bounds));
    
    UILabel *label = [[UILabel alloc] initWithFrame:rect];
    label.backgroundColor = [UIColor clearColor];
    label.text = self.marqueeLabel.text;
    label.attributedText = self.marqueeLabel.attributedText;
    
    [self.innerContainer addSubview:label];
    
    if (size.width > CGRectGetWidth(self.bounds)) {
        CGRect nextRect = rect;
        nextRect.origin.x = size.width + kLabelOffset;
        
        UILabel *nextLabel = [[UILabel alloc] initWithFrame:nextRect];
        nextLabel.backgroundColor = [UIColor clearColor];
        nextLabel.text = self.marqueeLabel.text;
        nextLabel.attributedText = self.marqueeLabel.attributedText;
        
        [self.innerContainer addSubview:nextLabel];
        
        CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"transform.translation.x"];
        animation.keyTimes = @[@0.f, @1.f];
        animation.duration = size.width / 50.f;
        animation.values = @[@0, @(-(size.width + kLabelOffset))];
        animation.repeatCount = INT16_MAX;
        animation.timingFunction = [CAMediaTimingFunction functionWithName:@"linear"];
        [self.innerContainer.layer addAnimation:animation forKey:@"Marquee"];
    } else {
        label.frame = self.bounds;
    }
}

控件使用很簡單:
NSString類型字符串

NSString *string = @"我是跑馬燈,我只能不停的滾動才能體現(xiàn)我的價值!請注意,前方高能;呃、其實啥也沒有~~~";
self.marqueeControl.marqueeLabel.text = string;

NSAttributedString類型字符串

NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:string];
 [attr addAttributes:@{NSForegroundColorAttributeName:[UIColor yellowColor]}
                  range:NSMakeRange(5, 9)];
    
 [attr addAttributes:@{NSForegroundColorAttributeName:[UIColor redColor],
                          NSFontAttributeName:[UIFont systemFontOfSize:12.f]}
                  range:NSMakeRange(18, 8)];
    
 [attr addAttributes:@{NSForegroundColorAttributeName:[UIColor greenColor]}
                  range:NSMakeRange(30, 6)];
    
 self.attrMarqueeControl.marqueeLabel.attributedText = attr;

GitHub demo: 跑馬燈

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 1、通過CocoaPods安裝項目名稱項目信息 AFNetworking網(wǎng)絡(luò)請求組件 FMDB本地數(shù)據(jù)庫組件 SD...
    陽明AI閱讀 16,168評論 3 119
  • 你笑了又笑 偏偏倒倒 我看著你 我想讓你依靠 你看了看我 笑了笑 轉(zhuǎn)過頭去 再沒有回望 我夢此寐于二零一七 我希望...
    維庸之妻閱讀 181評論 0 0
  • 去除多余行和分割線 去除分割線左邊空白
    霜之幽語閱讀 229評論 0 0
  • 趙高,一開始只是皇宮里的一位小宦官,從小的任務(wù)就是服侍皇上。趙高本是趙國的親戚,也算得上是半個皇室。后來趙國...
    街角賣回憶閱讀 320評論 0 0
  • 即便有著多年的禪修經(jīng)驗,我一樣有著很多困惑不得解答,因為一直都是自修,沒有老師的直接指導(dǎo),有了問題,只能接助書籍的...
    茗心M閱讀 335評論 2 5

友情鏈接更多精彩內(nèi)容