iOS 中UIlabel文字滾動動畫

BBFlashCtntLabel文件測試

#import <UIKit/UIKit.h>atom

 

typedef NS_ENUM(NSInteger, BBFlashCtntSpeed) {lua

    BBFlashCtntSpeedSlow = -1,orm

    BBFlashCtntSpeedMild,索引

    BBFlashCtntSpeedFastip

};rem

 

@interface BBFlashCtntLabel : UIViewanimation

 

@property (nonatomic, strong) NSString *text;string

@property (nonatomic, strong) UIFont *font;         // 默認:system(15)it

@property (nonatomic, strong) UIColor *textColor;

 

@property (nonatomic, strong) NSAttributedString *attributedText;

 

@property (nonatomic, assign) BBFlashCtntSpeed speed;

 

// 循環滾動次數(爲0時無限滾動)

@property (nonatomic, assign) NSUInteger repeatCount;

 

@property (nonatomic, assign) CGFloat leastInnerGap;

 

- (void)reloadView;

 

@end

 BBFlashCtntLabel文件

#import "BBFlashCtntLabel.h"

 

@interface BBFlashCtntLabel()

{

    BOOL seted;

    

    BOOL moveNeed;

    

    CGFloat rate;

}

 

@property (nonatomic, strong) UIView *innerContainer;

 

@end

 

@implementation BBFlashCtntLabel

 

- (void)awakeFromNib

{

    [super awakeFromNib];

    self.font = [UIFont systemFontOfSize:15];

    self.textColor = [UIColor blackColor];

    self.speed = BBFlashCtntSpeedMild;

    self.repeatCount = 0;

    self.leastInnerGap = 10.f;

    self.clipsToBounds = YES;

    rate = 80.f;

    [self setup];

}

 

- (instancetype)initWithFrame:(CGRect)frame

{

    if (self = [super initWithFrame:frame]) {

        self.font = [UIFont systemFontOfSize:15];

        self.textColor = [UIColor blackColor];

        self.speed = BBFlashCtntSpeedMild;

        self.repeatCount = 0;

        self.leastInnerGap = 10.f;

        self.clipsToBounds = YES;

        [self setup];

    }

    return self;

}

 

- (void)setSpeed:(BBFlashCtntSpeed)speed

{

    _speed = speed;

    switch (_speed) {

        case BBFlashCtntSpeedFast:

            rate = 90.;

            break;

        case BBFlashCtntSpeedMild:

            rate = 75;

            break;

        case BBFlashCtntSpeedSlow:

            rate = 40.;

            break;

        default:

            break;

    }

    [self reloadView];

}

 

- (void)setLeastInnerGap:(CGFloat)leastInnerGap

{

    _leastInnerGap = leastInnerGap;

    [self reloadView];

}

 

- (void)setText:(NSString *)text

{

    _text = text;

    _attributedText = nil;

    [self reloadView];

}

 

- (void)setAttributedText:(NSAttributedString *)attributedText

{

    _attributedText = [self setAttributedTextDefaultFont:attributedText];

    _text = nil;

    [self reloadView];

}

 

- (NSAttributedString *)setAttributedTextDefaultFont:(NSAttributedString *)attrText

{

    NSMutableAttributedString *rtn = [[NSMutableAttributedString alloc] initWithAttributedString:attrText];

    void (^enumerateBlock)(id, NSRange, BOOL *) = ^(id value, NSRange range, BOOL *stop) {

        if (!value || [value isKindOfClass:[NSNull class]]) {

           

            [rtn addAttribute:NSFontAttributeName

                        value:self.font

                        range:range];

        }

    };

    [rtn enumerateAttribute:NSFontAttributeName

                    inRange:NSMakeRange(0, rtn.string.length)

                    options:0

                 usingBlock:enumerateBlock];

    return rtn;

}

 

- (void)setFont:(UIFont *)font

{

    _font = font;

    [self reloadView];

}

 

- (void)setTextColor:(UIColor *)textColor

{

    _textColor = textColor;

    [self reloadView];

}

 

- (void)setup

{

    if (seted) {

        return ;

    }

    self.innerContainer = [[UIView alloc] initWithFrame:self.bounds];

    self.innerContainer.backgroundColor = [UIColor clearColor];

    self.innerContainer.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;

    

    [self addSubview:self.innerContainer];

    

    seted = YES;

}

 

- (void)reloadView

{

    [self.innerContainer.layer removeAnimationForKey:@"move"];

    for (UIView *sub in self.innerContainer.subviews) {

        if ([sub isKindOfClass:[UILabel class]]) {

            [sub removeFromSuperview];

        }

    }

    CGFloat width = [self evaluateContentWidth];

    moveNeed = width > self.bounds.size.width;

    CGRect f = CGRectMake(0, 0, width, self.bounds.size.height);

    UILabel *label = [[UILabel alloc] initWithFrame:f];

    label.backgroundColor = [UIColor clearColor];

    if (self.text) {

        label.text = self.text;

        label.textColor = self.textColor;

        label.font = self.font;

    } else {

        label.attributedText = self.attributedText;

    }

    

    [self.innerContainer addSubview:label];

    if (moveNeed) {

        CGRect f1 = CGRectMake(width + self.leastInnerGap, 0, width, f.size.height);

        UILabel *next = [[UILabel alloc] initWithFrame:f1];

        next.backgroundColor = [UIColor clearColor];

        if (self.text) {

            next.text = self.text;

            next.textColor = self.textColor;

            next.font = self.font;

        } else {

            next.attributedText = self.attributedText;

        }

        

        [self.innerContainer addSubview:next];

        

        CAKeyframeAnimation *moveAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform.translation.x"];

        moveAnimation.keyTimes = @[@0., @0.191, @0.868, @1.0];

        moveAnimation.duration = width / rate;

        moveAnimation.values = @[@0, @0., @(- width - self.leastInnerGap)];

        moveAnimation.repeatCount = self.repeatCount == 0 ? INT16_MAX : self.repeatCount;

        moveAnimation.timingFunction = [CAMediaTimingFunction functionWithName:@"linear"];

        [self.innerContainer.layer addAnimation:moveAnimation forKey:@"move"];

    }

}

 

- (CGFloat)evaluateContentWidth

{

    CGFloat width = 0.f;

    NSStringDrawingOptions options = NSStringDrawingTruncatesLastVisibleLine |

    NSStringDrawingUsesLineFragmentOrigin |

    NSStringDrawingUsesFontLeading;

    if (_text.length > 0) {

        NSDictionary *attr = @{NSFontAttributeName : self.font};

        CGSize size = [_text boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, self.bounds.size.height) options:options attributes:attr context:nil].size;

        width = size.width;

        

    } else if (_attributedText.length > 0) {

        CGSize size = [_attributedText boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, self.bounds.size.height) options:options context:nil].size;

        width = size.width;

    }

    

    return width;

}

 

@end

 

.m文件

 for (int i = 0; i < 5; i++) {

        //利用循環從下至上添加5個lable

        CGRect rect = CGRectMake(100, 450 - i * 105, 180, 100);

        BBFlashCtntLabel *lbl = [[BBFlashCtntLabel alloc] initWithFrame:rect];

        //設置文本框的背景顏色

        lbl.backgroundColor = [UIColor lightGrayColor];

//        lbl.leastInnerGap = 50.f;

        //對第三個文本框進行設置

        if (i % 3 == 0) {

            //設置播放次數

            lbl.repeatCount = 5;

            //設置播放速度爲 慢

            lbl.speed = BBFlashCtntSpeedSlow;

        }

        else if (i % 3 == 1) {

            //設置第一 跟第四個文本框的播放速度爲 中

            lbl.speed = BBFlashCtntSpeedMild;

        }

        else {

            //設置播放速度爲 快

            lbl.speed = BBFlashCtntSpeedFast;

        }

        NSString *str = @"測試文字。來來;‘了哈哈😄^_^abcdefg123456👿";

        

        if (i %2 == 0) {

            lbl.text = str;

            

            lbl.font = [UIFont systemFontOfSize:25];

            

            lbl.textColor = [UIColor whiteColor];

        } else {

            

            NSMutableAttributedString *att = [[NSMutableAttributedString alloc] initWithString:str];

            //根據索引值改變文字的大小

            [att addAttribute:NSFontAttributeName

                        value:[UIFont systemFontOfSize:25]

                        range:NSMakeRange(0, 5)];

            

            [att addAttribute:NSFontAttributeName

                        value:[UIFont systemFontOfSize:17]

                        range:NSMakeRange(15, 5)];

            //根據索引值給文字添加背景顏色

            [att addAttribute:NSBackgroundColorAttributeName

                        value:[UIColor blueColor]

                        range:NSMakeRange(0, 15)];

            //給文字添加顏色

            [att addAttribute:NSForegroundColorAttributeName

                        value:[UIColor redColor]

                        range:NSMakeRange(8, 7)];

            lbl.attributedText = att;

        }

        if (i == 0) {

            lbl.textColor = [UIColor greenColor];

            lbl.text = @"少許文字";

        }

        

        [self.view addSubview:lbl];

    }}

相關文章
相關標籤/搜索