https://github.com/niklasberglund/nsstring-fontsizethatfitsrect
A NSString category for getting the maximum font size a string can have to still fit within a rect
https://github.com/niklasberglund/nsstring-fontsizethatfitsrect
Last synced: 3 months ago
JSON representation
A NSString category for getting the maximum font size a string can have to still fit within a rect
- Host: GitHub
- URL: https://github.com/niklasberglund/nsstring-fontsizethatfitsrect
- Owner: niklasberglund
- License: mit
- Created: 2015-10-25T13:54:59.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2015-10-25T16:56:14.000Z (over 9 years ago)
- Last Synced: 2025-01-20T02:49:08.684Z (5 months ago)
- Language: Objective-C
- Size: 289 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# NSString-fontSizeThatFitsRect
A NSString category for getting the maximum font size a NSString can have and still fit within a rect. Useful when you want to maximize the font size and stay within the bounds of a UI element.
# Install
Add the files under `Source` to your project and do `#import "NSString+fontSizeThatFitsRect.h"` where you want to use this category.# Usage
The method definition is:
`- (CGFloat)fontSizeThatFitsRect:(CGRect)rect fontName:(NSString *)fontName`So call `fontSizeThatFitsRect:fontName:` on your NSString object and use the returned CGFloat as font size. For example:
```objectivec
NSString *fontName = @"Helvetica";
NSString *myString = @"Text here";
CGFloat fontSize = [myString fontSizeThatFitsRect:myContainerView.frame fontName:fontName];
UIFont *myFont = [UIFont fontWithName:fontName size:fontSize];
```