单一label多颜色多字体
来源:互联网 发布:贵州网络贩枪 编辑:程序博客网 时间:2024/05/21 07:53
核心算法来源:http://code4app.com/ios/单一label多颜色多字体/512439bf6803fa4124000000
调用方法如下
AttributedLabel *label = [[AttributedLabel alloc] initWithFrame:CGRectMake(20, 20, 150, 40)]; // 注意!!一定要先给text赋值,然后再加属性; label.text = @"this is test "; [self.view addSubview:label]; // 设置this为红色 [label setColor:[UIColor redColor] fromIndex:0 length:4]; // 设置is为黄色 [label setColor:[UIColor yellowColor] fromIndex:5 length:2]; // 设置this字体为加粗16号字 [label setFont:[UIFont boldSystemFontOfSize:30] fromIndex:0 length:4]; // 给this加上下划线 [label setStyle:kCTUnderlineStyleDouble fromIndex:0 length:4]; label.backgroundColor = [UIColor clearColor]; // 预计到账时间:申请通过后3个工作日内 AttributedLabel *label2 = [[AttributedLabel alloc] initWithFrame:CGRectMake(20, 120, 250, 100)]; label2.text = @"预计到账时间:申请通过后3个工作日内"; [self.view addSubview:label2]; [label2 setColor:[UIColor blueColor] fromIndex:12 length:1]; label2.backgroundColor = [UIColor clearColor];
核心算法:
#import <UIKit/UIKit.h>#import <CoreText/CoreText.h>#import <QuartzCore/QuartzCore.h>@interface AttributedLabel : UILabel{ NSMutableAttributedString *_attString;}// 设置某段字的颜色- (void)setColor:(UIColor *)color fromIndex:(NSInteger)location length:(NSInteger)length;// 设置某段字的字体- (void)setFont:(UIFont *)font fromIndex:(NSInteger)location length:(NSInteger)length;// 设置某段字的风格- (void)setStyle:(CTUnderlineStyle)style fromIndex:(NSInteger)location length:(NSInteger)length;@end
#import "AttributedLabel.h"@interface AttributedLabel(){}@property (nonatomic,retain)NSMutableAttributedString *attString;@end@implementation AttributedLabel@synthesize attString = _attString;- (void)dealloc{// [_attString release];// [super dealloc];}- (id)initWithFrame:(CGRect)frame{ self = [super initWithFrame:frame]; if (self) { // Initialization code } return self;}- (void)drawRect:(CGRect)rect{ CATextLayer *textLayer = [CATextLayer layer]; textLayer.string = _attString; textLayer.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height); [self.layer addSublayer:textLayer];}- (void)setText:(NSString *)text{ [super setText:text]; if (text == nil) { self.attString = nil; }else{ self.attString = [[NSMutableAttributedString alloc] initWithString:text]; }}// 设置某段字的颜色- (void)setColor:(UIColor *)color fromIndex:(NSInteger)location length:(NSInteger)length{ if (location < 0||location>self.text.length-1||length+location>self.text.length) { return; } [_attString addAttribute:(NSString *)kCTForegroundColorAttributeName value:(id)color.CGColor range:NSMakeRange(location, length)];}// 设置某段字的字体- (void)setFont:(UIFont *)font fromIndex:(NSInteger)location length:(NSInteger)length{ if (location < 0||location>self.text.length-1||length+location>self.text.length) { return; } [_attString addAttribute:(NSString *)kCTFontAttributeName value:(id)CFBridgingRelease(CTFontCreateWithName((CFStringRef)font.fontName, font.pointSize, NULL)) range:NSMakeRange(location, length)];}// 设置某段字的风格- (void)setStyle:(CTUnderlineStyle)style fromIndex:(NSInteger)location length:(NSInteger)length{ if (location < 0||location>self.text.length-1||length+location>self.text.length) { return; } [_attString addAttribute:(NSString *)kCTUnderlineStyleAttributeName value:(id)[NSNumber numberWithInt:style] range:NSMakeRange(location, length)];}/*// Only override drawRect: if you perform custom drawing.// An empty implementation adversely affects performance during animation.- (void)drawRect:(CGRect)rect{ // Drawing code}*/@end
0 0
- 单一label多颜色多字体
- 多标签<label>只有for属性的单一取值问题
- 单一label 多种字体 多种大小
- Label 字体颜色
- Label控件边框颜色
- Label文字多种颜色
- Label文字颜色
- label 省略号颜色改变
- ios label颜色渐变
- RaspiCam & opencv 单一颜色识别
- 如何让一个label显示多种颜色,多种字体大小,并非多个label拼接而成。
- MFC label 设置字体 颜色
- label字体颜色多样化显示
- iOS Label文字分段颜色
- iOS Label文字分段颜色
- IOS 设置label背景颜色
- 设置label高亮颜色
- label 显示 不同的颜色
- 嵌套的面板(JPanel)练习
- 黑马程序员--java基础语法下篇(day03)
- python logging动态变更输出日志文件名
- Visual Studio Enterprise 2015简体中文版(企业版)
- 小白学算法2.1——冒泡排序
- 单一label多颜色多字体
- iOS - UITableView性能优化总结
- poj Red and Black
- 如何添加Samba用户
- cocos2d-x v3.9 与ActionInterval的孩子们之间的对话(3)
- 1287: HH实习【斐波那契数列】
- C#中的反射例子及思考
- List<T> 排序
- C文件读写函数介绍(转)