Skip to content

Commit 69e7aff

Browse files
committed
bugfix: sizeThatFit:
1 parent 564ba08 commit 69e7aff

File tree

2 files changed

+55
-17
lines changed

2 files changed

+55
-17
lines changed

YYKit/Text/YYLabel.m

+34-3
Original file line numberDiff line numberDiff line change
@@ -448,9 +448,40 @@ - (void)setBounds:(CGRect)bounds {
448448
}
449449

450450
- (CGSize)sizeThatFits:(CGSize)size {
451-
[self _updateIfNeeded];
452-
if (!self._innerLayout) return size;
453-
return [self._innerLayout textBoundingSize];
451+
if (_ignoreCommonProperties) {
452+
return _innerLayout.textBoundingSize;
453+
}
454+
455+
if ((!_verticalForm && size.width == self.bounds.size.width) ||
456+
(_verticalForm && size.height == self.bounds.size.height)) {
457+
[self _updateIfNeeded];
458+
YYTextLayout *layout = self._innerLayout;
459+
BOOL contains = NO;
460+
if (layout.container.maximumNumberOfRows == 0) {
461+
if (layout.truncatedLine == nil) {
462+
contains = YES;
463+
}
464+
} else {
465+
if (layout.rowCount <= layout.container.maximumNumberOfRows) {
466+
contains = YES;
467+
}
468+
}
469+
if (contains) {
470+
return layout.textBoundingSize;
471+
}
472+
}
473+
474+
if (!_verticalForm) {
475+
size.height = YYTextContainerMaxSize.height;
476+
} else {
477+
size.width = YYTextContainerMaxSize.width;
478+
}
479+
480+
YYTextContainer *container = [_innerContainer copy];
481+
container.size = size;
482+
483+
YYTextLayout *layout = [YYTextLayout layoutWithContainer:container text:_innerText];
484+
return layout.textBoundingSize;
454485
}
455486

456487
- (NSString *)accessibilityLabel {

YYKit/Text/YYTextView.m

+21-14
Original file line numberDiff line numberDiff line change
@@ -2442,24 +2442,31 @@ - (void)tintColorDidChange {
24422442
}
24432443

24442444
- (CGSize)sizeThatFits:(CGSize)size {
2445-
YYTextLayout *textLayout = self.textLayout;
2446-
CGSize currentSize = CGSizePixelCeil(textLayout.textBoundingSize);
2447-
if (_verticalForm) {
2448-
if (currentSize.height == size.height) return currentSize;
2449-
} else {
2450-
if (currentSize.width == size.width) return currentSize;
2445+
if ((!_verticalForm && size.width == self.bounds.size.width) ||
2446+
(_verticalForm && size.height == self.bounds.size.height)) {
2447+
[self _updateIfNeeded];
2448+
if (!_verticalForm) {
2449+
if (_containerView.bounds.size.height <= size.height) {
2450+
return _containerView.bounds.size;
2451+
}
2452+
} else {
2453+
if (_containerView.bounds.size.width <= size.width) {
2454+
return _containerView.bounds.size;
2455+
}
2456+
}
24512457
}
24522458

2453-
if (_verticalForm) {
2454-
size.width = CGFLOAT_MAX;
2459+
if (!_verticalForm) {
2460+
size.height = YYTextContainerMaxSize.height;
24552461
} else {
2456-
size.height = CGFLOAT_MAX;
2462+
size.width = YYTextContainerMaxSize.width;
24572463
}
2458-
YYTextContainer *newContainer = _innerContainer.copy;
2459-
newContainer.size = size;
2460-
YYTextLayout *newLayout = [YYTextLayout layoutWithContainer:newContainer text:textLayout.text];
2461-
CGSize newSize = newLayout.textBoundingSize;
2462-
return CGSizePixelCeil(newSize);
2464+
2465+
YYTextContainer *container = [_innerContainer copy];
2466+
container.size = size;
2467+
2468+
YYTextLayout *layout = [YYTextLayout layoutWithContainer:container text:_innerText];
2469+
return layout.textBoundingSize;
24632470
}
24642471

24652472
#pragma mark - Override UIResponder

0 commit comments

Comments
 (0)