0
let ss = CATextLayer()
  ss.frame = rect
  ss.backgroundColor = UIColor.blue.cgColor
  ss.foregroundColor = UIColor.cyan.cgColor
  ss.string = mytextView.text
  myImage.layer.addSublayer(ss)

I am trying to add an editable textview as a sublayer of an imageview.the problem is, i am not able to edit and also not able to add gesture recognisers for this textlayer. How can i make this textlayer do exactly what a textview does.

Faheem Rahman
  • 343
  • 2
  • 10

1 Answers1

1

Just use UITextView or UITextField and add it as subview to the UIImageView, something like:

let textView = UITextView()
//configure text view like you want
//add constraints or size it as you want
myImage.addSubview(textView)

Remember UIImageView is just another subclass of UIView so you can add subviews to it like for a regular UIView.


Going on to what you are dealing with, since some of the views you add are CALayers and some will be UIViews or subclasses of both (for instance UITextView is UIView subclass)

I would add two properties to your class:

var addedViews = [Any]()
var undoStack = [Any]()

I know this is not very Swift like since you can put anything into these two arrays but still.

Then when you create a new layer or view you also add it to addedViews array:

let layer = CAShapeLayer()
layer.frame = CGRect(x: 30, y: 30, width: 100, height: 100)
layer.backgroundColor = UIColor.red.cgColor
layer.transform = CATransform3DMakeRotation(0.2, 0, 0, 1)
addedViews.append(layer)

So addedViews array will hold references to all the views you added, so when you undo you can do just the following:

if let viewLayer = addedViews.last {
    if let view = viewLayer as? UIView {
        view.removeFromSuperview()
    } else if let layer = viewLayer as? CALayer {
        layer.removeFromSuperlayer()
    }

    undoStack.append(viewLayer)
    addedViews.removeLast()
}

If you then want to redo the change you do the same thing but you get the last view from undoStack like so:

if let viewLayer = undoStack.last {
    if let view = viewLayer as? UIView {
        self.view.addSubview(view)
    } else if let layer = viewLayer as? CALayer {
        view.layer.addSublayer(layer)
    }

    addedViews.append(viewLayer)
    undoStack.removeLast()
}
Ladislav
  • 7,223
  • 5
  • 27
  • 31
  • I did the same. But the problem is i am using cashapelayers also as sublayers of the same image.When i undo the last layer, the subview is causing error. – Faheem Rahman Jul 12 '18 at 09:19
  • You do what? Can you please explain one more time, I have no idea what `cashapelayers` is and what you mean by `undo the last layer` – Ladislav Jul 12 '18 at 09:28
  • i am making an image editer. So i am adding layers over image. Its. having undo and redo functionality. So when i do the undo function, the subview and sublayers are acting differently. I am removing the last added layer, but subview is not removed. – Faheem Rahman Jul 13 '18 at 04:50
  • Why not have an array of all views added - could be both views and layers and then when you need to undo, you just get last one added and remove it – Ladislav Jul 13 '18 at 15:20
  • I am doing it like that only. I am removing the layer like this, var c = documentImage.layer.sublayers?.count sublayerarray.append(documentImage.layer.sublayers![c! -1]) Myimage.layer.sublayers?.remove(at:c! - 1) – Faheem Rahman Jul 17 '18 at 06:16
  • You use `sublayerarray.append(documentImage.layer.sublayers![c! -1]) ` for undo functionality? so you can add it back in? – Ladislav Jul 17 '18 at 07:08
  • It is ok, just trying to understand what you are trying to do...will try to give you a solution in about an hour – Ladislav Jul 17 '18 at 07:50
  • ok, i. think the problem is that i am using calayer and sublayer simultaneously – Faheem Rahman Jul 17 '18 at 09:53
  • I am doing the same. but the problem is i how will you add a textview with these layers and do the same undo and redo. – Faheem Rahman Jul 17 '18 at 10:37
  • I showed you how, just add it to addedViews when you add it like i did above for the layer, it can hold layers and views – Ladislav Jul 17 '18 at 10:38
  • Check the undo and redo functionality notice when we get the viewLayer we first check if it is of type UIView or CALayer and do the right thing in both cases – Ladislav Jul 17 '18 at 10:47