一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

苹果iOS UILabel文字跑马灯效果

时间:2022-06-25 23:36:32 编辑:袖梨 来源:一聚教程网

在实际开发过程中,我们会遇到这样的情况,一句话太长,显示不完全,最典型的就是导航栏显示的标题文字,如果过长的文字就会出现显示不完全的情况,用UILabel可以实现跑马灯的效果,将文字展示完整。具体代码如下:

 代码如下 复制代码

#pragma mark - 动画
-(void)startAnimationIfNeeded{
    //取消、停止所有的动画
    [titleLabel.layer removeAllAnimations]; //这里的titleLabel就是要实现跑马灯文字的label
    
    CGSize size = CGSizeMake(320,2000); //设置一个行高上限
    
//    CGSize textSize = [titleLabel.text sizeWithFont:titleLabel.font];
    NSDictionary *attribute = @{NSFontAttributeName: titleLabel.font};
    CGSize textSize = [titleLabel.text boundingRectWithSize:size options: NSStringDrawingTruncatesLastVisibleLine attributes:attribute context:nil].size;
 
    CGRect lframe = titleLabel.frame;
    lframe.size.width = textSize.width;
    titleLabel.frame = lframe;
    const float oriWidth = 10;
    if (textSize.width > oriWidth) {
        float offset = textSize.width - oriWidth;
        [UIView animateWithDuration:18.0
                              delay:0
                            options:
         UIViewAnimationOptionRepeat //动画重复的主开关
//         |UIViewAnimationOptionAutoreverse //动画重复自动反向,需要和上面这个一起用
         | UIViewAnimationOptionCurveLinear //动画的时间曲线,滚动字幕线性比较合理
                         animations:^{
                             titleLabel.transform = CGAffineTransformMakeTranslation(- offset - 400, 0);
                         }
                         completion:^(BOOL finished) {
                             
                         }
         ];
    }
}

注:跑动的距离宽度可根据自己的需要对方法进行修改,动画跑动的速度也可以修改的,效果看起来还不错。

再补充一篇

 代码如下 复制代码

- (void)viewDidLoad {    
    UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake(50.0, 20.0, 200.0, 50.0)];    
    UILabel *label2 = [[UILabel alloc]initWithFrame:CGRectMake(50.0, 80.0, 200.0, 50.0)];    
    UILabel *label3 = [[UILabel alloc]initWithFrame:CGRectMake(50.0, 140.0, 200.0, 50.0)];    
    UILabel *label4 = [[UILabel alloc]initWithFrame:CGRectMake(50.0, 200.0, 200.0, 50.0)];    
    UILabel *label5 = [[UILabel alloc]initWithFrame:CGRectMake(50.0, 260.0, 200.0, 50.0)];    
    UILabel *label6 = [[UILabel alloc]initWithFrame:CGRectMake(50.0, 320.0, 200.0, 50.0)];    
    UILabel *label7 = [[UILabel alloc]initWithFrame:CGRectMake(50.0, 380.0, 200.0, 50.0)];    
    //设置显示文字    
    label1.text = @"label1";    
    label2.text = @"label2";    
    label3.text = @"label3--label3--label3--label3--label3--label3--label3--label3--label3--label3--label3--";    
    label4.text = @"label4--label4--label4--label4--";    
    label5.text = @"label5--label5--label5--label5--label5--label5--";    
    label6.text = @"label6";    
    label7.text = @"label7";    
    //设置字体:粗体,正常的是 SystemFontOfSize    
    label1.font = [UIFont boldSystemFontOfSize:20];    
    //设置文字颜色 
    label1.textColor = [UIColor orangeColor];    
    label2.textColor = [UIColor purpleColor];    
    //设置文字位置    
    label1.textAlignment = UITextAlignmentRight;    
    label2.textAlignment = UITextAlignmentCenter;    
    //设置字体大小适应label宽度    
    label4.adjustsFontSizeToFitWidth = YES;    
 

    //设置label的行数    
    label5.numberOfLines = 2;   
    UIlabel.backgroudColor=[UIColor clearColor]; //可以去掉背景色  

 

    //设置高亮    
    label6.highlighted = YES;    
    label6.highlightedTextColor = [UIColor orangeColor];    
    //设置阴影    
    label7.shadowColor = [UIColor redColor];    
    label7.shadowOffset = CGSizeMake(1.0,1.0);    
    //设置是否能与用户进行交互    

    label7.userInteractionEnabled = YES;    
    //设置label中的文字是否可变,默认值是YES    
    label3.enabled = NO;    
    //设置文字过长时的显示格式    

    label3.lineBreakMode = UILineBreakModeMiddleTruncation;//截去中间    
//  typedef enum {    
//      UILineBreakModeWordWrap = 0,    
//      UILineBreakModeCharacterWrap,    
//      UILineBreakModeClip,//截去多余部分    
//      UILineBreakModeHeadTruncation,//截去头部    
//      UILineBreakModeTailTruncation,//截去尾部    
//      UILineBreakModeMiddleTruncation,//截去中间    
//  } UILineBreakMode;    
    //如果adjustsFontSizeToFitWidth属性设置为YES,这个属性就来控制文本基线的行为    
    label4.baselineAdjustment = UIBaselineAdjustmentNone;    
//  typedef enum {    
//      UIBaselineAdjustmentAlignBaselines,    
//      UIBaselineAdjustmentAlignCenters,    
//      UIBaselineAdjustmentNone,    
//  } UIBaselineAdjustment;    
    [self.view addSubview:label1];    
    [self.view addSubview:label2];    
    [self.view addSubview:label3];    
    [self.view addSubview:label4];    
    [self.view addSubview:label5];    
    [self.view addSubview:label6];    
    [self.view addSubview:label7];    
    [label1 release];    
    [label2 release];    
    [label3 release];    
    [label4 release];    
    [label5 release];    
    [label6 release];    
    [label7 release];    
    [super viewDidLoad];    
}    
/*  
 // Override to allow orientations other than the default portrait orientation.  
 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {  
 // Return YES for supported orientations  
 return (interfaceOrientation == UIInterfaceOrientationPortrait);  
 }  
 */   
- (void)didReceiveMemoryWarning {    
    // Releases the view if it doesn't have a superview.    
    [super didReceiveMemoryWarning];    
    // Release any cached data, images, etc that aren't in use.    
}    
- (void)viewDidUnload {    
    // Release any retained subviews of the main view.    
    // e.g. self.myOutlet = nil;    
}    
- (void)dealloc {    
    [super dealloc];    
}    
@end 

热门栏目