diff --git a/Sources/LCLabel/LCLabel.docc/Resources/__snapshots__/testCenterTextAlignment.1.png b/Sources/LCLabel/LCLabel.docc/Resources/__snapshots__/testCenterTextAlignment.1.png new file mode 100644 index 0000000..5bf1c53 Binary files /dev/null and b/Sources/LCLabel/LCLabel.docc/Resources/__snapshots__/testCenterTextAlignment.1.png differ diff --git a/Sources/LCLabel/LCLabel.swift b/Sources/LCLabel/LCLabel.swift index 21490e3..62912d1 100644 --- a/Sources/LCLabel/LCLabel.swift +++ b/Sources/LCLabel/LCLabel.swift @@ -238,21 +238,21 @@ final public class LCLabel: UILabel { "The new bounds are negative with isnt allowed, check the frame or the textInsets") textContainer.size = newBounds.size layoutManager.ensureLayout(for: textContainer) - let calculatedValues = layoutManager.usedRect(for: textContainer) + let drawableFrame = layoutManager.usedRect(for: textContainer) switch centeringTextAlignment { case .center: newBounds.origin - .y = floor((newBounds.height - calculatedValues.height) / 2) + .y = floor((newBounds.height - drawableFrame.height) / 2) case .bottom: - newBounds.origin.y = (newBounds.height - calculatedValues.height) + newBounds.origin.y = (newBounds.height - drawableFrame.height) case .top: break } return CGRect( - x: newBounds.origin.x, - y: newBounds.origin.y, - width: calculatedValues.width, - height: calculatedValues.height) + x: newBounds.origin.x + drawableFrame.origin.x, + y: newBounds.origin.y + drawableFrame.origin.y, + width: drawableFrame.width, + height: drawableFrame.height) } // MARK: Private diff --git a/Tests/LCLabelTests/LCLabelTests.swift b/Tests/LCLabelTests/LCLabelTests.swift index 504daeb..180e7cd 100644 --- a/Tests/LCLabelTests/LCLabelTests.swift +++ b/Tests/LCLabelTests/LCLabelTests.swift @@ -31,6 +31,29 @@ final class LCLabelTests: XCTestCase { XCTFail(message) } + func testCenterTextAlignment() { + let paragraph = NSMutableParagraphStyle() + paragraph.alignment = .center + + let attStr = NSMutableAttributedString( + string: "LCLabel is a low cost label", + attributes: [ + .foregroundColor: UIColor.white, + .font: UIFont.systemFont(ofSize: 14), + .paragraphStyle: paragraph, + ]) + let label = createLabel( + text: attStr, + frame: CGRect(x: 0, y: 0, width: 300, height: 40)) + label.numberOfLines = 1 + let failure = verifySnapshot( + matching: label, + as: .image, + snapshotDirectory: path) + guard let message = failure else { return } + XCTFail(message) + } + func testTextTopAlignment() { let attStr = NSMutableAttributedString( string: "LCLabel is a low cost label",