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

UILabel混色显示

时间: 2024-08-06 05:47:29

UILabel混色显示的相关文章

固定UILabel宽度分行显示

这种小伎俩估计都被用烂了,笔者给大家提供一个category文件,供大家简单设置哦. 各种富文本效果哦(普通文本也是可以用的呢): 3行,固定宽度200 2行,固定宽度200 无限行,固定宽度250 无限行,固定宽度250,设置段落样式 源码: UILabel+SizeToFit.h  与  UILabel+SizeToFit.m // // UILabel+SizeToFit.h // SizeToFit // // Copyright (c) 2014年 Y.X. All rights re

NSMuttableAttstring的使用(UILabel或字符串的多色显示)

在iOS开发中,常常会有一段文字显示不同的颜色和字体,或者给某几个文字加删除线或下划线的需求,而NSMuttableAttstring(带属性的字符串),上面的一些需求都可以很简便的实现. NSMutableAttributedString *AttributedStr = [[NSMutableAttributedStringalloc]initWithString:@"今天天气不错呀"]; [AttributedStr addAttribute:NSFontAttributeNam

封装一个UILabel圆形边框显示进度

封装了一个UILabel并让它显示圆形的边框,UILabel上面显示百份比,而边框则用Animation绘制到整个圆占指定百分比的点. 这只是我个人想的继承一个UILabel实现的,用到两个CAShapeLayer,第一个Layer的作用是画出灰色的背影圆圈,第二个Layer位置放置在第一个Layer的上面,并设置为红色描绘颜色并描绘到插定的位置,之后实现相应的动画效果即可. import UIKit class kCircleLabel: UILabel { var percent:Doubl

IOS开发中UILabel自适应大小显示的方法

1,根据label中的文字内容自动换行显示的方法 1>,认识两个属性 @property(nonatomic) NSInteger numberOfLines; @property(nonatomic) NSLineBreakMode lineBreakMode; 其中NSLineBreakMode为枚举类型 typedef NS_ENUM(NSInteger, NSLineBreakMode) { /* What to do with long lines */ NSLineBreakByWo

OpenGL透明与混色效果

在OpenGL中,物体透明技术通常被叫做混合(Blending). 透明是物体(或物体的一部分)非纯色而是混合色,这种颜色来自于不同浓度的自身颜色和它后面的物体颜色. 一个有色玻璃窗就是一种透明物体,玻璃有自身的颜色,但是最终的颜色包含了所有玻璃后面的颜色.这也正是混合这名称的出处,因为我们将多种(来自于不同物体)颜色混合为一个颜色,透明使得我们可以看穿物体. 透明物体可以是完全透明(它使颜色完全穿透)或者半透明的(它使颜色穿透的同时也显示自身颜色).一个物体的透明度,被定义为它的颜色的alph

swift UILabel多行显示时 计算UILable的高度(可用于UILable高度自适应)

代码如下 func heightForView(text:String, font:UIFont, width:CGFloat) -> CGFloat{ let label:UILabel = UILabel(frame: CGRect(x: 0, y: 0, width: width, height: CGFloat.greatestFiniteMagnitude)) label.numberOfLines = 0 label.lineBreakMode = NSLineBreakMode.b

PHPCMS文章列表图文混排显示

PHPCMS文章列表图文混排在模板制作中是比较常见的,也是许多门户站使用频率较多的,在多数站点中,我们经常会看到在首页调用的一列文章,第一条数据是图片,而第二条以及以后都是正常的图片显示,这里提供两种文章列表的混排方法. 第一种是有规则的文章列表混排,看如下代码: 1 2 3 4 5 6 7 8 9 10 11 {pc:content action="lists" catid="$catid" num="10" order="id DE

UILabel垂直方向显示(上下的顺序显示)。

NSString* text = @"一"; NSDictionary *attribute = @{NSFontAttributeName: [UIFont systemFontOfSize:16]}; CGSize size = [text boundingRectWithSize:CGSizeMake(1000, 90) options: NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragme

python-颜色显示

格式:\033[显示方式;字体色;背景色m......[\033[0m] ------------------------------------------- 字体色 | 背景色 | 颜色描述 ------------------------------------------- 30 | 40 | 黑色 31 | 41 | 红色 32 | 42 | 绿色 33 | 43 | 黃色 34 | 44 | 蓝色 35 | 45 | 紫红色 36 | 46 | 青蓝色 37 | 47 | 白色 --