CGContextSelectFont equivalent

11,758

Solution 1

As best I can understand from your code, the exact equivalent is:

CGContextSetTextDrawingMode(context, kCGTextFill); // This is the default
[[UIColor blackColor] setFill]; // This is the default
[@"Some text" drawAtPoint:CGPointMake(barX, barY)
           withAttributes:@{NSFontAttributeName:[UIFont fontWithName:@"Helvetica"
                                                                size:kBarLabelSize]
                            }];

Note that your calls to CGContextSetTextDrawingMode and CGContextSetRGBFillColor are setting the values to the defaults. Your call to CGContextSetTextMatrix is not needed when using UIKit drawing like this.

I have no idea what [barValue length] is here, however. I'm assuming that you simply incorrectly used this for the length of @"Some text". (length is not the number of bytes which is what you need. What you probably meant was [barValue lengthOfBytesUsingEncoding:NSUTF8StringEncoding]).

Note that UIKit string drawing (seen here) wraps Core Text.

Solution 2

I have found, at least in my case, the problem with the new NSString.drawAtPoint interface is that it may draw upside down, depending on how you are using the context.

An alternate is to use the Core Text methods, specifically the CTLine interface thusly:

NSDictionary *attribs = @{NSFontAttributeName:[UIFont fontWithName:@"Helvetica" size:14.0]};

NSAttributedString *fontStr = [[NSAttributedString alloc] initWithString:@"some text" attributes:attribs];

CTLineRef displayLine = CTLineCreateWithAttributedString( (__bridge CFAttributedStringRef)fontStr );
CGContextSetTextPosition( ctx, xPosition, yPosition );
CTLineDraw( displayLine, ctx );
CFRelease( displayLine );
Share:
11,758

Related videos on Youtube

rai212
Author by

rai212

Updated on June 06, 2022

Comments

  • rai212
    rai212 almost 2 years

    In iOS7 CGContextSelectFont is deprecated. Deprecation message says that I have to use Core Text, but I don't know which is the exact equivalent of this piece of code:

    CGContextSelectFont(context, "Helvetica", kBarLabelSize, kCGEncodingMacRoman);
    CGContextSetTextDrawingMode(context, kCGTextFill);
    CGContextSetRGBFillColor(context, 0, 0, 0, 1.0);
    CGContextSetTextMatrix (context, CGAffineTransformMake(1.0, 0.0, 0.0, -1.0, 0.0, 0.0));
    CGContextShowTextAtPoint(context, barX, barY, [@"Some text" cStringUsingEncoding:NSUTF8StringEncoding], [barValue length]);
    

    I've been able to create the font with this code:

    CFMutableAttributedStringRef attrStr = CFAttributedStringCreateMutable(kCFAllocatorDefault, 0);
    CTFontRef font = CTFontCreateWithName(CFSTR("Helvetica"), kBarLabelSize, NULL);
    CFAttributedStringSetAttribute(attrStr, CFRangeMake(0, CFAttributedStringGetLength(attrStr)), kCTFontAttributeName, font);
    

    But now haw can I "draw" a text with this font into the context?

  • Rob Napier
    Rob Napier over 10 years
    This is not compatible with CGContextShowTextAtPoint (which is also deprecated anyway)
  • rai212
    rai212 over 10 years
    I can't test it until now... and it works!. And yes, [barValue length] refers to @"Some text" length and yes, it was incorrectly used, I should use lengthOfBytes, but due to this works I don't realize it was wrong. Thank you very much
  • Dan Rosenstark
    Dan Rosenstark over 8 years
    Amazing and useful answer. However: the setFill is being ignored. Any hints?
  • Dan Rosenstark
    Dan Rosenstark over 8 years
    You can't use setFill, in my experience. I'm using this guy: NSDictionary *fontAsAttributes = @{NSFontAttributeName:font, NSForegroundColorAttributeName:textColor};
  • fishinear
    fishinear over 6 years
    How do you tell drawAtPoint:withAttributes: to use the given context? It looks like it uses the default one, which would likely be incorrect.
  • Rob Napier
    Rob Napier over 6 years
    @fishinear Why would the default one likely be incorrect? In most cases it's exactly what you want. If you want to change the current context, use the UIGraphics functions to set the context (UIGraphicsPushContext, UIGraphicsBeginImageContext, UIGraphicsBeginPDFContext...)
  • fishinear
    fishinear over 6 years
    Ah, cool, UIGraphicsPushContext was what I was looking for. Thanks.

Related