From 81835efda552516fd8c0535a1622b8ecd85df567 Mon Sep 17 00:00:00 2001 From: Michael Schneider Date: Sun, 30 Apr 2017 14:06:59 -0700 Subject: [PATCH] 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];
+// ...
+
+ + +
+
+```