您的位置:首页 > 移动开发 > IOS开发

两种方式 创建富文本的代码 ios +

2012-11-25 15:46 141 查看
#import <CoreText/CoreText.h>
#import <QuartzCore/QuartzCore.h>

NSAttributedString *getAttributedString(){
    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)];
    return attriString;
}

@interface TView : UIView

@end

@implementation TView

- (id)initWithFrame:(CGRect)frame
{
    self = [super
initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor
clearColor];
    }
    return self;
}

-(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));
//    CGContextTranslateCTM(ctx, 0, rect.size.height);
//    CGContextScaleCTM(ctx, 1, -1);
    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);
}

@end

@interface BIDViewController ()

@end

@implementation BIDViewController

- (void)viewDidLoad
{
    [super
viewDidLoad];

    TView *view = [[TView
alloc] initWithFrame:CGRectMake(0,
0, 200, 200)];
    [self.view
addSubview:view];
    [view release];
    
    CATextLayer *textLayer = [CATextLayer
layer];
    textLayer.string = getAttributedString();
    textLayer.frame = CGRectMake(0,
CGRectGetMaxY(view.frame),
200, 200);
    [self.view.layer
addSublayer:textLayer];
}

- (void)viewDidUnload
{
    [super
viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation !=
UIInterfaceOrientationPortraitUpsideDown);
}

@end

转自 Created by zhangao on 5/30/12.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: