Skip to content

Commit

Permalink
Clean up and source citing.
Browse files Browse the repository at this point in the history
  • Loading branch information
Tj committed Aug 13, 2015
1 parent db01c70 commit 51b3913
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions Libraries/Text/RCTText.m
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,6 @@ - (void)didMoveToWindow

- (CGRect)updateToFitFrame:(CGRect)frame
{

if ([_textStorage.string rangeOfString:@"ENTER MATCH CODE"].location != NSNotFound) {
NSLog(@"stop");
}
NSLayoutManager *layoutManager = [_textStorage.layoutManagers firstObject];
NSTextContainer *textContainer = [layoutManager.textContainers firstObject];
[textContainer setLineBreakMode:NSLineBreakByWordWrapping];
Expand All @@ -187,7 +183,8 @@ - (CGRect)updateToFitFrame:(CGRect)frame
__block BOOL hitMinimumScale = NO;
while ((requiredSize.height > CGRectGetHeight(frame) ||
requiredSize.width > CGRectGetWidth(frame) ||
(linesRequired > textContainer.maximumNumberOfLines && textContainer.maximumNumberOfLines != 0))
(linesRequired > textContainer.maximumNumberOfLines &&
textContainer.maximumNumberOfLines != 0))
&& !hitMinimumScale)
{
[_textStorage beginEditing];
Expand All @@ -200,7 +197,7 @@ - (CGRect)updateToFitFrame:(CGRect)frame
UIFont *originalFont = [_originalString attribute:NSFontAttributeName
atIndex:range.location
effectiveRange:&range];
UIFont *newFont = [font fontWithSize:font.pointSize - 1];
UIFont *newFont = [font fontWithSize:font.pointSize - .5];
if (newFont.pointSize > originalFont.pointSize * self.minimumFontScale) {
[_textStorage removeAttribute:NSFontAttributeName range:range];
[_textStorage addAttribute:NSFontAttributeName value:newFont range:range];
Expand All @@ -209,7 +206,6 @@ - (CGRect)updateToFitFrame:(CGRect)frame
}
}
}];

[_textStorage endEditing];

linesRequired = [self numberOfLinesRequired:layoutManager];
Expand All @@ -221,6 +217,8 @@ - (CGRect)updateToFitFrame:(CGRect)frame
return frame;
}

// Via Apple Text Layout Programming Guide
// https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/TextLayout/Tasks/CountLines.html
- (NSInteger)numberOfLinesRequired:(NSLayoutManager *)layoutManager
{
NSInteger numberOfLines, index, numberOfGlyphs = [layoutManager numberOfGlyphs];
Expand All @@ -234,6 +232,8 @@ - (NSInteger)numberOfLinesRequired:(NSLayoutManager *)layoutManager
return numberOfLines;
}

// Via Apple Text Layout Programming Guide
//https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/TextLayout/Tasks/StringHeight.html
- (CGSize)calculateSize:(NSTextStorage *)storage
{
NSLayoutManager *layoutManager = [storage.layoutManagers firstObject];
Expand All @@ -242,16 +242,18 @@ - (CGSize)calculateSize:(NSTextStorage *)storage
return [layoutManager usedRectForTextContainer:textContainer].size;
}

//Start fresh with the original drawn string each time, in case frame has gotten larger
- (void)resetDrawnTextStorage
{
//Start fresh with the original string each time.
[_textStorage beginEditing];
NSRange originalRange = NSMakeRange(0, _originalString.length);

[_textStorage setAttributes:@{}
range:originalRange];
NSRange originalRange = NSMakeRange(0, _originalString.length);
[_textStorage setAttributes:@{} range:originalRange];

[_originalString enumerateAttributesInRange:originalRange options:0 usingBlock:^(NSDictionary *attrs, NSRange range, BOOL *stop) {
[_originalString enumerateAttributesInRange:originalRange
options:0
usingBlock:^(NSDictionary *attrs, NSRange range, BOOL *stop)
{
[_textStorage setAttributes:attrs range:range];
}];

Expand Down

0 comments on commit 51b3913

Please sign in to comment.