Skip to content

Commit a482d31

Browse files
committed
Resigned cell height logic which in returned fixed a lagging issue with the showing/hiding of the keyboard
1 parent 1f73e7b commit a482d31

4 files changed

Lines changed: 49 additions & 23 deletions

File tree

ARMRef/ARMRef/Source/CollectionView/ACollectionViewCell.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ NS_ASSUME_NONNULL_BEGIN
3131
@property (nonatomic, readonly, class) NSString *identifier;
3232
@property (nonatomic, weak) AInstruction *instruction;
3333

34-
- (CGFloat) maxSizeForInstruction:(AInstruction *)instruction withWidth:(CGFloat)width;
34+
+ (CGFloat) heightForInstruction:(AInstruction *)instruction withWidth:(CGFloat)width;
3535

3636
@end
3737

ARMRef/ARMRef/Source/CollectionView/ACollectionViewCell.m

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,15 @@ - (instancetype) initWithFrame:(CGRect)frame {
5353
- (void) layoutSubviews {
5454
[super layoutSubviews];
5555

56+
CGFloat width = self.bounds.size.width;
57+
5658
// Top label
57-
CGSize topSize = [self.topLabel sizeThatFits:CGSizeMake(self.contentView.bounds.size.width - 20.0f, CGFLOAT_MAX)];
58-
self.topLabel.frame = CGRectMake(10.0f, 9.0f, topSize.width, topSize.height);
59+
CGRect topFrame = [self.class _topLabelFrameForString:self.topLabel.text withWidth:width];
60+
self.topLabel.frame = CGRectMake(10.0f, 9.0f, topFrame.size.width, topFrame.size.height);
5961

6062
// Bottom label
61-
CGSize bottomSize = [self.bottomLabel sizeThatFits:CGSizeMake(self.contentView.bounds.size.width - 20.0f, CGFLOAT_MAX)];
62-
self.bottomLabel.frame = CGRectMake(10.0f, CGRectGetMaxY(self.topLabel.frame), bottomSize.width, bottomSize.height);
63+
CGRect bottomFrame = [self.class _bottomLabelFrameForString:self.bottomLabel.text withWidth:width];
64+
self.bottomLabel.frame = CGRectMake(10.0f, CGRectGetMaxY(self.topLabel.frame), bottomFrame.size.width, bottomFrame.size.height);
6365
}
6466

6567
- (void)drawRect:(CGRect)rect {
@@ -80,15 +82,40 @@ + (NSString *) identifier {
8082

8183
#pragma mark - Public methods
8284

83-
- (CGFloat) maxSizeForInstruction:(AInstruction *)instruction withWidth:(CGFloat)width {
84-
self.topLabel.text = instruction.mnemonic;
85-
self.bottomLabel.text = instruction.shortDesc;
86-
85+
+ (CGFloat) heightForInstruction:(AInstruction *)instruction withWidth:(CGFloat)width {
8786
CGFloat padding = 18.0f;
88-
CGSize topSize = [self.topLabel sizeThatFits:CGSizeMake(width, CGFLOAT_MAX)];
89-
CGSize bottomSize = [self.bottomLabel sizeThatFits:CGSizeMake(width, CGFLOAT_MAX)];
87+
CGRect topFrame = [self _topLabelFrameForString:instruction.mnemonic withWidth:width];
88+
CGRect bottomFrame = [self _bottomLabelFrameForString:instruction.shortDesc withWidth:width];
89+
90+
return topFrame.size.height + bottomFrame.size.height + padding;
91+
}
92+
93+
#pragma mark - Private class
94+
95+
+ (CGRect) _topLabelFrameForString:(NSString *)string withWidth:(CGFloat)width {
96+
return [string boundingRectWithSize:CGSizeMake(width, CGFLOAT_MAX)
97+
options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
98+
attributes:self._topLabelAttributes
99+
context:nil];
100+
}
101+
102+
+ (NSDictionary *) _topLabelAttributes {
103+
return @{
104+
NSFontAttributeName: [UIFont systemFontOfSize:20.0f weight:UIFontWeightBold]
105+
};
106+
}
107+
108+
+ (CGRect) _bottomLabelFrameForString:(NSString *)string withWidth:(CGFloat)width {
109+
return [string boundingRectWithSize:CGSizeMake(width, CGFLOAT_MAX)
110+
options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
111+
attributes:self._bottomLabelAttributes
112+
context:nil];
113+
}
90114

91-
return topSize.height + bottomSize.height + padding;
115+
+ (NSDictionary *) _bottomLabelAttributes {
116+
return @{
117+
NSFontAttributeName: [UIFont systemFontOfSize:14.0f weight:UIFontWeightRegular]
118+
};
92119
}
93120

94121
#pragma mark - Setters
@@ -102,8 +129,8 @@ - (void) setHighlighted:(BOOL)highlighted {
102129
- (void) setInstruction:(AInstruction *)instruction {
103130
_instruction = instruction;
104131

105-
self.topLabel.text = instruction.mnemonic;
106-
self.bottomLabel.text = instruction.shortDesc;
132+
self.topLabel.attributedText = [[NSAttributedString alloc] initWithString:instruction.mnemonic attributes:self.class._topLabelAttributes];
133+
self.bottomLabel.attributedText = [[NSAttributedString alloc] initWithString:instruction.shortDesc attributes:self.class._bottomLabelAttributes];
107134

108135
[self setNeedsLayout];
109136
}
@@ -114,7 +141,6 @@ - (UILabel *) topLabel {
114141
if (!_topLabel) {
115142
_topLabel = [[UILabel alloc] init];
116143
_topLabel.backgroundColor = UIColor.clearColor;
117-
_topLabel.font = [UIFont systemFontOfSize:20.0f weight:UIFontWeightBold];
118144
_topLabel.textColor = UIColor.blackColor;
119145
_topLabel.numberOfLines = 2;
120146
}
@@ -126,7 +152,6 @@ - (UILabel *) bottomLabel {
126152
if (!_bottomLabel) {
127153
_bottomLabel = [[UILabel alloc] init];
128154
_bottomLabel.backgroundColor = UIColor.clearColor;
129-
_bottomLabel.font = [UIFont systemFontOfSize:14.0f weight:UIFontWeightRegular];
130155
_bottomLabel.textColor = UIColor.blackColor;
131156
_bottomLabel.numberOfLines = 0;
132157
_bottomLabel.lineBreakMode = NSLineBreakByWordWrapping;

ARMRef/ARMRef/Source/CollectionView/ACollectionViewDataHandle.m

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ @implementation ACollectionViewDataHandle
3636

3737
- (nonnull __kindof UICollectionViewCell *) collectionView:(nonnull UICollectionView *)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath {
3838
ACollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ACollectionViewCell.identifier forIndexPath:indexPath];
39-
cell.instruction = safetyObjectAtIndex(self.instructions, indexPath.row);
4039

4140
return cell;
4241
}
@@ -47,6 +46,10 @@ - (NSInteger) collectionView:(nonnull UICollectionView *)collectionView numberOf
4746

4847
#pragma mark - UICollectionViewDelegate
4948

49+
- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(ACollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
50+
cell.instruction = safetyObjectAtIndex(self.instructions, indexPath.row);
51+
}
52+
5053
- (void) collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
5154
[self.handleTouch collectioinViewHandle:self didTouchInstruction:self.instructions[indexPath.row]];
5255
}
@@ -64,10 +67,8 @@ - (void)collectionView:(UICollectionView *)collectionView didUnhighlightItemAtIn
6467
#pragma mark - UICollectionViewDelegateFlowLayout
6568

6669
- (CGSize) collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
67-
ACollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ACollectionViewCell.identifier forIndexPath:indexPath];
68-
cell.instruction = safetyObjectAtIndex(self.instructions, indexPath.row);
69-
70-
CGFloat maxSize = [cell maxSizeForInstruction:self.instructions[indexPath.row] withWidth:collectionView.bounds.size.width - 20.0f];
70+
CGFloat maxSize = [ACollectionViewCell heightForInstruction:safetyObjectAtIndex(self.instructions, indexPath.row)
71+
withWidth:collectionView.bounds.size.width - 20.0f];
7172

7273
return CGSizeMake(collectionView.bounds.size.width - 20.0f, maxSize);
7374
}

ARMRef/ARMRef/Source/Main/AMainViewController.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,10 +190,10 @@ - (void) searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
190190
}
191191

192192
- (void) searchBarCancelButtonClicked:(UISearchBar *)searchBar {
193-
[self _updateDataAndLoaderWithString:nil];
194-
195193
searchBar.text = nil;
196194
[searchBar resignFirstResponder];
195+
196+
[self _updateDataAndLoaderWithString:nil];
197197
}
198198

199199
- (void) searchBarSearchButtonClicked:(UISearchBar *)searchBar {

0 commit comments

Comments
 (0)