ios Label TextFile 文本来回滚动 包括好用的三方

通常显示不够了,比如八个字。只能显示6个字 。产品要求 来回滚动 

下面有两种 方法 : 一种UIScrollView  一种 View动画  如果不能满足你  请点击这个  https://github.com/cbpowell/MarqueeLabel  好用的第三方

- (void)viewDidLoad
{
	[super viewDidLoad];
	self.view.backgroundColor = [UIColor blackColor];

	// 注册字体
	REGISTER_FONT(bundleFont(@"新蒂小丸子体.ttf"), @"新蒂小丸子体");
	
	// 获取文本
	NSString *string = @"  喜欢这首情思幽幽的曲子,仿佛多么遥远,在感叹着前世的情缘,又是那么柔软,在祈愿着来世的缠绵。《莲的心事》,你似琉璃一样的晶莹,柔柔地拨动我多情的心弦。我,莲的心事,有谁知?我,莲的矜持,又有谁懂?  ";
	
	// 初始化label
	UILabel *label	  = [UILabel new];
	label.text		  = string;
	label.numberOfLines = 0;
	label.textColor	 = [UIColor cyanColor];
	label.font		  = [UIFont fontWithName:CUSTOM_FONT(@"新蒂小丸子体", 0)
										  size:20.f];
	
	// 计算尺寸
	CGSize size		 = [label boundingRectWithSize:CGSizeMake(0, 0)];
	label.frame		 = (CGRect){CGPointZero, size};

	// 初始化ScrollView
	UIScrollView *showView = 
		[[UIScrollView alloc] initWithFrame:CGRectMake(0, 90, 320, size.height)];
	showView.contentSize   = size;
	showView.showsHorizontalScrollIndicator = NO;
	[showView addSubview:label];
	[self.view addSubview:showView];
	
	// 形成边缘的遮罩
	UIImageView *imageView = 
	[[UIImageView alloc] initWithFrame:CGRectMake(0, 90, 320, size.height)];
	imageView.image = [UIImage imageNamed:@"bg"];
	[self.view addSubview:imageView];
	
	// 动画
	[UIView animateKeyframesWithDuration:10
								   delay:7
								 options:UIViewKeyframeAnimationOptionAllowUserInteraction
							  animations:^{
								  // 计算移动的距离
								  CGPoint point = showView.contentOffset;
								  point.x = size.width - 320.f;
								  showView.contentOffset = point;
							  }
							  completion:^(BOOL finished) {
								  
							  }];
}

第二种 简洁版
#import "Ridehorselight.h"

@interface Ridehorselight ()

@property (nonatomic, strong) NSTimer *timer; //定时器
@property (nonatomic, weak) UIView *viewAnima; //Label的背景
@property (nonatomic, weak) UILabel *customLabel; //Label

@end

@implementation Ridehorselight

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    [self addClildView];
    [self setTimer];
}

- (void)addClildView {

    self.view.backgroundColor = [UIColor whiteColor];

    CGFloat viewX = (self.view.frame.size.width-200)/2;
    UIView *viewAnima = [[UIView alloc] initWithFrame:CGRectMake(viewX, 100, 200, 40)];
    viewAnima.backgroundColor = [UIColor  yellowColor];
    self.viewAnima = viewAnima;
    self.viewAnima.clipsToBounds = YES; //剪掉超出view范围部分

    CGFloat customLabelY = (self.viewAnima.frame.size.height-30)/2;
    UILabel *customLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.viewAnima.frame.size.width, customLabelY, 200, 30)];
    customLabel.text = @"跑马灯效果,滚动文本!";
    customLabel.textColor = [UIColor redColor];
    self.customLabel = customLabel;

    [self.view addSubview:viewAnima];
    [viewAnima addSubview:customLabel];
}

- (void)setTimer {

    self.timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(changePos) userInfo:nil repeats:YES];
}

- (void)changePos {

    CGPoint cenPos = self.customLabel.center;
    if (cenPos.x < -100) {

        CGFloat distance = self.customLabel.frame.size.width/2;
        self.customLabel.center = CGPointMake(self.viewAnima.frame.size.width+distance, 20);
    } else {
        self.customLabel.center = CGPointMake(cenPos.x-5, 20);
    }
}

 

 

原文地址:https://www.cnblogs.com/tangyuanby2/p/7122184.html