From 93a263d874789c23646cb1f77df0198e9e58cf33 Mon Sep 17 00:00:00 2001 From: Mehdi Mulani Date: Fri, 17 Feb 2017 15:02:36 -0800 Subject: [PATCH] Make RCTRefreshControl not dependent on order of setting properties Summary: With the Cxx bridge, properties are not guaranteed to be set it any order (and furthermore, they should not be). RCTRefreshControl previously had its title set first, and would crash otherwise. This fixes that. Reviewed By: mhorowitz Differential Revision: D4580253 fbshipit-source-id: 39baecceb8b67c6a851c08ba9cabbf4dc99359cb --- React/Views/RCTRefreshControl.m | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/React/Views/RCTRefreshControl.m b/React/Views/RCTRefreshControl.m index 524b81d17..ee73bf9a4 100644 --- a/React/Views/RCTRefreshControl.m +++ b/React/Views/RCTRefreshControl.m @@ -14,6 +14,8 @@ @implementation RCTRefreshControl { BOOL _isInitialRender; BOOL _currentRefreshingState; + NSString *_title; + UIColor *_titleColor; } - (instancetype)init @@ -86,23 +88,33 @@ RCT_NOT_IMPLEMENTED(- (instancetype)initWithCoder:(NSCoder *)aDecoder) - (NSString *)title { - return self.attributedTitle.string; + return _title; } - (void)setTitle:(NSString *)title { - NSRange range = NSMakeRange(0, self.attributedTitle.length); - NSDictionary *attrs = [self.attributedTitle attributesAtIndex:0 effectiveRange: &range]; - self.attributedTitle = [[NSAttributedString alloc] initWithString:title attributes:attrs]; + _title = title; + [self _updateTitle]; } - (void)setTitleColor:(UIColor *)color { - NSRange range = NSMakeRange(0, self.attributedTitle.length); - NSDictionary *attrs = [self.attributedTitle attributesAtIndex:0 effectiveRange: &range]; - NSMutableDictionary *attrsMutable = [attrs mutableCopy]; - [attrsMutable setObject:color forKey:NSForegroundColorAttributeName]; - self.attributedTitle = [[NSAttributedString alloc] initWithString:self.attributedTitle.string attributes:attrsMutable]; + _titleColor = color; + [self _updateTitle]; +} + +- (void)_updateTitle +{ + if (!_title) { + return; + } + + NSMutableDictionary *attributes = [NSMutableDictionary dictionary]; + if (_titleColor) { + attributes[NSForegroundColorAttributeName] = _titleColor; + } + + self.attributedTitle = [[NSAttributedString alloc] initWithString:_title attributes:attributes]; } - (void)setRefreshing:(BOOL)refreshing