Skip to content

Commit

Permalink
Some cleanup and code hygiene.
Browse files Browse the repository at this point in the history
  • Loading branch information
odrobnik committed May 21, 2011
1 parent 96d7c45 commit 2eb5dbb
Showing 1 changed file with 53 additions and 27 deletions.
80 changes: 53 additions & 27 deletions Classes/DTAttributedTextContentView.m
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ @interface DTAttributedTextContentView ()
@property (nonatomic, retain) NSMutableDictionary *customViewsForLinksIndex;
@property (nonatomic, retain) NSMutableDictionary *customViewsForAttachmentsIndex;

- (void)removeAllCustomViews;
- (void)removeSubviewsOutsideRect:(CGRect)rect;
- (void)removeAllCustomViewsForLinks;


@end

Expand Down Expand Up @@ -54,7 +57,7 @@ @implementation DTAttributedTextContentView

- (void)setup
{
self.contentMode = UIViewContentModeRedraw; // to avoid bitmap scaling effect on resize
self.contentMode = UIViewContentModeTopLeft; // to avoid bitmap scaling effect on resize

// possibly already set in NIB
if (!self.backgroundColor)
Expand Down Expand Up @@ -304,6 +307,10 @@ - (void)layoutSubviews
if (shouldOnlyLayoutVisibleSubviews)
{
CGRect visibleRect = CGRectIntersection([self convertRect:self.window.frame fromView:self.window], self.bounds);

// Ignore horizontal movement, i.e. paging
visibleRect.origin.x = 0;
visibleRect.size.width = self.bounds.size.width;

[self removeSubviewsOutsideRect:visibleRect];
[self layoutSubviewsForRect:visibleRect];
Expand Down Expand Up @@ -345,10 +352,12 @@ - (CGSize)sizeThatFits:(CGSize)size
size.width = self.bounds.size.width;
}

CGSize neededSize = [self.layouter suggestedFrameSizeToFitEntireStringConstraintedToWidth:size.width-edgeInsets.left-edgeInsets.right];
CGSize neededSize = CGSizeMake(size.width, CGRectGetMaxY(self.layoutFrame.frame) + edgeInsets.bottom);

// increase by edge insets
return CGSizeMake(size.width, ceilf(neededSize.height+edgeInsets.top+edgeInsets.bottom));
// this returns an incorrect size before 4.2
// CGSize neededSize = [self.layouter suggestedFrameSizeToFitEntireStringConstraintedToWidth:size.width-edgeInsets.left-edgeInsets.right];

return neededSize;
}

- (NSString *)description
Expand All @@ -365,19 +374,24 @@ - (NSString *)description

- (void)relayoutText
{
// remove custom views
[self.customViews makeObjectsPerformSelector:@selector(removeFromSuperview)];
self.customViews = nil;

CGSize neededSize = [self sizeThatFits:CGSizeZero];

// set frame to fit text preserving origin
self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, neededSize.width, neededSize.height);

// need new layouter
self.layouter = nil;
self.layoutFrame = nil;

// remove custom views
[self removeAllCustomViewsForLinks];

if (_attributedString)
{
// triggers new layout
CGSize neededSize = [self sizeThatFits:self.bounds.size];

// set frame to fit text preserving origin
self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, neededSize.width, neededSize.height);
}

[self setNeedsDisplay];
[self setNeedsLayout];
}

- (void)removeAllCustomViewsForLinks
Expand Down Expand Up @@ -456,20 +470,21 @@ - (void)setAttributedString:(NSAttributedString *)string

- (void)setFrame:(CGRect)frame
{
CGRect previousFrame = self.frame;

// need to remove otherwise some get wrong positions
// [self removeAllCustomViews];

[super setFrame:frame];

if (!_layoutFrame)
{
return;
}

CGSize sizeThatFits = [self sizeThatFits:self.bounds.size];

if (!CGSizeEqualToSize(frame.size, previousFrame.size) && !CGRectIsEmpty(frame) && !(frame.size.height<0))
if (!CGSizeEqualToSize(frame.size, sizeThatFits))
{
// if we have a layouter then it can create a new layoutFrame for us on redraw
if (_layouter)
{
// next redraw will do new layout
self.layoutFrame = nil;
// layouter means we are responsible for layouting yourselves
[self relayoutText];
}
}
}
Expand Down Expand Up @@ -501,9 +516,12 @@ - (void)setBackgroundColor:(UIColor *)newColor

- (DTCoreTextLayouter *)layouter
{
if (!_layouter && _attributedString)
if (!_layouter)
{
_layouter = [[DTCoreTextLayouter alloc] initWithAttributedString:_attributedString];
if (_attributedString)
{
_layouter = [[DTCoreTextLayouter alloc] initWithAttributedString:_attributedString];
}
}

return _layouter;
Expand All @@ -513,9 +531,15 @@ - (DTCoreTextLayoutFrame *)layoutFrame
{
if (!_layoutFrame)
{
CGRect rect = UIEdgeInsetsInsetRect(self.bounds, edgeInsets);
_layoutFrame = [self.layouter layoutFrameWithRect:rect range:NSMakeRange(0, 0)];
[_layoutFrame retain];
// we can only layout if we have our own layouter
if (self.layouter)
{
CGRect rect = UIEdgeInsetsInsetRect(self.bounds, edgeInsets);
rect.size.height = CGFLOAT_OPEN_HEIGHT; // necessary height set as soon as we know it.

_layoutFrame = [self.layouter layoutFrameWithRect:rect range:NSMakeRange(0, 0)];
[_layoutFrame retain];
}
}
return _layoutFrame;
}
Expand All @@ -528,6 +552,8 @@ - (void)setLayoutFrame:(DTCoreTextLayoutFrame *)layoutFrame

_layoutFrame = [layoutFrame retain];

// [self sizeToFit];

[self removeAllCustomViewsForLinks];

if (layoutFrame)
Expand Down

0 comments on commit 2eb5dbb

Please sign in to comment.