1

I have a problem with UITextKit using exclusionPaths: when I place an image inside a TextView, that I’m editing, first everything looks fine:

enter image description here

But when the „Done“-Button is tapped, it will look like this, the ident is wrong:

enter image description here

This is the class to store the informations for the image

class ImageClass {
    var image: UIImage!
    var imageView: UIImageView
    var imageName: String!
    var active:Bool
    var exclusivePath: UIBezierPath!

    init(image: UIImage!, imageName: String!) {
        self.image = image
        imageView = UIImageView(image: image!)
        self.imageName = imageName
        self.active = false
        self.exclusivePath = nil
    }
}

The user chooses the image in a UICollectionView that fires a delegate, when the image was selected. (imageViewObjects is an array, where I can put objects of ImageClass, so I can use mutiple images)

// MARK: - SelectImageDelegate
func selectedImage(name:String) {
    let image = UIImage(named: name)
    let imageViewObject = ImageClass(image: image, imageName: name)
    self.imageViewObjects.append(imageViewObject)
    imageViewObject.imageView.frame = CGRectMake(4, 7, width!, height!)

    // define the exlusionPaths
    let bezierPath = self.exclusionPathForImageView(imageViewObject.imageView)
    imageViewObject.exclusivePath = bezierPath
    self.updateExclusionPaths()
    self.textView.addSubview(imageViewObject.imageView)
}

// set UIBezierPath for the UIImageView
func exclusionPathForImageView(imageView: UIImageView) -> UIBezierPath {
    let bezierPath = UIBezierPath(rect:
        CGRectMake(imageView.frame.origin.x, imageView.frame.origin.y,
        imageView.frame.width, imageView.frame.height))
    return bezierPath
}

func updateExclusionPaths() {
    textView.textContainer.exclusionPaths = []
    for imageViewObject in self.imageViewObjects {
        textView.textContainer.exclusionPaths.append(imageViewObject.exclusivePath)
    }
}

The context of bezierPath is ok.

When „Done“ is tapped, I stop the editing

   let doneBarButton = UIBarButtonItem(title: "Done", style: .Done, target: view, action: Selector("endEditing:"))

In textViewDidEndEditing() now I do nothing.

I also tried it with a UIButton for the image. Similar result.

Any tip for a solution? plz help me!!!!

Ulli H
  • 1,748
  • 1
  • 19
  • 32

1 Answers1

0

found a workaround:

func textViewShouldEndEditing(textView: UITextView) -> Bool {
    textView.editable = false
}

and I had to add to set textView.editable = true, i.e. in viewDidLoad()

    let tapGesture = UITapGestureRecognizer(target: self, action: "resetEditable")
    tapGesture.numberOfTapsRequired = 1
    self.textView.addGestureRecognizer(tapGesture)

func resetEditable() {
    textView.editable = true
    textView.becomeFirstResponder()
}
Ulli H
  • 1,748
  • 1
  • 19
  • 32