2

I am working on an application that allows the user to add an image and move it around in a UITextView. The text flows around the image using exclusionPaths, but the words will break if there is not enough room on the remainder of the line instead of moving to the next line.

This is what I'm using to add the image and create the exclusionPaths for the text container

textView.addSubview(imageview)
let imagePath = UIBezierPath(rect: imageview.frame)
textView.textContainer.exclusionPaths = imagePath

This is what is happening, notice how the words will break up to the right of the image

This is what I want to happen. The words should move to the next line if there is not enough room on the current line

Thank you for any help with this!

1 Answers1

0

Can you create a rect using imageView.bounds and then give a try not sure but hoping for it to work

Ashish
  • 204
  • 1
  • 8
  • I'm not quit sure if I understand but if you are referring to "UIBezierPath(rect: imageview.frame)" changing "frame" to "bounds," does not help. The issue is the text wrapping at the end of the line and spilting the words up. – Hayden Kreuter Sep 23 '18 at 06:35
  • I think the way you are setting exclusion path need bit modification can you refer the below link and scroll till you see exclusion path section in the article basically they calculate rect using textview.convertRect:fromView method https://www.appcoda.com/intro-text-kit-ios-programming-guide/ – Ashish Sep 23 '18 at 07:22
  • I implemented the textview.convertRect:fromView method into my project but still having the same problem, the text view still seems to be letter wrapping when the image doesn't allow enough room for the word instead of moving the word to the next line. – Hayden Kreuter Sep 23 '18 at 15:45
  • For the time being can you just put self.textView.layoutManager.hyphenationFactor = 1.0, i am also looking if we can get range of words that are getting hyphenated and move them to next line by subclassing layoutManager – Ashish Sep 24 '18 at 07:13
  • Alright thank you, please keep me updated if you find anything else out – Hayden Kreuter Sep 28 '18 at 01:13
  • https://github.com/dwivediashish00/TextviewHtmlSupport i have setup project base so what i did was in storyboard i enabled attributed text property which allowed me to switch line breaking mode to word wrap it kind of worked but then it removed the image from imageview but i am working on that part you can take the sample from my git and experiment a little bit – Ashish Sep 28 '18 at 16:47
  • I'm not sure if I'm missing something but this still does not seem to fix the problem. I enabled the attributed text property and turned line breaking mode to word wrap, but the words were still getting split up when moving the image too close to the edge. Thank you for taking a crack at it though, I appreciate your help! – Hayden Kreuter Sep 29 '18 at 02:38