From 1a9df43a1329a47c1d060eb95d4432975de0bdc8 Mon Sep 17 00:00:00 2001 From: Michael Schneider Date: Sun, 30 Apr 2017 14:06:59 -0700 Subject: [PATCH 1/3] Add workaround for setting a custom lineSpacing and maxNumberOfLines to ASTextNode docs --- docs/_docs/text-node.md | 57 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/docs/_docs/text-node.md b/docs/_docs/text-node.md index b2774fac1..7582a1ff3 100755 --- a/docs/_docs/text-node.md +++ b/docs/_docs/text-node.md @@ -148,3 +148,60 @@ In a similar way, you can react to long presses and highlighting with the follow `– textNode:shouldLongPressLinkAttribute:value:atPoint:` +### Incorrect maximum number of lines with line spacing + +Using a `NSParagraphStyle` with a non-default `lineSpacing` can cause problems if multiline text with a maximum number of lines is wanted. For example see the following code: + +
+SwiftObjective-C + +
+
+// ...
+NSString *someLongString = ...;
+
+NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
+paragraphStyle.lineSpacing = 10.0;
+
+UIFont *font = [UIFont fontWithName:@"SomeFontName" size:15];
+
+NSDictionary *attributes = @{
+	NSFontAttributeName : font,
+	NSParagraphStyleAttributeName: paragraphStyle
+};
+
+ASTextNode *textNode = [[ASTextNode alloc] init];
+textNode.maximumNumberOfLines = 4;
+textNode.attributedText = [[NSAttributedString	alloc] initWithString:someLongString
+																												   attributes:attributes];
+// ...
+
+ + +
+
+ +`ASTextNode` uses Text Kit internally to calculate the amount to shrink that results in the max number of lines. Unfortunately in certain cases this will result that the text will shrink too much and in the example above instead of 4, 3 lines of text and a weird gap at the bottom will show up. To get around this issue for now, you have to set the `truncationMode` explicitly to `NSLineBreakByTruncatingTail` on the text node: + +
+SwiftObjective-C + +
+
+// ...
+ASTextNode *textNode = [[ASTextNode alloc] init];
+textNode.maximumNumberOfLines = 4;
+textNode.truncationMode = NSLineBreakByTruncatingTail;
+textNode.attributedText = [[NSAttributedString	alloc] initWithString:someLongString
+																												   attributes:attributes];
+// ...
+
+ + +
+
+``` From 9eea2bfb9760a86169efaaece8e770165bf456d2 Mon Sep 17 00:00:00 2001 From: Michael Schneider Date: Tue, 2 May 2017 15:28:07 -0700 Subject: [PATCH 2/3] Address comments and add Swift example --- docs/_docs/text-node.md | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/docs/_docs/text-node.md b/docs/_docs/text-node.md index 7582a1ff3..22af98eb2 100755 --- a/docs/_docs/text-node.md +++ b/docs/_docs/text-node.md @@ -24,7 +24,7 @@ _node.attributedText = string;