iOS 中UIlabel文字滚动动画

BBFlashCtntLabel文件

#import <UIKit/UIKit.h>

 

typedef NS_ENUM(NSInteger, BBFlashCtntSpeed) {

    BBFlashCtntSpeedSlow = -1,

    BBFlashCtntSpeedMild,

    BBFlashCtntSpeedFast

};

 

@interface BBFlashCtntLabel : UIView

 

@property (nonatomic, strong) NSString *text;

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

@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];

    }}

 

转载于:https://www.cnblogs.com/anjiubo/p/5334778.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值