CGContextSelectFont equivalent

后端 未结 3 2213
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-12 15:53

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:

3条回答
  •  广开言路
    2021-02-12 16:37

    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.

提交回复
热议问题