自定义Label,设置关键字的颜色

本文介绍如何创建一个自定义UILabel,以便在文本中指定关键字并为其设置独特的颜色,达到高亮显示的效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

下面是一个自定义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)deall
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值