Skip to content
This repository has been archived by the owner on May 18, 2022. It is now read-only.

Commit

Permalink
Optimize scrolling slightly. jessesquires#1058.
Browse files Browse the repository at this point in the history
  • Loading branch information
juliensaad committed Oct 16, 2015
1 parent 3bd3fbf commit 846b9aa
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -540,11 +540,7 @@ - (UICollectionViewCell *)collectionView:(JSQMessagesCollectionView *)collection
cell.messageBubbleTopLabel.textInsets = UIEdgeInsetsMake(0.0f, bubbleTopLabelInset, 0.0f, 0.0f);
}

cell.textView.dataDetectorTypes = UIDataDetectorTypeAll;

cell.backgroundColor = [UIColor clearColor];
cell.layer.rasterizationScale = [UIScreen mainScreen].scale;
cell.layer.shouldRasterize = YES;

return cell;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@

@interface JSQMessagesTimestampFormatter ()

// Separate formatters for the Date & Time parts for performance reasons
@property (strong, nonatomic, readwrite) NSDateFormatter *dateFormatter;
@property (strong, nonatomic, readwrite) NSDateFormatter *timeFormatter;

@end

Expand Down Expand Up @@ -50,6 +52,9 @@ - (instancetype)init
[_dateFormatter setLocale:[NSLocale currentLocale]];
[_dateFormatter setDoesRelativeDateFormatting:YES];

_timeFormatter = [[NSDateFormatter alloc] init];
[_timeFormatter setLocale:[NSLocale currentLocale]];

UIColor *color = [UIColor lightGrayColor];

NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
Expand All @@ -69,6 +74,7 @@ - (instancetype)init
- (void)dealloc
{
_dateFormatter = nil;
_timeFormatter = nil;
_dateTextAttributes = nil;
_timeTextAttributes = nil;
}
Expand All @@ -81,9 +87,10 @@ - (NSString *)timestampForDate:(NSDate *)date
return nil;
}

[self.dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[self.dateFormatter setTimeStyle:NSDateFormatterShortStyle];
return [self.dateFormatter stringFromDate:date];
NSString *relativeDate = [self relativeDateForDate:date];
NSString *time = [self timeForDate:date];

return [NSString stringWithFormat:@"%@ %@", relativeDate, time];
}

- (NSAttributedString *)attributedTimestampForDate:(NSDate *)date
Expand Down Expand Up @@ -112,9 +119,9 @@ - (NSString *)timeForDate:(NSDate *)date
return nil;
}

[self.dateFormatter setDateStyle:NSDateFormatterNoStyle];
[self.dateFormatter setTimeStyle:NSDateFormatterShortStyle];
return [self.dateFormatter stringFromDate:date];
[self.timeFormatter setDateStyle:NSDateFormatterNoStyle];
[self.timeFormatter setTimeStyle:NSDateFormatterShortStyle];
return [self.timeFormatter stringFromDate:date];
}

- (NSString *)relativeDateForDate:(NSDate *)date
Expand All @@ -128,4 +135,4 @@ - (NSString *)relativeDateForDate:(NSDate *)date
return [self.dateFormatter stringFromDate:date];
}

@end
@end
38 changes: 33 additions & 5 deletions JSQMessagesViewController/Views/JSQMessagesCellTextView.m
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@

#import "JSQMessagesCellTextView.h"

@implementation JSQMessagesCellTextView
@implementation JSQMessagesCellTextView {
BOOL _hasText;
}

- (void)awakeFromNib
{
Expand All @@ -31,7 +33,7 @@ - (void)awakeFromNib
self.dataDetectorTypes = UIDataDetectorTypeNone;
self.showsHorizontalScrollIndicator = NO;
self.showsVerticalScrollIndicator = NO;
self.scrollEnabled = NO;
//self.scrollEnabled = NO;
self.backgroundColor = [UIColor clearColor];
self.contentInset = UIEdgeInsetsZero;
self.scrollIndicatorInsets = UIEdgeInsetsZero;
Expand All @@ -45,14 +47,40 @@ - (void)awakeFromNib
self.textContainer.lineFragmentPadding = 0;
}

- (void)setText:(NSString *)text {
[super setText:text];
// In prepareForReuse we would set the text to nil,
// and then we would set the text to the correct value.
// This dance takes time, especially with data detectors in place.
// We make sure that the outside can see the correct values and we do layout.
- (void)setText:(NSString *)text
{
if (text) {
[super setText:text];
}
_hasText = !!text.length;
[self setNeedsLayout];
}

- (NSString *)text
{
return (_hasText) ? [super text] : nil;
}

- (void)setAttributedText:(NSAttributedString *)attributedText
{
if (attributedText) {
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineSpacing = self.font.pointSize * self.lineHeightFactor;
NSDictionary *attrsDictionary = @{ NSFontAttributeName: self.font, NSParagraphStyleAttributeName: paragraphStyle};
self.attributedText = [[NSAttributedString alloc] initWithString:text attributes:attrsDictionary];
attributedText = [[NSAttributedString alloc] initWithString:attributedText.string attributes:attrsDictionary];
[super setAttributedText:attributedText];
}
_hasText = !!attributedText.length;
[self setNeedsLayout];
}

- (NSAttributedString *)attributedText
{
return (_hasText) ? [super attributedText] : nil;
}

- (void)setSelectedRange:(NSRange)selectedRange
Expand Down

0 comments on commit 846b9aa

Please sign in to comment.