Можно ли заменить color
на CFStringRef
?
Я хочу, чтобы текст red
например, по умолчанию black
, как я могу это изменить?
Я пробовал это:
-(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);
}
Но текст черный.
red color effect
в диапазоне 0,0. Вам это кажется правильным? - person Larme   schedule 22.06.2015[textToDraw length]
? Если вы хотите раскрасить весь текст, вы должны в последней строкеCFRangeMake(0, [textToDraw length])
илиCFRangeMake(0, CFStringGetLength(stringRef)
. - person Larme   schedule 22.06.2015framesetter
, но инициализируете его с помощьюcurrentText
(у которого нет понятия красного) вместоattrString
. Может быть проблема. - person Larme   schedule 22.06.2015