创建多彩文字Label By hl

一般用富文本实现多种花样的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

时间: 2024-08-01 00:09:29

创建多彩文字Label By hl的相关文章

python Tkinter 基础 创建一行文字的窗体并设置窗体的标题 Label,title

# Tkinter 基础 创建一行文字的窗体并设置窗体的标题 Label,title import tkinter as tk app = tk.Tk() # 实例化一个TK 用于容纳整个GUI程序 app.title("test01") # 设置窗体的标题栏 # 设置label主键 , 显示文本,图标与图片 theLabel = tk.Label(app, text = "这是一个窗口") theLabel.pack() # 自动调节 主键的尺寸与位置 # 窗口的

利用CSS的Clip属性来创造多彩文字

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> &l

threejs创建三维文字TextGeometry

THREE.js 封装了 TextGeometry 类可以很容易地生成三维文字 TextGeometry(text : String, parameters : Object) 参数说明 text — The text that needs to be shown. (要显示的字符串) parameters — Object that can contains the following parameters. font — an instance of THREE.Font.(字体格式) si

Android TextView设置多彩文字

在应用开发中时常会遇到需要在一段文字中插入几个不一样颜色文字的需求; 以前本人都是用多个TextView拼起来,不仅感觉很蠢,操作起来也蛮恶心; 直到接触到了SpannableStringBuilder,感觉整个人都好了; 在我搭建界面布局,会有一些带String占位符的默认文字,如:"现在的气温是:%s","今天天气:%1$s,最高气温:%2$s,最低气温:%3$s,降雨率:%4$s,pm2.5:%5$s."; 之后在获取到数据时,直接String.format(

Cocos2D创建多彩文本显示标签

大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) Cocos2D中默认的CCLableTTF类从源代码里看是支持多彩以及粗体之类的字符显示属性的,但可惜的是在最新的几版的cocos2D里,只能使用单色属性,否则App就会崩溃. 在Github中cocos2D的源代码里,可以看到各位大牛已经发现了这个问题,但是直到目前最新的Cocos2D 3.4.9中这个问题还未解决: [iOS] Attributed Str

js创建div input label span

var newdiv=document.createElement("div");            newdiv.className="switch-animate switch-on"            newdiv.id="switch_div"            document.getElementById("dhcpd_div").appendChild(newdiv);                

安卓开发之BaseAdapter用法举例,创建图形文字混合列表项

baseAdapter的用法    1.创建一个数组资源类GeneralBean package com.example.hoyin0211.entry; public class GeneralBean {    private int resid;    private String name;    @Override    public String toString() {        return "GeneralBean [resid=" + resid + "

label 根据文字label自适应宽度

UILabel *Label = [[UILabel alloc]init]; Label.text = @"长一点也是可以的"; Label.textColor = black_Color; Label.textAlignment = NSTextAlignmentLeft; gsmcLabel.font = [UIFont systemFontOfSize:12]; CGSize size = [Label.text sizeWithAttributes:[NSDictionary

NSAttributeString创建各种文字效果

[objc] view plain copy NSDictionary *attributes [email protected]{ NSForegroundColorAttributeName: [UIColorredColor], NSFontAttributeName: [UIFontfontWithName:@"Zapfino" size:16.0] }; NSString *strDisplayText =@"This is an attributed string