Permalink
Browse files

Removed defunct debugFrames property on content view

  • Loading branch information...
1 parent f026819 commit 9f8f10fb80eb6a798ee2c86998462a7514bfc29d @odrobnik odrobnik committed Jan 18, 2013
@@ -125,7 +125,6 @@
@property (nonatomic, copy) NSAttributedString *attributedString;
@property (nonatomic) UIEdgeInsets edgeInsets;
-@property (nonatomic) BOOL drawDebugFrames;
@property (nonatomic) BOOL shouldDrawImages;
@property (nonatomic) BOOL shouldDrawLinks;
@property (nonatomic) BOOL shouldLayoutCustomSubviews;
@@ -20,7 +20,6 @@
@interface DTAttributedTextContentView ()
{
- BOOL _drawDebugFrames;
BOOL _shouldDrawImages;
BOOL _shouldDrawLinks;
BOOL _shouldLayoutCustomSubviews;
@@ -472,8 +471,9 @@ - (NSString *)description
- (void)relayoutText
{
- // Make sure we actually have a superview before attempting to relayout the text.
- if (self.superview) {
+ // Make sure we actually have a superview and a previous layout before attempting to relayout the text.
+ if (_layoutFrame && self.superview)
+ {
// need new layouter
self.layouter = nil;
self.layoutFrame = nil;
@@ -796,7 +796,6 @@ - (dispatch_semaphore_t)selfLock
@synthesize attributedString = _attributedString;
@synthesize delegate = _delegate;
@synthesize edgeInsets = _edgeInsets;
-@synthesize drawDebugFrames = _drawDebugFrames;
@synthesize shouldDrawImages = _shouldDrawImages;
@synthesize shouldDrawLinks = _shouldDrawLinks;
@synthesize shouldLayoutCustomSubviews = _shouldLayoutCustomSubviews;
@@ -11,18 +11,31 @@
@class DTAttributedTextView;
@interface DTAttributedTextView : UIScrollView
-{
- DTAttributedTextContentView *contentView;
- UIView *backgroundView;
-}
+/**
+ The attributed text to be displayed in the text content view of the receiver.
+ */
@property (nonatomic, strong) NSAttributedString *attributedString;
-@property (nonatomic, strong, readonly) DTAttributedTextContentView *contentView;
+/**
+ References to the DTAttributedTextContentView that display the text. This is not named contentView because this class inherits from `UIScrollView` which has an internal property of this name
+ */
+@property (nonatomic, strong, readonly) DTAttributedTextContentView *attributedTextContentView;
+
+/**
+ A view to be displayed behind the text content view
+ */
@property (nonatomic, strong) IBOutlet UIView *backgroundView;
+/**
+ A delegate implementing DTAttributedTextContentViewDelegate to provide custom subviews for images and links.
+ */
@property (nonatomic, unsafe_unretained) IBOutlet id <DTAttributedTextContentViewDelegate> textDelegate;
+/**
+ If the content view of the receiver should draw links. Set to `NO` if displaying links as custom views via textDelegate;
+ */
+@property (nonatomic, assign) BOOL shouldDrawLinks;
/**
Scrolls the receiver to the anchor with the given name to the top.
@@ -8,6 +8,7 @@
#import "DTAttributedTextView.h"
#import "DTCoreText.h"
+#import <QuartzCore/QuartzCore.h>
@interface DTAttributedTextView ()
@@ -18,6 +19,15 @@ - (void)setup;
@implementation DTAttributedTextView
+{
+ DTAttributedTextContentView *_attributedTextContentView;
+ UIView *_backgroundView;
+
+ // these are pass-through, i.e. store until the content view is created
+ __unsafe_unretained id textDelegate;
+ NSAttributedString *_attributedString;
+ BOOL _shouldDrawLinks;
+}
- (id)initWithFrame:(CGRect)frame
{
@@ -33,8 +43,8 @@ - (id)initWithFrame:(CGRect)frame
- (void)dealloc
{
- contentView.delegate = nil;
- [contentView removeObserver:self forKeyPath:@"frame"];
+ _attributedTextContentView.delegate = nil;
+ [_attributedTextContentView removeObserver:self forKeyPath:@"frame"];
}
- (void)layoutSubviews
@@ -44,7 +54,7 @@ - (void)layoutSubviews
[self contentView];
// layout custom subviews for visible area
- [contentView layoutSubviewsInRect:self.bounds];
+ [_attributedTextContentView layoutSubviewsInRect:self.bounds];
}
- (void)awakeFromNib
@@ -73,7 +83,7 @@ - (void)setup
self.opaque = YES;
}
- self.autoresizesSubviews = YES;
+ self.autoresizesSubviews = NO;
self.clipsToBounds = YES;
}
@@ -105,7 +115,7 @@ - (void)scrollToAnchorNamed:(NSString *)anchorName animated:(BOOL)animated
#pragma mark Notifications
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
- if (object == contentView && [keyPath isEqualToString:@"frame"])
+ if (object == _attributedTextContentView && [keyPath isEqualToString:@"frame"])
{
CGRect newFrame = [[change objectForKey:NSKeyValueChangeNewKey] CGRectValue];
self.contentSize = newFrame.size;
@@ -115,141 +125,182 @@ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(N
#pragma mark Properties
- (DTAttributedTextContentView *)contentView
{
- if (!contentView)
+ if (!_attributedTextContentView)
{
// subclasses can specify a DTAttributedTextContentView subclass instead
Class classToUse = [self classForContentView];
- contentView = [[classToUse alloc] initWithFrame:self.bounds];
- contentView.userInteractionEnabled = YES;
- contentView.backgroundColor = self.backgroundColor;
- contentView.shouldLayoutCustomSubviews = NO; // we call layout when scrolling
+ CGRect frame = UIEdgeInsetsInsetRect(self.bounds, self.contentInset);
+ _attributedTextContentView = [[classToUse alloc] initWithFrame:frame];
+ _attributedTextContentView.userInteractionEnabled = YES;
+ _attributedTextContentView.backgroundColor = self.backgroundColor;
+ _attributedTextContentView.shouldLayoutCustomSubviews = NO; // we call layout when scrolling
+
+ // adjust opaqueness based on background color alpha
CGFloat alpha = [self.backgroundColor alphaComponent];
if (alpha < 1.0)
{
- self.opaque = NO;
- contentView.opaque = NO;
+ _attributedTextContentView.opaque = NO;
}
else
{
- self.opaque = YES;
- contentView.opaque = YES;
+ _attributedTextContentView.opaque = YES;
}
+
+ // set text delegate if it was set before instantiation of content view
+ _attributedTextContentView.delegate = textDelegate;
+
+ // pass on setting
+ _attributedTextContentView.shouldDrawLinks = _shouldDrawLinks;
+
+ // set text we previously got
+ _attributedTextContentView.attributedString = _attributedString;
+
+ CGSize neededSize = [_attributedTextContentView sizeThatFits:CGSizeZero];
+ frame.size = neededSize;
+ _attributedTextContentView.frame = frame;
// we want to know if the frame changes so that we can adjust the scrollview content size
- [contentView addObserver:self forKeyPath:@"frame" options:NSKeyValueObservingOptionNew context:nil];
+ [_attributedTextContentView addObserver:self forKeyPath:@"frame" options:NSKeyValueObservingOptionNew context:nil];
- [self addSubview:contentView];
+ [self addSubview:_attributedTextContentView];
}
- return contentView;
+ return _attributedTextContentView;
}
- (void)setBackgroundColor:(DTColor *)newColor
{
if ([newColor alphaComponent]<1.0)
{
super.backgroundColor = newColor;
- contentView.backgroundColor = [DTColor clearColor];
+ _attributedTextContentView.backgroundColor = [DTColor clearColor];
self.opaque = NO;
}
else
{
super.backgroundColor = newColor;
- if (contentView.opaque)
+ if (_attributedTextContentView.opaque)
{
- contentView.backgroundColor = newColor;
+ _attributedTextContentView.backgroundColor = newColor;
}
}
}
- (UIView *)backgroundView
{
- if (!backgroundView)
+ if (!_backgroundView)
{
- backgroundView = [[UIView alloc] initWithFrame:self.bounds];
- backgroundView.backgroundColor = [DTColor whiteColor];
+ _backgroundView = [[UIView alloc] initWithFrame:self.bounds];
+ _backgroundView.backgroundColor = [DTColor whiteColor];
// default is no interaction because background should have no interaction
- backgroundView.userInteractionEnabled = NO;
+ _backgroundView.userInteractionEnabled = NO;
- [self insertSubview:backgroundView belowSubview:self.contentView];
+ [self insertSubview:_backgroundView belowSubview:self.contentView];
// make content transparent so that we see the background
- contentView.backgroundColor = [DTColor clearColor];
- contentView.opaque = NO;
+ _attributedTextContentView.backgroundColor = [DTColor clearColor];
+ _attributedTextContentView.opaque = NO;
}
- return backgroundView;
+ return _backgroundView;
}
-- (void)setBackgroundView:(UIView *)newBackgroundView
+- (void)setBackgroundView:(UIView *)backgroundView
{
- if (backgroundView != newBackgroundView)
+ if (_backgroundView != backgroundView)
{
- [backgroundView removeFromSuperview];
- backgroundView = newBackgroundView;
+ [_backgroundView removeFromSuperview];
+ _backgroundView = backgroundView;
- [self insertSubview:backgroundView belowSubview:self.contentView];
+ if (_attributedTextContentView)
+ {
+ [self insertSubview:_backgroundView belowSubview:_attributedTextContentView];
+ }
+ else
+ {
+ [self addSubview:_backgroundView];
+ }
- if (backgroundView)
+ if (_backgroundView)
{
// make content transparent so that we see the background
- contentView.backgroundColor = [DTColor clearColor];
- contentView.opaque = NO;
+ _attributedTextContentView.backgroundColor = [DTColor clearColor];
+ _attributedTextContentView.opaque = NO;
}
else
{
- contentView.backgroundColor = [DTColor whiteColor];
- contentView.opaque = YES;
+ _attributedTextContentView.backgroundColor = [DTColor whiteColor];
+ _attributedTextContentView.opaque = YES;
}
}
}
- (void)setAttributedString:(NSAttributedString *)string
{
- self.contentView.attributedString = string;
-
+ _attributedString = string;
+
// might need layout for visible custom views
[self setNeedsLayout];
- // adjust content size right away
- self.contentSize = self.contentView.frame.size;
+ if (_attributedTextContentView)
+ {
+ // pass it along if contentView already exists
+ _attributedTextContentView.attributedString = string;
+
+ // adjust content size right away
+ self.contentSize = _attributedTextContentView.frame.size;
+ }
}
- (NSAttributedString *)attributedString
{
- return self.contentView.attributedString;
+ return _attributedString;
}
-
- (void)setFrame:(CGRect)frame
{
if (!CGRectEqualToRect(self.frame, frame))
{
if (self.frame.size.width != frame.size.width)
{
- contentView.frame = CGRectMake(0,0,frame.size.width, frame.size.height);
+ // height does not matter, that will be determined anyhow
+ CGRect contentFrame = CGRectMake(0, 0, frame.size.width - self.contentInset.left - self.contentInset.right, 0);
+
+ _attributedTextContentView.frame = contentFrame;
}
[super setFrame:frame];
}
}
- (void)setTextDelegate:(id<DTAttributedTextContentViewDelegate>)aTextDelegate
{
- self.contentView.delegate = aTextDelegate;
+ // store unsafe pointer to delegate because we might not have a contentView yet
+ textDelegate = aTextDelegate;
+
+ // set it if possible, otherwise it will be set in contentView lazy property
+ _attributedTextContentView.delegate = aTextDelegate;
}
- (id<DTAttributedTextContentViewDelegate>)textDelegate
{
- return contentView.delegate;
+ return _attributedTextContentView.delegate;
+}
+
+- (void)setShouldDrawLinks:(BOOL)shouldDrawLinks
+{
+ _shouldDrawLinks = shouldDrawLinks;
+ _attributedTextContentView.shouldDrawLinks = YES;
}
-@synthesize attributedString;
-@synthesize contentView;
-@synthesize textDelegate;
+@synthesize attributedTextContentView = _attributedTextContentView;
+@synthesize attributedString = _attributedString;
+@synthesize textDelegate = _textDelegate;
+
+@synthesize shouldDrawLinks = _shouldDrawLinks;
@end
Oops, something went wrong. Retry.

0 comments on commit 9f8f10f

Please sign in to comment.