2015-06-22 64 views
0

是否可以將color更改爲CFStringRef

我想要文本red例如,默認爲black,我該如何更改?
請給我一些解決方案。先謝謝了。CFStringRef更改Objective-c中的顏色

我嘗試這樣做:

-(void)drawText:(NSString*)textToDraw inFrame:(CGRect)frameRect 
{ 

    CFStringRef stringRef = (__bridge CFStringRef)textToDraw; 
    // Prepare the text using a Core Text Framesetter 
    CFAttributedStringRef currentText = CFAttributedStringCreate(NULL, stringRef, NULL); 
    CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(currentText); 


    CGMutablePathRef framePath = CGPathCreateMutable(); 
    CGPathAddRect(framePath, NULL, frameRect); 




    CFMutableAttributedStringRef attrString = CFAttributedStringCreateMutable(kCFAllocatorDefault, 0); 
    CFAttributedStringReplaceString (attrString,CFRangeMake(0, 0), stringRef); 


    CGColorRef _red=[UIColor redColor].CGColor; 

    CFAttributedStringSetAttribute(attrString, CFRangeMake(0, [textToDraw length]),kCTForegroundColorAttributeName, _red); 



    // Get the frame that will do the rendering. 
    CFRange currentRange = CFRangeMake(0, 0); 
    CTFrameRef frameRef = CTFramesetterCreateFrame(framesetter, currentRange, framePath, NULL); 
    CGPathRelease(framePath); 

    // Get the graphics context. 
    CGContextRef currentContext = UIGraphicsGetCurrentContext(); 

    // Put the text matrix into a known state. This ensures 
    // that no old scaling factors are left in place. 
    CGContextSetTextMatrix(currentContext, CGAffineTransformIdentity); 


    // Core Text draws from the bottom-left corner up, so flip 
    // the current transform prior to drawing. 
    CGContextTranslateCTM(currentContext, 0, frameRect.origin.y*2); 
    CGContextScaleCTM(currentContext, 1.0, -1.0); 

    // Draw the frame. 
    CTFrameDraw(frameRef, currentContext); 

    CGContextScaleCTM(currentContext, 1.0, -1.0); 
    CGContextTranslateCTM(currentContext, 0, (-1)*frameRect.origin.y*2); 


    CFRelease(frameRef); 
    CFRelease(stringRef); 
    CFRelease(framesetter); 


} 

但文字是黑色

+0

請檢查http://stackoverflow.com/questions/13738149/change-color-a-part-text-of-sentence-in-ios –

+0

它不適合我 –

+0

字符串沒有顏色,你必須按照評論中的鏈接問題中的建議使用AttributedString。以前的答案不起作用? – Larme

回答

0

我已經編輯你的代碼,你的目的

-(void)drawText:(NSString*)textToDraw 
     withColor: (UIColor*) color 
     inFrame:(CGRect)frameRect 
{ 


    CFStringRef stringRef = (__bridge CFStringRef)textToDraw; 
    // Prepare the text using a Core Text Framesetter 

    CGMutablePathRef framePath = CGPathCreateMutable(); 
    CGPathAddRect(framePath, NULL, frameRect); 

    /// ATTRIBUTES FOR COLOURED STRING 
    NSDictionary *attrs = @{ NSForegroundColorAttributeName : color }; 
    NSAttributedString *attString = [[NSAttributedString alloc] initWithString:textToDraw attributes:attrs]; 

    CTFramesetterRef framesetter = 
    CTFramesetterCreateWithAttributedString((CFAttributedStringRef)attString); //3 
    CTFrameRef frameRef = 
    CTFramesetterCreateFrame(framesetter, 
          CFRangeMake(0, [attString length]), framePath, NULL); 


    // Get the graphics context. 
    CGContextRef currentContext = UIGraphicsGetCurrentContext(); 

    // Put the text matrix into a known state. This ensures 
    // that no old scaling factors are left in place. 
    CGContextSetTextMatrix(currentContext, CGAffineTransformIdentity); 


    // Core Text draws from the bottom-left corner up, so flip 
    // the current transform prior to drawing. 
    CGContextTranslateCTM(currentContext, 0, frameRect.origin.y*2); 
    CGContextScaleCTM(currentContext, 1.0, -1.0); 

    // Draw the frame. 
    CTFrameDraw(frameRef, currentContext); 

    CGContextScaleCTM(currentContext, 1.0, -1.0); 
    CGContextTranslateCTM(currentContext, 0, (-1)*frameRect.origin.y*2); 


    CFRelease(frameRef); 
    CFRelease(stringRef); 
    CFRelease(framesetter); 
} 

希望這有助於。