From 5562d1ce766466e0648b7a411ea96af9aaf15594 Mon Sep 17 00:00:00 2001 From: Zac West <74188+zacwest@users.noreply.github.com> Date: Fri, 2 Apr 2021 14:40:51 -0700 Subject: [PATCH] Fix orphan (single-word last line) tappability by using line break strategy (#44) Fixes #42. Fixes tappability of regions relocated to a new line to avoid single-word last lines by UILabel. Xcode 12 (and the iOS 14 SDK) expose `.lineBreakStrategy` which is a new value on `UILabel` and `NSParagraphStyle` to control the orphan avoidance and line break positioning in strings. This new method/enum is backwards compatible as it seems to be formerly-private API made public, so it works on all versions of iOS the label supports as long as you compile using Xcode 12. --- CHANGELOG.md | 5 +++++ ZSWTappableLabel/ZSWTappableLabel.m | 10 +++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 09fa99b..3034fa6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +# 3.3 (2021-04-02) + +- Fixes tappability of regions relocated to a new line to avoid single-word last lines by UILabel. This requires compiling using Xcode 12, but works backwards-compatibly. +- Fixes accessibility elements sometimes exposing more than 1 element per tappable span. + # 3.2 (2020-04-17) - Fixes handling accessibility element activation that spans multiple lines. diff --git a/ZSWTappableLabel/ZSWTappableLabel.m b/ZSWTappableLabel/ZSWTappableLabel.m index c6f78d9..eb69a31 100644 --- a/ZSWTappableLabel/ZSWTappableLabel.m +++ b/ZSWTappableLabel/ZSWTappableLabel.m @@ -137,10 +137,18 @@ - (ZSWTappableLabelTouchHandling *)createTouchHandlingIfNeeded { range:range]; } }]; - + +#ifdef __IPHONE_14_0 + // lineBreakStrategy was exposed publicly in Xcode 12, but works backwords-compatibly + if (self.textAlignment != NSTextAlignmentLeft || self.lineBreakStrategy != NSLineBreakStrategyNone) { +#else if (self.textAlignment != NSTextAlignmentLeft) { +#endif NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init]; style.alignment = self.textAlignment; +#ifdef __IPHONE_14_0 + style.lineBreakStrategy = self.lineBreakStrategy; +#endif [attributedText enumerateAttribute:NSParagraphStyleAttributeName inRange:NSMakeRange(0, attributedText.length)