2

Using an exclusion path with a UITextView is not properly wrapping the words for the first and last line in the example shown (lower example). It does render properly using word-wrap with CoreText (top example image).

Top example Core Text; bottom Text Kit

Here is the code for the UITextView (both use the same size bezier path, and both use the same font and paragraph settings)

NSString *testText = @"Text Kit exclusion paths ..."; //text truncated for brevity
UIBezierPath *viewPath =  [UIBezierPath bezierPathWithRect:CGRectMake(0, 0, 280, 280)];
UIBezierPath *shapePath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(10, 10, 265, 265)];

viewPath.usesEvenOddFillRule = true;
shapePath.usesEvenOddFillRule = true;

[shapePath appendPath:viewPath];

NSMutableAttributedString *title = [[NSMutableAttributedString alloc]initWithString:testText];
UIFont *font = [UIFont fontWithName:@"BradleyHandITCTT-Bold" size:14];
[title addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, title.length)];

//add color
[title addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, title.length)];

//add alignment
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setAlignment:NSTextAlignmentCenter];
[paragraphStyle setLineBreakMode:NSLineBreakByWordWrapping];
[title addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, title.length)];


UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(0.0, 370.0, 280, 280)];
textView.textContainerInset = UIEdgeInsetsMake(0,0,0,0);
textView.textContainer.exclusionPaths = @[shapePath];
[textView.textContainer setLineBreakMode:NSLineBreakByWordWrapping];
textView.contentInset = UIEdgeInsetsMake(0,0,0,0);
textView.attributedText = title;
textView.backgroundColor = [UIColor clearColor];

It is worth noting that the Text Kit is respecting the word wrapping rules except for the first and (possibly last) line where the text does not actually fit. I need this to work with a UITextView because text entry is required, or I would be done with my Core Text example. Swift answers are acceptable as well as obj-c.

How can I make UIKIt behave as it does with Core Text, or in other words properly word wrap all of the words in the example path provided?

Jeshua Lacock
  • 5,730
  • 1
  • 28
  • 58
  • AFAICS NSLineBreakByWordWrapping is ambiguous... the docs say it won't break if there's not enough space for a single word, but it doesn't say what it *does* do in that case: fall back to character break, or not putting anything at all on that line. FWIW you can set your UITextView's textContainer to be a custom NSTextContainer that does line breaking the way you want. – Reuben Scratton Feb 16 '16 at 23:32
  • I don't understand why it works no issue wit Core Text (didn't have to set anything special), I would have thought that was what was underlying the UITextView. I tried using a custom text container with no difference, but I suppose I must not have been over riding what was needed. – Jeshua Lacock Feb 16 '16 at 23:36
  • If you could possibly show me an example of what you mean I could consider accepting it. – Jeshua Lacock Feb 16 '16 at 23:37
  • Sure, I'll knock up a demo app. – Reuben Scratton Feb 17 '16 at 14:44
  • Terribly sorry, it's harder than I thought. I need NSTypesetter to modify the soft break behaviour but that's not available on iOS. I can't find a way to bend TextKit to do what you want... – Reuben Scratton Feb 17 '16 at 23:00
  • @ReubenScratton Thanks for trying. Sumbitted to Apple Radar: ticket 24748182. – Jeshua Lacock Feb 19 '16 at 21:21
  • @Jeshua Lacock did you find a solution? how did you use it with core-text? I don't need user input so I was wondering if you can share a sample code – George Asda Jan 14 '17 at 12:53
  • @GeorgeAsda Apple marked my radar report as a duplicate, so I have no way to track the issue. – Jeshua Lacock Jan 14 '17 at 22:11
  • @Jeshua Lacock well Apple being Apple they may not even look into this... How did you manage to achieve the first of your screenshots? – George Asda Jan 14 '17 at 22:16
  • @GeorgeAsda it is done with CoreText instead of using a UITextView - so all the "free" features such as text editing, copying and pasted, etc. etc. aren't implemented. – Jeshua Lacock Jan 15 '17 at 03:30

0 Answers0