UILabel中字体滚动显示

记录下UILabel中字体超出Label宽度时滚动显示

SHFontCycleLabel.h

//
//  SHFontCycleLabel.h
//  SHFontCycleLabel
//
//  Created by HarrySun on 2017/4/21.
//  Copyright © 2017年 Mobby. All rights reserved.
//
//  简书:http://www.jianshu.com/u/87c7aa9de064
//  GitHub:https://github.com/272095249
//

#import <UIKit/UIKit.h>

@interface SHFontCycleLabel : UIView


@property (nonatomic, strong) UILabel *myLabel;

@property (nonatomic, copy) NSString *labelText;


@end

SHFontCycleLabel.m

//
//  SHFontCycleLabel.m
//  SHFontCycleLabel
//
//  Created by HarrySun on 2017/4/21.
//  Copyright © 2017年 Mobby. All rights reserved.
//
//  简书:http://www.jianshu.com/u/87c7aa9de064
//  GitHub:https://github.com/272095249
//

#import "SHFontCycleLabel.h"


@interface SHFontCycleLabel ()

@property (nonatomic, strong) UILabel *secondLabel;
@property (nonatomic, strong) NSTimer *myTimer;

@end

@implementation SHFontCycleLabel

- (instancetype)initWithFrame:(CGRect)frame{
    
    if (self = [super initWithFrame:frame]) {
        
        [self addSubview:self.myLabel];
        self.layer.masksToBounds = YES;
    }
    
    return self;
}

- (void)setLabelText:(NSString *)labelText{
    
    [self.myTimer invalidate];
    self.myTimer = nil;
    [self.secondLabel removeFromSuperview];
    
    self.myLabel.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
    
    _labelText = labelText;
    self.myLabel.text = _labelText;
    
    CGRect rect = [_labelText boundingRectWithSize:CGSizeMake(10000, self.frame.size.height) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:self.myLabel.font} context:nil];
    
    if (rect.size.width > self.frame.size.width) {
        
        self.myLabel.frame = CGRectMake(0, 0, rect.size.width + 30, self.frame.size.height);
        [self createSecondLabel];
        self.myTimer = [NSTimer scheduledTimerWithTimeInterval:0.025 target:self selector:@selector(startFontCycle) userInfo:nil repeats:YES];
    }
}

- (void)startFontCycle{
    
    self.myLabel.frame = CGRectMake(self.myLabel.frame.origin.x - 1, 0, self.myLabel.frame.size.width, self.myLabel.frame.size.height);
    self.secondLabel.frame = CGRectMake(CGRectGetMaxX(self.myLabel.frame), 0, self.secondLabel.frame.size.width, self.secondLabel.frame.size.height);
    
    if (CGRectGetMaxX(self.myLabel.frame) < 0) {
        
        UILabel *tempLabel;
        tempLabel = self.myLabel;
        self.myLabel = self.secondLabel;
        self.secondLabel = tempLabel;
    }
}



- (void)createSecondLabel{
    
    if (self.secondLabel) {
        
        self.secondLabel = nil;
    }
    
    self.secondLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.myLabel.frame.size.width, 0, self.myLabel.frame.size.width, self.frame.size.height)];
    self.secondLabel.text = self.myLabel.text;
    
    [self addSubview:self.secondLabel];
}

- (UILabel *)myLabel{
    
    if (!_myLabel) {
        
        _myLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    }
    
    return _myLabel;
}


- (void)dealloc{
    
    [self.myTimer invalidate];
    self.myTimer = nil;
}

@end

SHFontCycleLabel使用

#import "SHFontCycleLabel.h"


SHFontCycleLabel *label = [[SHFontCycleLabel alloc]initWithFrame:CGRectMake(10, 100, 120, 40)];
[label setBackgroundColor:[UIColor yellowColor]];
label.labelText = @"测试Label滚动,测试Label滚动,测试Label滚动,测试Label滚动";
[self.view addSubview:label];
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,262评论 4 61
  • if goto for switch 函数 func defer 你可以在函数中添加多个defer语句。当函数执行...
    楼上那位阅读 224评论 0 0
  • 初识简书,是在朋友圈看到朋友发的一篇关于闹的沸沸扬扬的“魏则西事件”的文章,深有感触,遂搜了一下简书,一看简...
    镜花水月2016阅读 349评论 0 1
  • 很久没有这样的情绪了 无能为力到眼泪唰唰往下掉,即便是走在大街上。 作为一个刚毕业一年且在外打拼的学生来说,我以为...
    晶Jing_5411阅读 562评论 0 0