mirror of
https://github.com/Swiftgram/Telegram-iOS.git
synced 2025-12-22 14:20:20 +00:00
adjust font size to make text fit within constrained size
# Conflicts: # AsyncDisplayKit/ASTextNode.mm
This commit is contained in:
@@ -49,6 +49,9 @@ static NSCharacterSet *_defaultAvoidTruncationCharacterSet()
|
||||
_constrainedSize = constrainedSize;
|
||||
_attributes = attributes;
|
||||
_sizeIsCalculated = NO;
|
||||
if ([attributes.pointSizeScaleFactors count] > 0) {
|
||||
_currentScaleFactor = attributes.currentScaleFactor;
|
||||
}
|
||||
}
|
||||
return self;
|
||||
}
|
||||
@@ -84,8 +87,8 @@ static NSCharacterSet *_defaultAvoidTruncationCharacterSet()
|
||||
// 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];
|
||||
constrainedSize:shadowConstrainedSize
|
||||
textKitAttributes:attributes];
|
||||
}
|
||||
return _fontSizeAdjuster;
|
||||
}
|
||||
@@ -137,8 +140,8 @@ static NSCharacterSet *_defaultAvoidTruncationCharacterSet()
|
||||
- (void)_calculateSize
|
||||
{
|
||||
[self truncater];
|
||||
if (_attributes.minimumScaleFactor < 1 && _attributes.minimumScaleFactor > 0) {
|
||||
[[self fontSizeAdjuster] adjustFontSize];
|
||||
if ([_attributes.pointSizeScaleFactors count] > 0) {
|
||||
_currentScaleFactor = [[self fontSizeAdjuster] scaleFactor];
|
||||
}
|
||||
|
||||
// Force glyph generation and layout, which may not have happened yet (and isn't triggered by
|
||||
@@ -156,8 +159,12 @@ static NSCharacterSet *_defaultAvoidTruncationCharacterSet()
|
||||
// TextKit often returns incorrect glyph bounding rects in the horizontal direction, so we clip to our bounding rect
|
||||
// to make sure our width calculations aren't being offset by glyphs going beyond the constrained rect.
|
||||
boundingRect = CGRectIntersection(boundingRect, {.size = constrainedRect.size});
|
||||
|
||||
_calculatedSize = [_shadower outsetSizeWithInsetSize:boundingRect.size];
|
||||
CGSize boundingSize = [_shadower outsetSizeWithInsetSize:boundingRect.size];
|
||||
_calculatedSize = CGSizeMake(boundingSize.width, boundingSize.height);
|
||||
|
||||
if (_currentScaleFactor > 0.0 && _currentScaleFactor < 1.0) {
|
||||
_calculatedSize.height = ceilf(_calculatedSize.height * _currentScaleFactor);
|
||||
}
|
||||
}
|
||||
|
||||
#pragma mark - Drawing
|
||||
@@ -176,11 +183,32 @@ static NSCharacterSet *_defaultAvoidTruncationCharacterSet()
|
||||
LOG(@"%@, shadowInsetBounds = %@",self, NSStringFromCGRect(shadowInsetBounds));
|
||||
|
||||
[[self context] performBlockWithLockedTextKitComponents:^(NSLayoutManager *layoutManager, NSTextStorage *textStorage, NSTextContainer *textContainer) {
|
||||
|
||||
NSTextStorage *scaledTextStorage = nil;
|
||||
BOOL isScaled = (self.currentScaleFactor > 0 && self.currentScaleFactor < 1.0);
|
||||
|
||||
if (isScaled) {
|
||||
// if we are going to scale the text, swap out the non-scaled text for the scaled version.
|
||||
NSMutableAttributedString *scaledString = [[NSMutableAttributedString alloc] initWithAttributedString:textStorage];
|
||||
[ASTextKitFontSizeAdjuster adjustFontSizeForAttributeString:scaledString withScaleFactor:_currentScaleFactor];
|
||||
scaledTextStorage = [[NSTextStorage alloc] initWithAttributedString:scaledString];
|
||||
|
||||
[textStorage removeLayoutManager:layoutManager];
|
||||
[scaledTextStorage addLayoutManager:layoutManager];
|
||||
}
|
||||
|
||||
LOG(@"usedRect: %@", NSStringFromCGRect([layoutManager usedRectForTextContainer:textContainer]));
|
||||
NSRange glyphRange = [layoutManager glyphRangeForTextContainer:textContainer];
|
||||
NSRange glyphRange = [layoutManager glyphRangeForBoundingRect:CGRectMake(0,0,textContainer.size.width, textContainer.size.height) inTextContainer:textContainer];
|
||||
LOG(@"boundingRect: %@", NSStringFromCGRect([layoutManager boundingRectForGlyphRange:glyphRange inTextContainer:textContainer]));
|
||||
|
||||
[layoutManager drawBackgroundForGlyphRange:glyphRange atPoint:shadowInsetBounds.origin];
|
||||
[layoutManager drawGlyphsForGlyphRange:glyphRange atPoint:shadowInsetBounds.origin];
|
||||
|
||||
if (isScaled) {
|
||||
// put the non-scaled version back
|
||||
[scaledTextStorage removeLayoutManager:layoutManager];
|
||||
[textStorage addLayoutManager:layoutManager];
|
||||
}
|
||||
}];
|
||||
|
||||
UIGraphicsPopContext();
|
||||
|
||||
Reference in New Issue
Block a user