NSMutableAttributedString常规用法

首先导入CoreText.framework,并在需要使用的文件中导入:


#import
创建一个NSMutableAttributedString:
创建方式:
NSMutableAttributedString *attriString = [[[NSMutableAttributedString alloc] initWithString:@"this is string!”]   
                                              autorelease];  
配置属性:
把this的字体颜色变为黄色  
[attriString addAttribute:(NSString *)kCTForegroundColorAttributeName  
                    value:(id)[UIColor yellowColor].CGColor   
                    range:NSMakeRange(0, 4)];  
把is变为红色  
[attriString addAttribute:(NSString *)kCTForegroundColorAttributeName  
                    value:(id)[UIColor redColor].CGColor   
                    range:NSMakeRange(5, 2)];  
改变this的字体  
[attriString addAttribute:(NSString *)kCTFontAttributeName  
                    value:(id)CTFontCreateWithName((CFStringRef)[UIFont boldSystemFontOfSize:14].fontName,  
                                                   14,   
                                                   NULL)  
                    range:NSMakeRange(0, 4)];  
给this加上下划线 
[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);  
    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);  
}  




CGContextConcatCTM(ctx, CGAffineTransformScale(CGAffineTransformMakeTranslation(0, rect.size.height), 1.f, -1.f));  


等价于:


CGContextTranslateCTM(ctx, 0, rect.size.height);  
CGContextScaleCTM(ctx, 1, -1);  


计算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!

你可能感兴趣的:(Objective-C)