(转)iphone 字符串的多样化显示(NSAttributedString)
来源:互联网 发布:vc6.0使用教程c语言 编辑:程序博客网 时间:2024/05/16 07:02
NSAttributedString可以让我们使一个字符串显示的多样化,但是目前到iOS 5为止,好像对它支持的不是很好,因为显示起来不太方便(至少没有在OS X上方便)。
首先导入CoreText.framework,并在需要使用的文件中导入:
#import<CoreText/CoreText.h>
创建一个NSMutableAttributedString:
NSMutableAttributedString *attriString = [[[NSMutableAttributedString alloc] initWithString:@"this is test!"] autorelease];
非常常规的创建方式,接下来我们给它配置属性:
//把this的字体颜色变为红色 [attriString addAttribute:(NSString *)kCTForegroundColorAttributeName value:(id)[UIColor redColor].CGColor range:NSMakeRange(0, 4)]; //把is变为黄色 [attriString addAttribute:(NSString *)kCTForegroundColorAttributeName value:(id)[UIColor yellowColor].CGColor range:NSMakeRange(5, 2)]; //改变this的字体,value必须是一个CTFontRef [attriString addAttribute:(NSString *)kCTFontAttributeName value:(id)CTFontCreateWithName((CFStringRef)[UIFont boldSystemFontOfSize:14].fontName, 14, NULL) range:NSMakeRange(0, 4)]; //给this加上下划线,value可以在指定的枚举中选择 [attriString addAttribute:(NSString *)kCTUnderlineStyleAttributeName value:(id)[NSNumber numberWithInt:kCTUnderlineStyleDouble] range:NSMakeRange(0, 4)];
这样就算是配置好了,但是我们可以发现NSAttributedString继承于NSObject,并且不支持任何draw的方法,那我们就只能自己draw了。写一个UIView的子类(假设命名为TView),在initWithFrame中把背景色设为透明(self.backgroundColor = [UIColor clearColor]),然后在重写drawRect方法:
-(void)drawRect:(CGRect)rect{ [super drawRect:rect]; CGContextRef ctx = UIGraphicsGetCurrentContext(); CGContextConcatCTM(ctx, CGAffineTransformScale(CGAffineTransformMakeTranslation(0, rect.size.height), 1.f, -1.f)); CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)attriString); 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的坐标系统不同.
CTFramesetter是CTFrame的创建工厂,NSAttributedString需要通过CTFrame绘制到界面上,得到CTFramesetter后,创建path(绘制路径),然后得到CTFrame,最后通过CTFrameDraw方法绘制到界面上。
如果想要计算NSAttributedString所要的size,就需要用到这个API:
CTFramesetterSuggestFrameSizeWithConstraints,用NSString的sizeWithFont算多行时会算不准的,因为在CoreText里,行间距也是你来控制的。
设置行间距和换行模式都是设置一个属性:kCTParagraphStyleAttributeName,这个属性里面又分为很多子
属性,其中就包括
- kCTLineBreakByCharWrapping
- kCTParagraphStyleSpecifierLineSpacingAdjustment
//段落 //line break CTParagraphStyleSetting lineBreakMode; CTLineBreakMode lineBreak = kCTLineBreakByCharWrapping; //换行模式 lineBreakMode.spec = kCTParagraphStyleSpecifierLineBreakMode; lineBreakMode.value = &lineBreak; lineBreakMode.valueSize = sizeof(CTLineBreakMode); //行间距 CTParagraphStyleSetting LineSpacing; CGFloat spacing = 4.0; //指定间距 LineSpacing.spec = kCTParagraphStyleSpecifierLineSpacingAdjustment; LineSpacing.value = &spacing; LineSpacing.valueSize = sizeof(CGFloat); CTParagraphStyleSetting settings[] = {lineBreakMode,LineSpacing}; CTParagraphStyleRef paragraphStyle = CTParagraphStyleCreate(settings, 2); //第二个参数为settings的长度 [attributedString addAttribute:(NSString *)kCTParagraphStyleAttributeName value:(id)paragraphStyle range:NSMakeRange(0, attributedString.length)];
-----------------------------------------猥琐的分界线-----------------------------------------
这并不是唯一的方法,还有另一种替代方案:
CATextLayer *textLayer = [CATextLayer layer]; textLayer.string = getAttributedString(); textLayer.frame = CGRectMake(0, CGRectGetMaxY(view.frame), 200, 200); [self.view.layer addSublayer:textLayer];
CATextLayer可以直接支持NSAttributedString!
<a href="http://download.csdn.net/detail/zhangao0086/4340376">源码地址</a>
原文:http://blog.csdn.net/zhangao0086/article/details/7616385
by yytong
- (转)iphone 字符串的多样化显示(NSAttributedString)
- iphone 字符串的多样化显示(NSAttributedString)
- NSAttributedString使一个字符串显示的多样化
- 带属性的字符串 NSMutableAttributedString/NSAttributedString
- 带属性的字符串 NSMutableAttributedString/NSAttributedString
- 字符串截取 多样化.
- NSAttributedString 详解(属性字符串)
- NSAttributedString字符串属性
- iOS-属性字符串NSAttributedString
- label字体颜色多样化显示
- 多样化的Drawable
- NSAttributedString同控件字体的不同显示颜色
- NSAttributedString 动态计算字符串的高度ios6,7
- UIKit 带属性的字符串 NSAttributedString、NSMutableAttributedString、NSTextStorge(未完成)
- UILabel的一些常用属性、NSAttributedString属性字符串
- halcon学习备忘四(Region的选择处理与信息的多样化显示)
- halcon学习备忘四(Region的选择处理与信息的多样化显示)
- halcon学习之Region的选择处理与信息的多样化显示
- IOS push推送(javapns包与notnoop包的区别)
- VC2012换字体问题
- 什么是微营销
- altium designer设置不会产生.pcbpreview、.schpreview等的垃圾文件
- Android4.4之Keyguard--KeyguardViewManager
- (转)iphone 字符串的多样化显示(NSAttributedString)
- tuxedo以集群的方式调 ejb
- 算法系列15天速成——第三天 七大经典排序【下】
- MessageDigest简介
- hdu 3400Line belt(三分法)
- db2相关
- 在URL中,通过图片名称替换图片名称,从而实现换图片的功能
- HTML字符实体(Character Entities),转义字符串(Escape Sequence) 为什么要用转义字符串?
- win7系统破解rational rose2003找不到FlexLm License Manager的解决办法