I don't know if this was a good practise or not, but the following code worked for me. I handled string condition and checked it with NULL
and skipped the statement as follows.
- (void)drawText:(NSString*)textToDraw inFrame:(CGRect)frameRect fontSize:
(float)sizeofFont
{
NSInteger length = [textToDraw length];
CFStringRef string = (__bridge CFStringRef) textToDraw;
CFMutableAttributedStringRef currentText =
CFAttributedStringCreateMutable(kCFAllocatorDefault, 0);
if (string == NULL)
{
//CFAttributedStringReplaceString(nil ,range, nil);
}
else
{
CFAttributedStringReplaceString(currentText,CFRangeMake(0, 0), string); //EXC_BAD_ACCESS due to nil values of string and currentText
}
CTFontRef font = CTFontCreateWithName((CFStringRef)@"UniversLTStd-BoldCn",
sizeofFont, nil);
CFAttributedStringSetAttribute(currentText,CFRangeMake(0,
length),kCTFontAttributeName,font);
if (self.flagToSetBlueColoredText == NO)
{
CFAttributedStringSetAttribute(currentText,CFRangeMake(0, length),kCTForegroundColorAttributeName,[UIColor blackColor].CGColor);
}
else if(self.flagToSetBlueColoredText == YES)
{
CFAttributedStringSetAttribute(currentText,CFRangeMake(0, length),kCTForegroundColorAttributeName,[UIColor blueColor].CGColor);
}
CTFramesetterRef framesetter =
CTFramesetterCreateWithAttributedString(currentText);
CGMutablePathRef framePath = CGPathCreateMutable();
CGPathAddRect(framePath, NULL, frameRect);
CFRange currentRange = CFRangeMake(0, 0);
CTFrameRef frameRef = CTFramesetterCreateFrame(framesetter, currentRange,
framePath, NULL);
CGPathRelease(framePath);
CGContextRef currentContext = UIGraphicsGetCurrentContext();
CGContextSetTextMatrix(currentContext, CGAffineTransformIdentity);
CGContextTranslateCTM(currentContext, 0, frameRect.origin.y*2);
CGContextScaleCTM(currentContext, 1.0, -1.0);
CTFrameDraw(frameRef, currentContext);
CGContextScaleCTM(currentContext, 1.0, -1.0);
CGContextTranslateCTM(currentContext, 0, (-1)*frameRect.origin.y*2);
CFRelease(frameRef);
//CFRelease(currentText);
CFRelease(framesetter);
//CFRelease(stringRef);
}