码迷,mamicode.com
首页 > 其他好文 > 详细

创建多彩文字Label By hl

时间:2015-09-15 18:19:00      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:

一般用富文本实现多种花样的Label文字,下图是利用UILabel分类快速创建的多彩多样lable文字,快速简单,自定义性强,更重要的是无代码污染

https://github.com/joaoffcosta/UILabel-FormattedText

 

技术分享

新加2个方法

*自定义添加*/
- (void) setFont:(UIFont *)font substring:(NSString*)substring;
- (void) setFont:(UIFont *)font WithtColor:(UIColor *)textColor  substring:(NSString*)substring;

 

/**
 *  自定义添加
 */
- (void) setFont:(UIFont*)font substring:(NSString*)substring {
    NSRange range = [self.text rangeOfString:substring];
    if (range.location != NSNotFound)
    {
        [self setFont:font range:range];
    }
}


- (void) setFont:(UIFont *)font WithtColor:(UIColor *)textColor  substring:(NSString*)substring
{
    NSRange range = [self.text rangeOfString:substring];
    if (range.location != NSNotFound)
    {
        [self setFont:font range:range];
        [self setTextColor:textColor range:range];
    }
}

 

 

使用

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.view.backgroundColor=[UIColor blackColor];
    
    NSString *text=@"思埠集团于2015年1月6日正式入股本土第一家在新三板上市日化企业——幸美股份(代码830929),成为幸美最大股东。广东思埠集团直接控股新三板挂牌上市公司幸美股份,正式成为准上市公司。";
    
    
    UILabel *labelA = [[UILabel alloc] init];
    [labelA setFrame:CGRectMake(0, 15, self.view.frame.size.width,25)];
    [labelA setBackgroundColor:[UIColor clearColor]];
    [labelA setTextAlignment:NSTextAlignmentCenter];
    [labelA setText:@"Color Label"];
    [labelA setTextColor:[UIColor cyanColor]];
    [labelA setFont:[UIFont systemFontOfSize:22]];;
    [labelA setTextColor:[UIColor redColor] String:@"Color"];
    [self.view addSubview:labelA];
    
    
    
    // Colors
    UILabel *labelC = [[UILabel alloc] init];
    [labelC setFrame:CGRectMake(0, 30, self.view.frame.size.width, floorf(self.view.frame.size.height / 5))];
    [labelC setBackgroundColor:[UIColor clearColor]];
    [labelC setTextAlignment:NSTextAlignmentCenter];
    [labelC setText:text];
    [labelC setNumberOfLines:2];
    [labelC setTextColor:[UIColor yellowColor]];
    [labelC setFont:[UIFont fontWithName:@"Courier" size:14]];
    [labelC setTextColor:[UIColor whiteColor] range:NSMakeRange(20, 3)];
    [labelC setTextColor:[UIColor cyanColor] range:NSMakeRange(28, 4)];
    [labelC setTextUnderLine:[UIColor redColor] range:NSMakeRange(10, 6)];
    [self.view addSubview:labelC];
    
    // Fonts
    UILabel *labelF = [[UILabel alloc] init];
    [labelF setFrame:CGRectMake(0, floorf(self.view.frame.size.height / 4), self.view.frame.size.width, floorf(self.view.frame.size.height / 3))];
    [labelF setBackgroundColor:[UIColor clearColor]];
    [labelF setTextAlignment:NSTextAlignmentCenter];
    [labelF setText:text];
    [labelF setNumberOfLines:2];
    [labelF setTextColor:[UIColor yellowColor]];
    [labelF setFont:[UIFont fontWithName:@"Courier" size:14]];
    [labelF setTextColor:[UIColor redColor] String:@"幸美股份"];
    [labelF setFont:[UIFont systemFontOfSize:20] WithtColor:[UIColor whiteColor] substring:@"思埠集团"];
    [self.view addSubview:labelF];
    
    
    // Colors AND Fonts
    UILabel *labelCF = [[UILabel alloc] init];
    [labelCF setFrame:CGRectMake(0, floorf(self.view.frame.size.height * 2 / 4), self.view.frame.size.width, floorf(self.view.frame.size.height / 3))];
    [labelCF setBackgroundColor:[UIColor clearColor]];
    [labelCF setTextAlignment:NSTextAlignmentCenter];
    [labelCF setText:text];
    [labelCF setNumberOfLines:2];
    [labelCF setTextColor:[UIColor yellowColor]];
    [labelCF setFont:[UIFont fontWithName:@"Courier" size:14]];
    [labelCF setTextColor:[UIColor redColor] range:NSMakeRange(20, 8)];
    [labelCF setFont:[UIFont fontWithName:@"Courier-Bold" size:14] range:NSMakeRange(20, 8)];
    [labelCF setTextColor:[UIColor cyanColor] range:NSMakeRange(33, 11)];
    [labelCF setFont:[UIFont fontWithName:@"Courier-Oblique" size:14] range:NSMakeRange(33, 11)];
    [self.view addSubview:labelCF];

}

 

Demo:http://files.cnblogs.com/files/sixindev/UILabelFormattedText.zip

 

创建多彩文字Label By hl

标签:

原文地址:http://www.cnblogs.com/sixindev/p/4810903.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!