蘋果iOS UILabel文字跑馬燈效果

來源:互聯網
上載者:User

在實際開發過程中,我們會遇到這樣的情況,一句話太長,顯示不完全,最典型的就是導覽列顯示的標題文字,如果過長的文字就會出現顯示不完全的情況,用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 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.