iOS文本的跑马显示

跑马是大家都很熟悉的一种现象(嘿嘿嘿~~~),这个不做多解释。我们会在众多的app或者网页上看到跑马显示的效果。
近期项目需要实现这样一个功能:一个展示文字的控件显示一段文字(例如一个标题),实现在左右方向上的循环滚动。为了实现这样一个小功能,我就封装了一个控件,用于实现其功能。
其具体的实现过程及代码如下:
自定义视图EntertainingDiversionsView继承于UIView

EntertainingDiversionsView .h

#import 

@interface EntertainingDiversionsView : UIView

/**
 显示字体的颜色   默认黑色
 */
@property(nonatomic,strong)UIColor *show_textColor;

/**
 显示字体的大小  默认17
 */
@property(nonatomic,assign)CGFloat show_textFontSize;

/**
 显示字体的背景颜色  默认白色
 */
@property(nonatomic,strong)UIColor *show_textBgColor;

/**
 滚动时间间隔 多长时间滚动一个像素 默认是0.01 值越小滚动的速度越快
 */
@property(nonatomic,assign)NSTimeInterval show_speedInterval;


/**
 跑马灯初始化

 @param frame 位置
 @param runSpeedInteral 滚动速度间隔(以像素点为单位)
 @return EntertainingDiversionsView
 */
- (instancetype)initWithFrame:(CGRect)frame runSpeedInteral:(CGFloat)runSpeedInteral;
/**
 运动状态下显示的文字设置

 @param showText showText description
 */
-(void)showTextInRuning:(NSString *)showText;

/**
 开始滚动
 */
-(void)run;

/**
 暂停滚动
 */
-(void)stop;
@end

EntertainingDiversionsView .m

#import "EntertainingDiversionsView.h"

@interface EntertainingDiversionsView ()

@property(nonatomic, strong)NSTimer *timer;
@property(nonatomic, strong)UILabel *showTextLabel;

@end
@implementation EntertainingDiversionsView

-(instancetype)initWithFrame:(CGRect)frame runSpeedInteral:(CGFloat)runSpeedInteral{
    EntertainingDiversionsView *entertainingDiversionsView =[self initWithFrame:frame];
    _show_speedInterval = runSpeedInteral;
    
    [self initTimer];
    return entertainingDiversionsView;
}


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.clipsToBounds = YES;//切除超出部分
    }
    return self;
}

-(UILabel *)showTextLabel{
    if (_showTextLabel == nil) {
        _showTextLabel = [[UILabel alloc]init];
        [self addSubview:_showTextLabel];
    }
    return _showTextLabel;
}

-(void)showTextInRuning:(NSString *)showText{
    self.showTextLabel.text = showText;
    NSDictionary *attribute = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:18]};
    CGSize size = [self.showTextLabel.text sizeWithAttributes:attribute];
    self.showTextLabel.frame=CGRectMake(self.frame.size.width, 0, size.width, self.frame.size.height);
}

#pragma mark------初始化计时器,并设置计时器轮式切换的速度
-(void)initTimer{
    if (_timer == nil) {
        if (_show_speedInterval == 0) {
            _timer = [NSTimer timerWithTimeInterval:0.01 target:self selector:@selector(eventWithTimeChange) userInfo:nil repeats:YES];
        }else{
            _timer = [NSTimer timerWithTimeInterval:_show_speedInterval target:self selector:@selector(eventWithTimeChange) userInfo:nil repeats:YES];
        }
        [[NSRunLoop currentRunLoop]addTimer:_timer forMode:NSDefaultRunLoopMode];
        [_timer fire];
    }
}

#pragma mark------位置改变
-(void)eventWithTimeChange{
    CGRect rect = self.showTextLabel.frame;
    rect.origin.x -= 1;
    //设置rect.origin.x < rect.origin.x < -rect.size.width - rect.size.width/4的时候重置frame,是为了内容完全展示完毕的时候 有一个时间间隔 这个根据个人情况而定
    if (rect.origin.x < -rect.size.width - rect.size.width/4) {
        rect.origin.x = self.frame.size.width;
    }
    self.showTextLabel.frame = rect;
}

#pragma mark------属性设置
-(void)setShow_textColor:(UIColor *)show_textColor{
    
    self.showTextLabel.textColor = show_textColor;
    
}

-(void)setShow_textBgColor:(UIColor *)show_textBgColor{
    
    self.showTextLabel.backgroundColor = show_textBgColor;
    
}

-(void)setShow_textFontSize:(CGFloat)show_textFontSize{
    
    self.showTextLabel.font = [UIFont boldSystemFontOfSize:show_textFontSize];
    
}
//开始滚动
-(void)run{
    [_timer setFireDate:[NSDate distantPast]];
}
//停止滚动
-(void)stop{
    [_timer setFireDate:[NSDate distantFuture]];
}
//释放占用
-(void)dealloc{
    [_timer invalidate];
    _timer = nil;
}
@end

以上是其自定义过程,其使用也是相当的简单,只需要实现:

entertainingDiversionsView = [[EntertainingDiversionsView alloc]initWithFrame:CGRectMake(20, 100, self.view.frame.size.width - 40, 40) runSpeedInteral:0.01];
[entertainingDiversionsView showTextInRuning:@"北京教培师训网络科技股份有限公司"];
[self.view addSubview:entertainingDiversionsView];

另外我们也可以设置一下跑马字体的大小,颜色,背景颜色信息

entertainingDiversionsView.show_textColor = [UIColor redColor];
entertainingDiversionsView.show_textFontSize = 20;
entertainingDiversionsView.show_textBgColor = [UIColor greenColor];

你可能感兴趣的:(iOS文本的跑马显示)