iOS设计中在视图中显示滚动汉字
代码实现如下:
ViewController.m #import "ViewController.h" //导入类方法头文件 #import “BBFlashCtntLabel.h” @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; //初始化类对象 BBFlashCtntLabel *label=[[BBFlashCtntLabel alloc]init]; //背景色 label.backgroundColor=[UIColor greenColor]; //位置 label.frame=CGRectMake(50, 100, 300, 50); label.speed=BBFlashCtntSpeedMild; NSString *str=@"贵州人民欢迎您来到贵州旅游O(∩_∩)O哈哈~"; NSMutableAttributedString *att=[[NSMutableAttributedString alloc]initWithString:str]; [att addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:30] range:NSMakeRange(0, 22)];//字体大小(长度) [att addAttribute:NSBackgroundColorAttributeName value:[UIColor whiteColor] range:NSMakeRange(0, 22)];//字体背景颜色 [att addAttribute:NSForegroundColorAttributeName value:[UIColor purpleColor] range:NSMakeRange(0, 22)];//字体颜色 label .attributedText=att; [self.view addSubview:label]; }
第三方类文件:
代码如下:(以下为带三方内库代码,可以到网络上下载)
BBFlashCtntLabel.h // DebugTest // // Created by iXcoder on 15/3/2. // Copyright (c) 2015年 iXcoder. All rights reserved. // #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.m // DebugTest // // Created by iXcoder on 15/3/2. // Copyright (c) 2015年 iXcoder. All rights reserved. // #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