iOS: How do you measure the width and height of a string using Quartz?

前端 未结 2 1394
甜味超标
甜味超标 2021-01-31 05:33

Before I ask my questions, this is from Apple\'s documentation re: how to determine the width of a string using Quartz:

If text measurements are important

相关标签:
2条回答
  • 2021-01-31 05:50

    When performing drawing operations on threads other than the UI thread, you must use the technique you described. This is especially important to note when using things like CATiledLayer, which performs it's rendering asynchronously on background threads, or when drawing custom graphics in the background for any other reason.

    I agree with PGB when you're doing "simple" graphics and you are guaranteed to be running on the main thread. However, sizeWithFont is doing the same thing as the technique you described - it's simply doing it for you using UIGraphicsGetCurrentContext().

    0 讨论(0)
  • 2021-01-31 06:05

    On the iPhone SDK, there's a family of methods on NSString that provide what you want.

    As of iOS 7.0, these methods are:

    - boundingRectWithSize:options:attributes:context:
    - sizeWithAttributes:
    

    On older versions of iOS we had these, now deprecated:

    – sizeWithFont:  
    – sizeWithFont:forWidth:lineBreakMode:  
    – sizeWithFont:constrainedToSize:  
    – sizeWithFont:constrainedToSize:lineBreakMode:  
    – sizeWithFont:minFontSize:actualFontSize:forWidth:lineBreakMode:
    
    0 讨论(0)
提交回复
热议问题