自定义Label,设置关键字的颜色
来源:互联网 发布:windows10共享设置软件 编辑:程序博客网 时间:2024/05/15 23:43
下面是一个自定义UILabel ,通过他,可以给Label中的关键字设置颜色
#import <UIKit/UIKit.h>#import <CoreText/CoreText.h>#import <QuartzCore/QuartzCore.h>@interface CustomLabel : UILabel{ NSMutableAttributedString *_mattString;}//针对某节文字设置单一颜色-(void)setSubTextColorArray:(NSArray*)colorArray subTextPositionRangeArray:(NSArray*)subTextPositionRangeArray;//针对某节文字设置单一颜色-(void)setSubTextColor:(UIColor*)color subTextPositionRangeArray:(NSArray*)subTextPositionRangeArray;// 设置某段字的颜色- (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 "CustomLabel.h"@implementation CustomLabel- (void)dealloc{ [_mattString release]; [super dealloc];}- (id)initWithFrame:(CGRect)frame{ self = [super initWithFrame:frame]; if (self) { // Initialization code } return self;}- (void)drawRect:(CGRect)rect{ if (self.tag==10) { [super drawRect:rect]; } else{ CATextLayer *textLayer = [CATextLayer layer]; textLayer.string = _mattString; textLayer.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height); NSArray* arr=[NSArray arrayWithObject:textLayer]; [self.layer setSublayers:arr]; }}- (void)setText:(NSString *)text{ [super setText:text]; if (text == nil) { [_mattString release]; _mattString = nil; }else{ _mattString = [[[[NSMutableAttributedString alloc] initWithString:text]retain] autorelease]; }}//针对某节文字设置单一颜色-(void)setSubTextColorArray:(NSArray*)colorArray subTextPositionRangeArray:(NSArray*)subTextPositionRangeArray{ if ([colorArray count]==[subTextPositionRangeArray count]) { for (int i=0; i<[colorArray count]; i++) { NSRange tempRange=((NSValue*)[subTextPositionRangeArray objectAtIndex:i]).rangeValue; [self setColor:[colorArray objectAtIndex:i] fromIndex:tempRange.location length:tempRange.length]; } } else { NSLog(@"ERROR:colorArray count!=subTextPositionRangeArray count"); }}//针对某节文字设置单一颜色-(void)setSubTextColor:(UIColor*)color subTextPositionRangeArray:(NSArray*)subTextPositionRangeArray{ for (NSValue* tempVale in subTextPositionRangeArray) { NSRange tempRange=tempVale.rangeValue; [self setColor:color fromIndex:tempRange.location length:tempRange.length]; }}// 设置某段字的颜色- (void)setColor:(UIColor *)color fromIndex:(NSInteger)location length:(NSInteger)length{ if (location < 0||location>self.text.length-1||length+location>self.text.length) { return; } [_mattString 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; } [_mattString addAttribute:(NSString *)kCTFontAttributeName value:(id)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; } [_mattString addAttribute:(NSString *)kCTUnderlineStyleAttributeName value:(id)[NSNumber numberWithInt:style] range:NSMakeRange(location, length)];}
- 自定义Label,设置关键字的颜色
- 自定义的Label,可以设置同一Label字体的大小不同,颜色不一
- 自定义Label指定范围的字体颜色
- iOS label设置不同的字体 颜色
- 设置Gtk Label的背景颜色和字体颜色
- 170325 PyQt5-Label背景颜色和文本颜色的设置
- 实现自定义label上 指定文字的颜色
- MFC label 设置字体 颜色
- IOS 设置label背景颜色
- 设置label高亮颜色
- iOS Label分段设置颜色
- delphi label 背景颜色设置!
- 自定义背景颜色的设置
- 自定义设置hr的颜色
- stm32_009_MDK如何自定义某关键字的颜色
- 如何动态设置label控件的字体颜色属性
- iOS 设置Label中特定的文字大小和颜色
- iOS 中为同一个label设置不同的颜色
- hdu 2509 Be the Winner(博弈)
- 深入理解C++的动态绑定和静态绑定
- Hibernate的fetch (转)
- 寻找Bug记(一)
- 用VC操作WORD
- 自定义Label,设置关键字的颜色
- (21) - 简要知识点(一) (File类,Properties类,基本数据类型的包装类)
- qt5.0.2连接mysql
- SQL Server 限制IP登陆
- ADSGouraud.vp
- ADSGouraud.fp
- ecmall 挂件显示方法
- 怎样让文件不被移动,删除,复制
- 野指针