How to use drawInRect:withAttributes: instead of drawAtPoint:forWidth:withFont:fontSize:lineBreakMode:baselineAdjustment: in iOS 7

后端 未结 2 801
旧巷少年郎
旧巷少年郎 2021-01-31 08:15

This method is deprecated in iOS 7.0:

drawAtPoint:forWidth:withFont:fontSize:lineBreakMode:baselineAdjustment:

Now use drawInRect:withAtt

2条回答
  •  囚心锁ツ
    2021-01-31 09:00

    You can use NSDictionary and apply attributes like this:

    NSFont *font = [NSFont fontWithName:@"Palatino-Roman" size:14.0];
    
    NSDictionary *attrsDictionary =
    
    [NSDictionary dictionaryWithObjectsAndKeys:
                                  font, NSFontAttributeName,
                                  [NSNumber numberWithFloat:1.0], NSBaselineOffsetAttributeName, nil];
    

    Use attrsDictionary as argument.

    Refer: Attributed String Programming Guide

    Refer: Standard Attributes

    SWIFT: IN String drawInRect is not available but we can use NSString instead:

    let font = UIFont(name: "Palatino-Roman", size: 14.0)
    let baselineAdjust = 1.0
    let attrsDictionary =  [NSFontAttributeName:font, NSBaselineOffsetAttributeName:baselineAdjust] as [NSObject : AnyObject]
    let str:NSString = "Hello World"
    str.drawInRect(CGRectZero, withAttributes: attrsDictionary)
    

提交回复
热议问题