UILabel混色顯示

來源:互聯網
上載者:User

標籤:style   class   blog   code   http   tar   

UILabel混色顯示

 

效果:

源碼:

////  RootViewController.m//  ColorLabels////  Copyright (c) 2014年 Y.X. All rights reserved.//#import "RootViewController.h"#import "YXGCD.h"@interface RootViewController ()@property (nonatomic, strong)  UIView   *upView;@property (nonatomic, strong)  UILabel  *upLabel;@property (nonatomic, strong)  UIView   *downView;@property (nonatomic, strong)  UILabel  *downLabel;@property (nonatomic, strong)  GCDTimer *timer;@end@implementation RootViewController- (void)viewDidLoad{    [super viewDidLoad];        /*
給upView的frame值做動畫才是label能夠混色顯示的核心 upView(紅色背景) ===> upLabel(白色底字) | | | | | | | | downView(白色背景) ===> downLabel(紅色底字) */ // 上面一層 { // 紅色背景 _upView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 15)]; _upView.center = self.view.center; _upView.layer.cornerRadius = 2.f; _upView.backgroundColor = [UIColor redColor]; _upView.layer.masksToBounds = YES; // 核心(不讓subview顯示超出範圍) [self.view addSubview:_upView]; // 白色底字 _upLabel = [[UILabel alloc] initWithFrame:_upView.bounds]; _upLabel.font = [UIFont fontWithName:@"HelveticaNeue-Thin" size:10.f]; _upLabel.text = @"You Xian Ming No Zuo No Die"; _upLabel.textColor = [UIColor whiteColor]; _upLabel.textAlignment = NSTextAlignmentCenter; [_upView addSubview:_upLabel]; } // 下面一層 { // 白色背景 _downView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 15)]; _downView.center = self.view.center; _downView.layer.cornerRadius = 2.f; _downView.backgroundColor = [UIColor whiteColor]; [self.view addSubview:_downView]; // 紅色底字 _downLabel = [[UILabel alloc] initWithFrame:_downView.bounds]; _downLabel.textColor = [UIColor redColor]; _downLabel.font = [UIFont fontWithName:@"HelveticaNeue-Thin" size:10.f]; _downLabel.text = @"You Xian Ming No Zuo No Die"; _downLabel.textAlignment = NSTextAlignmentCenter; [_downView addSubview:_downLabel]; } // 顯示上面一層 [self.view bringSubviewToFront:_upView]; // 給上面一層的frame值做動畫 _timer = [[GCDTimer alloc] initInQueue:[GCDQueue mainQueue]]; [_timer event:^{ [UIView animateWithDuration:0.3f animations:^{ CGRect tmpRect = _upView.frame; tmpRect.size.width = arc4random()%200; _upView.frame = tmpRect; }]; } timeInterval:NSEC_PER_SEC]; [_timer start];}@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.