Merge pull request #1091 from lkzhao/ASTextKitFontSizeAdjuster

[ASTextNode] Support iteratively shrinking font size in order to ensure no single words are wrapped.
This commit is contained in:
appleguy
2016-01-25 19:36:39 -08:00
8 changed files with 193 additions and 2 deletions

View File

@@ -15,6 +15,7 @@
#import "ASTextKitContext.h"
#import "ASTextKitShadower.h"
#import "ASTextKitTailTruncater.h"
#import "ASTextKitFontSizeAdjuster.h"
#import "ASTextKitTruncating.h"
//#define LOG(...) NSLog(__VA_ARGS__)
@@ -37,7 +38,7 @@ static NSCharacterSet *_defaultAvoidTruncationCharacterSet()
CGSize _calculatedSize;
BOOL _sizeIsCalculated;
}
@synthesize attributes = _attributes, context = _context, shadower = _shadower, truncater = _truncater;
@synthesize attributes = _attributes, context = _context, shadower = _shadower, truncater = _truncater, fontSizeAdjuster = _fontSizeAdjuster;
#pragma mark - Initialization
@@ -76,6 +77,19 @@ static NSCharacterSet *_defaultAvoidTruncationCharacterSet()
return _truncater;
}
- (ASTextKitFontSizeAdjuster *)fontSizeAdjuster
{
if (!_fontSizeAdjuster) {
ASTextKitAttributes attributes = _attributes;
// We must inset the constrained size by the size of the shadower.
CGSize shadowConstrainedSize = [[self shadower] insetSizeWithConstrainedSize:_constrainedSize];
_fontSizeAdjuster = [[ASTextKitFontSizeAdjuster alloc] initWithContext:[self context]
minimumScaleFactor:attributes.minimumScaleFactor
constrainedSize:shadowConstrainedSize];
}
return _fontSizeAdjuster;
}
- (ASTextKitContext *)context
{
if (!_context) {
@@ -122,13 +136,16 @@ static NSCharacterSet *_defaultAvoidTruncationCharacterSet()
- (void)_calculateSize
{
if (_attributes.minimumScaleFactor < 1 && _attributes.minimumScaleFactor > 0) {
[[self fontSizeAdjuster] adjustFontSize];
}
// Force glyph generation and layout, which may not have happened yet (and isn't triggered by
// -usedRectForTextContainer:).
[[self context] performBlockWithLockedTextKitComponents:^(NSLayoutManager *layoutManager, NSTextStorage *textStorage, NSTextContainer *textContainer) {
[layoutManager ensureLayoutForTextContainer:textContainer];
}];
CGRect constrainedRect = {CGPointZero, _constrainedSize};
__block CGRect boundingRect;
[[self context] performBlockWithLockedTextKitComponents:^(NSLayoutManager *layoutManager, NSTextStorage *textStorage, NSTextContainer *textContainer) {