Heading pretty much explains it. I have an image that I'm drawing text on. I want the text to be sized according to the size of the image and want to find a way to get a height for the font that is just a little shorter than the image itself.
相关问题
- Core Data lightweight migration crashes after App
- How can I implement password recovery in an iPhone
- State preservation and restoration strategies with
- “Zero out” sensitive String data in Swift
- Get the NSRange for the visible text after scroll
相关文章
- 现在使用swift开发ios应用好还是swift?
- UITableView dragging distance with UIRefreshContro
- TCC __TCCAccessRequest_block_invoke
- Where does a host app handle NSExtensionContext#co
- Swift - hide pickerView after value selected
- How do you detect key up / key down events from a
- didBeginContact:(SKPhysicsContact *)contact not in
- Attempt to present UIAlertController on View Contr
Note: This makes the assumption that your font size will always be smaller than the point size of the CGRect. Adjust methods as necessary to correct for that assumption.
Try this. Works for any of the 3 main methods to create a font. Should be fairly self-explanatory, but increment is the amount you wish to decrement between checks. If you don't care about exact sizing, make that number larger for greater speed. If you do care, make it smaller for higher accuracy.
From here
http://developer.apple.com/library/ios/#documentation/StringsTextFonts/Conceptual/CoreText_Programming/Operations/Operations.html
To use this, guess a point size, find it's line height (you may or may not care about leading). Then use the ratio between the answer and the height you have to scale the point size. I don't think you're guaranteed that the height will be exactly right -- if you care about it being exact, you have to iterate until it's close enough (use the new size as the new guess).
OK, so for everyone who thinks an iteration is not avoidable:
desiredFontSize
will contain the font size in points of which the height is exactly the same as the height of the specified rectangle. You may want to multiply it by, say, 0.8 to make the font a bit smaller than the rect's actual size to make it look good.