NSMutableAttributedString的使用

本文介绍如何在iOS应用中使用NSAttributedString实现复杂文本样式,包括颜色、字体大小和下划线等效果,并提供了绘制NSAttributedString的方法及使用CATextLayer作为UILabel替代方案。

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

在项目中要时UIlabel中显示不同字体大小的字符串,刚开始使用的是uilabel控键,但后来发现要使用NSMutableAttributedString,还没有接触过者种类型的字符串于是就学一下:
在IOS6之前需要使用NSMutableAttributedString时都需要导入:CoreText.framework框架的,但在IOS6 之后就不在需要了,其实它的创建很简单:
NSMutableAttributedString *attriString = [[NSMutableAttributedString alloc] initWithString:@"this is test!"] 
这样就创建了一个NSMutableAttributedString对象下面是对他的一些简单操作:
//把this的字体颜色变为红色 
[attriString addAttribute:(NSString *)kCTForegroundColorAttributeName 
value:[UIColor redColor].CGColor 
range:NSMakeRange(0, 4)]; 
//把is变为黄色 
[attriString addAttribute:(NSString *)kCTForegroundColorAttributeName 
value:[UIColor yellowColor].CGColor 
range:NSMakeRange(5, 2)]; 
//改变this的字体,把this改变成20号字体
[attriString addAttribute:(NSString *)kCTFontAttributeName 
value: [UIFont fontWithName:@"Arial" size:20]
range:NSMakeRange(0, 4)]; 
//给this加上下划线,value可以在指定的枚举中选择 
[attriString addAttribute:(NSString *)kCTUnderlineStyleAttributeName 
value:(id)[NSNumber numberWithInt:kCTUnderlineStyleDouble] 
range:NSMakeRange(0, 4)]; 
return attriString;
这样就算是配置好了,但由于NSAttributedString继承于NSObject,并且不支持任何draw的方法,那我们就只能自己draw了。写一个UIView的子类(假设命名为TView),在initWithFrame中把背景色设为透明(self.backgroundColor = [UIColor clearColor]),然后在重写drawRect方法:
-(void)drawRect:(CGRect)rect

[super drawRect:rect]; 
NSAttributedString *attriString = getAttributedString(); 
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextConcatCTM(ctx, CGAffineTransformScale(CGAffineTransformMakeTranslation(0, rect.size.height), 1.f, -1.f)); 
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)attriString);//前面定义的NSMutableAttributedString字符串
CGMutablePathRef path = CGPathCreateMutable(); 
CGPathAddRect(path, NULL, rect); 
CTFrameRef frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, 0), path, NULL); 
CFRelease(path); 
CFRelease(framesetter); 
CTFrameDraw(frame, ctx); 
CFRelease(frame); 
}
在代码中我们调整了CTM(current transformation matrix),这是因为Quartz 2D的坐标系统不同,比如(10, 10)到(20, 20)的直线坐标:


坐标类似于数学中的坐标,可以先不调整CTM,看它是什么样子的,下面两种调整方法是完全一样的:
CGContextConcatCTM(ctx, CGAffineTransformScale(CGAffineTransformMakeTranslation(0, rect.size.height), 1.f, -1.f));
复制代码
==
CGContextTranslateCTM(ctx, 0, rect.size.height); 
CGContextScaleCTM(ctx, 1, -1
复制代码
CTFramesetter是CTFrame的创建工厂,NSAttributedString需要通过CTFrame绘制到界面上,得到CTFramesetter后,创建path(绘制路径),然后得到CTFrame,最后通过CTFrameDraw方法绘制到界面上。
如果想要计算NSAttributedString所要的size,就需要用到这个API:
CTFramesetterSuggestFrameSizeWithConstraints,用NSString的sizeWithFont算多行时会算不准的,因为在CoreText里,行间距也是你来控制的。
设置行间距和换行模式都是设置一个属性:kCTParagraphStyleAttributeName,这个属性里面又分为很多子
属性,其中就包括
kCTLineBreakByCharWrapping
kCTParagraphStyleSpecifierLineSpacingAdjustment
设置如下:
这并不是唯一的方法,还有另一种替代方案
CATextLayer *textLayer = [CATextLayer layer]; 
textLayer.string = getAttributedString(); //得到此前实例化的NSAttributedString对象
textLayer.frame = CGRectMake(0, CGRectGetMaxY(view.frame), 200, 200); 
[self.view.layer addSublayer:textLayer];
复制代码
CATextLayer可以直接支持NSNutableAttributedString! CATsxtLayer可以直接代替UIlabel;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值