CGContextSelectFont equivalent

后端 未结 3 2166
佛祖请我去吃肉
佛祖请我去吃肉 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:26

    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 );
    

提交回复
热议问题