iOS7's deprecation of NSString's drawAtPoint:forWidth:withFont:minFontSize:actualFontSize:lineBreakMode:baselineAdjustment:

Rob

With iOS7's release, the following function has been deprecated:

drawAtPoint:forWidth:withFont:minFontSize:actualFontSize:lineBreakMode:baselineAdjustment:

In Apple's documentation it suggests to use

drawInRect:withAttributes:

The reason I use this function is because of the <code>minFontSize</code> parameter, which lets me draw a string inside a rect.

If the text won't fit, it will first shrink the text size to <code>minFontSize</code> and then if it doesn't fit, it will truncate it.

I am unable to accomplish this so far using <code>drawInRect:withAttributes:</code>.

Which key I can use to determine the <code>minFontSize</code> equivalent?

Florian

It is a little more complicated than before and you cannot use a minimum font size, but have to use minimum font scale factor. There is also a bug in the iOS SDK, which breaks it for most use cases (see notes at the bottom). Here is what you have to do:

// Create text attributes
NSDictionary *textAttributes = @{NSFontAttributeName: [UIFont systemFontOfSize:18.0]};

// Create string drawing context
NSStringDrawingContext *drawingContext = [[NSStringDrawingContext alloc] init];
drawingContext.minimumScaleFactor = 0.5; // Half the font size

CGRect drawRect = CGRectMake(0.0, 0.0, 200.0, 100.0);
[string drawWithRect:drawRect
             options:NSStringDrawingUsesLineFragmentOrigin
          attributes:textAttributes
             context:drawingContext];

Notes:

  • There seems to be a bug in the iOS 7 SDK at least up to version 7.0.3: If you specify a custom font in the attributes, the miniumScaleFactor is ignored. If you pass nil for the attributes, the text is scaled correctly.

  • The NSStringDrawingUsesLineFragmentOrigin option is important. It tells the text drawing system, that the drawing rect's origin should be at the top left corner.

  • There is no way to set the baselineAdjustment using the new method. You would have to do that yourself by calling boundingRectWithSize:options:attributes:context: first and then adjusting the rect before you pass it to drawWithRect:options:attributes:context.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

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

From Dev

Replace the deprecation sizeWithFont:minFontSIze:actualFontSize in ios 7

From Dev

Replace the deprecation sizeWithFont:minFontSIze:actualFontSize in ios 7

From Dev

Replacement for sizeWithFont:ForWidth:lineBreakMode:

From Dev

How to set NSString's background cornerRadius on iOS7

From Dev

iOS7 UIImage drawAtPoint not retina

From Dev

sizeWithFont:constrainedToSize:lineBreakMode: deprecated in iOS7

From Dev

Chrome's Deprecation of the NPAPI & FireBreath

From Dev

iOS - get modal controller's parent controller in iOS7

From Dev

Is ios7 UITableViewController's Clear on Appearance broken (from Storyboard)?

From Dev

What are iOS7's dynamic type CSS classes?

From Dev

how to remove splitviewcontroller's gesture iOS7

From Dev

Remove Navigationbar's bottom border iOS7

From Dev

IOS7 style blur UICollectionViewCell's view on tap

From Dev

UiStepper IOS7 Remove Border it's possible?

From Dev

Change MFMailComposeViewController's UINavigationBar color in iOS7

From Dev

iOS7's drawViewHierarchyInRect doesn't work?

From Dev

Show UISearchDisplayController's searchResultsTableView when empty text on ios7

From Dev

UIButton's Selected State not working in iOS7

From Dev

Resize UItextview according to it's content in iOS7

From Dev

Apple's controllers not presenting properly in iOS7

From Dev

Attaching JSON image to SLComposeViewController's tweetSheet (ios7)

From Dev

IOS7: UITabBarController's "more" tab design is is disturbed

From Dev

Is ios7 UITableViewController's Clear on Appearance broken (from Storyboard)?

From Dev

Converting NSString to Epoch timestamp iOS7

From Dev

NSString to NSDate issue IOS7

From Dev

Converting NSString to Epoch timestamp iOS7

From Dev

How to handle anorm's Pk deprecation

From Dev

Spring's Annotation Type Required deprecation

Related Related

  1. 1

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

  2. 2

    Replace the deprecation sizeWithFont:minFontSIze:actualFontSize in ios 7

  3. 3

    Replace the deprecation sizeWithFont:minFontSIze:actualFontSize in ios 7

  4. 4

    Replacement for sizeWithFont:ForWidth:lineBreakMode:

  5. 5

    How to set NSString's background cornerRadius on iOS7

  6. 6

    iOS7 UIImage drawAtPoint not retina

  7. 7

    sizeWithFont:constrainedToSize:lineBreakMode: deprecated in iOS7

  8. 8

    Chrome's Deprecation of the NPAPI & FireBreath

  9. 9

    iOS - get modal controller's parent controller in iOS7

  10. 10

    Is ios7 UITableViewController's Clear on Appearance broken (from Storyboard)?

  11. 11

    What are iOS7's dynamic type CSS classes?

  12. 12

    how to remove splitviewcontroller's gesture iOS7

  13. 13

    Remove Navigationbar's bottom border iOS7

  14. 14

    IOS7 style blur UICollectionViewCell's view on tap

  15. 15

    UiStepper IOS7 Remove Border it's possible?

  16. 16

    Change MFMailComposeViewController's UINavigationBar color in iOS7

  17. 17

    iOS7's drawViewHierarchyInRect doesn't work?

  18. 18

    Show UISearchDisplayController's searchResultsTableView when empty text on ios7

  19. 19

    UIButton's Selected State not working in iOS7

  20. 20

    Resize UItextview according to it's content in iOS7

  21. 21

    Apple's controllers not presenting properly in iOS7

  22. 22

    Attaching JSON image to SLComposeViewController's tweetSheet (ios7)

  23. 23

    IOS7: UITabBarController's "more" tab design is is disturbed

  24. 24

    Is ios7 UITableViewController's Clear on Appearance broken (from Storyboard)?

  25. 25

    Converting NSString to Epoch timestamp iOS7

  26. 26

    NSString to NSDate issue IOS7

  27. 27

    Converting NSString to Epoch timestamp iOS7

  28. 28

    How to handle anorm's Pk deprecation

  29. 29

    Spring's Annotation Type Required deprecation

HotTag

Archive