我有以下代码:
NSString *name = @"some text goes in here";
CFStringRef string = (__bridge CFStringRef) self.highlightItem_.comment;
CFMutableAttributedStringRef comment = CFAttributedStringCreateMutable(kCFAllocatorDefault, 0);
CFAttributedStringReplaceString (comment ,CFRangeMake(0, 0), string);
CGColorRef blue =[UIColor colorWithRed:128/255.f green:203/255.f blue:255/255.f alpha:1.0].CGColor;
CGColorRef gray =[UIColor colorWithWhite:153/255.f alpha:1.0].CGColor;
CFAttributedStringSetAttribute(comment, CFRangeMake(0, [name length]),kCTForegroundColorAttributeName, blue);
CFAttributedStringSetAttribute(comment, CFRangeMake([name length], [self.highlightItem_.comment length] - [name length]),kCTForegroundColorAttributeName, gray);
CTFontRef nameFont = CTFontCreateWithName((__bridge CFStringRef)kProximaNovaBold, 15.0f, nil);
CFAttributedStringSetAttribute(comment,CFRangeMake(0, [name length]),kCTFontAttributeName,nameFont);
CTFontRef commentFont = CTFontCreateWithName((__bridge CFStringRef)kProximaNova, 15.0f, nil);
CFAttributedStringSetAttribute(comment, CFRangeMake([name length], [self.highlightItem_.comment length] - [name length]),kCTFontAttributeName,commentFont);
CGContextSaveGState(context);
CGRect captionFrame = CGRectMake(0, 0, rect.size.width - 80, commentHeight);
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(comment);
CGMutablePathRef captionFramePath = CGPathCreateMutable();
CGPathAddRect(captionFramePath, NULL, captionFrame);
CTFrameRef mainCaptionFrame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, 0), captionFramePath, NULL);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetTextMatrix(context, CGAffineTransformIdentity);
CGContextTranslateCTM(context, 70, self.imageHeight_ + commentHeight + 20);
CGContextScaleCTM(context, 1.0, -1.0);
CTFrameDraw(mainCaptionFrame, context);
CGContextRestoreGState(context);
CFRelease(framesetter);
CGPathRelease(captionFramePath);
我不确定为什么当我在模拟器上运行它时它一切正常,但是当我在设备上运行它时,它崩溃了:
CFAttributedStringSetAttribute(comment, CFRangeMake(0, [name length]),kCTForegroundColorAttributeName, blue);
任何想法为什么?
答案 0 :(得分:1)
所以问题是我没有保留我的CGColor。如果我把CGColorRetain然后它工作
答案 1 :(得分:0)
应该不是
CFMutableAttributedStringRef comment = CFAttributedStringCreateMutable(kCFAllocatorDefault, 0);
CFAttributedStringReplaceString (comment ,CFRangeMake(0, 0), string);
读
CFMutableAttributedStringRef comment = CFAttributedStringCreateMutable(
kCFAllocatorDefault,
CFAttributedStringGetLength(string));
CFAttributedStringReplaceString(comment,
CFRangeMake(0, CFAttributedStringGetLength(string)),
string);