From 42a8bb2126769a95050a86e29ad11a310f33344d Mon Sep 17 00:00:00 2001 From: MustiiKhalil Date: Wed, 24 Aug 2022 13:04:05 +0200 Subject: [PATCH] Adds support for text centering --- .../testCenterTextAlignment.1.png | Bin 0 -> 9865 bytes Sources/LCLabel/LCLabel.swift | 14 +++++------ Tests/LCLabelTests/LCLabelTests.swift | 23 ++++++++++++++++++ 3 files changed, 30 insertions(+), 7 deletions(-) create mode 100644 Sources/LCLabel/LCLabel.docc/Resources/__snapshots__/testCenterTextAlignment.1.png 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 0000000000000000000000000000000000000000..5bf1c5335b4daa5df5397a414c2a18d149eed07f GIT binary patch literal 9865 zcmeHsWmg=}6Ye5Gf`{Pl?he5{Xo9myu;3nC7I$|I5Zv80Xt3b!?(QsdA<6%~_|)mu~ZR6Wz(bEa#973C$7-r~Lm002nRQevM004TqgzB3&3%Ng)|sqkWlHWd|B zG#8Z=wYIRfQ?k)BFp@N~GO{x@khIftk<_!a)MM3U1pw6jbJbwAVrMYhSAGFvD)PfB zqK$;S0j#8KUWX(NLDIzDsQNIF>mnf-B|v;`TG|jzEevnoPob*&y`?B-g_)E342&gh zsnDp!M{XxKZ4bvz&nx^pi}t_ycfPwpc^ykch>?mw09^DN*-4}01!(wv<(puCbzqx7 zIb{y74Um`I-eUiP?7q_%xZQNIg(4n!N=daVQ`mdHMRj(^!Mdi>qg(H9Z~+i+KoFU~}v zdbte~Hz$cMAkd#T(nmNgd;xs{6UhN%?;k*AJPO z9CvKv7UvNhLK}yQ3b6qYZTjMz4!_YKh2Ir|@yitQuY!Qx{3#KL+0cmz(b!!fO%jN> zS;U=_X`UoNo)u>c^-gLKRwGIcnj)}w&kNRAQHIvIKTz&2H?0xK(MvG7iwq3fLeBKk zg&alLgAAhdL7tK#sbNOBCb3b5Fr7nk7r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-4KIofOTIvZ9byH>Q&WAbu!UPLgOam4S6TlqB(xaQr)D1LJ}l>%+AJTLF3gu0a8<5uQuMGsWm#Vf(O^ z9g{;6&mf}w{d$60ZIrW+;{E9XP5LY%4D;#_&&))QiLk{+jL%Og={n2c!$BqEbAx$@ z9)o(Vne0m=W1_0KwRZE7F5TrkRv#s(JjL*>!xMS}SszLbYn!g3ZC#RTM3gkM){-eq`^0 zsGH7=q0$W$zJQkJ1!G}vr!KX1#M4z4nC9KWWAEB+J(ILo2cZ@mkju#gIwK9-1P6v@ zaatp0jPlRVPVHc=PZ(}wl7z~$(D&ZEsttlzwByH_B8%{KonZ)s2k(Z5bH7PdUK|W! zaalE0t$(wMCTk7U(>i)xu^GeUhAWd(3!IbIZb)X&mO$%GbMZ`-ypAjsR$&0N(2HcU zLGv0=r?J3!0bZq>7$6A;NepIt8o8Ii=zQGEgUB>y76mU{>K zk~D-U{}_bo;X2TK{^aB(^iruUbFbf~ufY5RNelzQE%F-nMP94H@sTXmUb24--d1Q{ zWYB-E0A35Z+ILpN8ebUxLL&Z$0Fl90h~&SFmPr4@dnu*5L-Q{r_J0Tn^bSG(dwfme z|KZ)tVeDP|7n0?_1j3Tv82)ASE%ZOUoy>njn*V)B`l$aQfX#;R>o22S%>VE{nNE8% k{TK58je&#oeugstFb^~M%Y*7=YXguLmlrD&`Qr0`0GcO8ZU6uP literal 0 HcmV?d00001 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",