Skip to content
This repository has been archived by the owner on Feb 17, 2021. It is now read-only.

Fixed Potential truncation issue in LabelLayout #229

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

PrkumariLI
Copy link
Contributor

  • Added line break mode support in LabelLayout to calculate correct size based on line break mode.

@PrkumariLI PrkumariLI force-pushed the HUED-8605-LabelLayoutHeightCalculationFix branch from 2343c3f to 1e91330 Compare August 27, 2018 13:01
@PrkumariLI PrkumariLI changed the title HUED-8605 Test & Fix Potential truncation issue in LabelLayout Fixed Potential truncation issue in LabelLayout Aug 27, 2018
@PrkumariLI
Copy link
Contributor Author

  • Added LabelLayout unit test to verify that size calculation logic considers line break mode while calculating LabelLayout size.

@@ -11,6 +11,12 @@ import LayoutKit

class LabelLayoutTests: XCTestCase {

// For the defined `sampleText` and `labelLayoutMaxWidth` combination, `LabelLayout` requires 2 line of
// text for char-wrapping and 3 lines for word-wrapping/truncating-tail.
// So don't change this combination as the tests are based on these values.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we add a test that verifies that char-wrapping and word-wrapping do indeed return different values? That way we'd catch the issue if these constants are indeed changed. That test then can explain that other tests are relying on this difference.

Looking further down, I see that there are already tests that verify this, so it'd be nice to mention that in the comment so that the reader understands that this is verified by tests and not that fragile. The current comment makes it sound like this is easy to break.

// Use different line break mode for `LabelLayout` and dummy label.
// So in this case, size calculation should not match with dummy label's size calculation.
let label = UILabel(text: LabelLayoutTests.sampleText, numberOfLines: 0, lineBreakMode: .byCharWrapping)
let layout = LabelLayout(text: LabelLayoutTests.sampleText, config: { label in
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should be able to remove this config parameter and the two similar ones below, yes?

func testAttributedTextSizeCalculationWithWordWrapping() {
// Use same line break mode for `LabelLayout` and dummy label and then match LabelLayout's size with dummy label's size calculation.
let attributedText = NSAttributedString(string: LabelLayoutTests.sampleText)
verifyAttributedTextSizeCalculation(with: attributedText, lineBreakMode: .byWordWrapping)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to iterate through all the different line break modes to confirm that the sizes match for all of them?

let options: NSStringDrawingOptions = [
.usesLineFragmentOrigin
]

/**
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use // comments to be consistent with the other comments in this method. Also, /** comments usually are meant for doc comments that get parsed and processed and formatted by tools to produce API documentation, and this comment is internal information.

Thank you for adding this comment, by the way!

So use `paragraphStyle` only in case when UILabel/UITextView's mode is `byCharWrapping` because if we use `paragraphStyle`
with `byTruncatingTail`, `boundingRect(with:options:attributes:)` always give single line height.
*/
var attributes: [NSAttributedStringKey: Any] = [NSAttributedStringKey.font: font]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor issue, but can we make this a let instead of a var? slightly easier to read if one doesn't have to trace through the code looking to see if there are any more spots where it might get updated.

@objc public let numberOfLines: Int
@objc public let alignment: LOKAlignment
@objc public let viewClass: UILabel.Type
@objc public let configure: ((UILabel) -> Void)?

// TODO: Remove this init once all consumers are switch to `init(attributedString:font:lineBreakMode:lineHeight:numberOfLines:alignment:flexibility:viewReuseId:viewClass:configure:)`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should just add the new parameter, since it's optional.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • We have added a new parameter in LOKLabelLayout.swift but it will be a breaking change because every Objective-C caller will have to pass the new parameter else it won't build. Also we made it non-optional parameter as optional enum won't be accessible in Objective-C.
    Let us know if this should be fine.

- Added LabelLayout unit test to verify that size calculation logic considers line break mode while calculating LabelLayout size.
- Added line break mode support in LabelLayout to calculate correct size based on line break mode.
@PrkumariLI PrkumariLI force-pushed the HUED-8605-LabelLayoutHeightCalculationFix branch from 1e91330 to 089149f Compare September 4, 2018 15:24
@PrkumariLI
Copy link
Contributor Author

  • Fixed review comments.

@p-jadhav
Copy link
Contributor

p-jadhav commented Jun 7, 2019

These changes are being tracked in pull request: #260

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants