1

I am creating a paint app in Xcode using swift. I can make the painting functionality work but I now need the user to also be able to add text. The goal is to allow the user to be able to tap on a "T" button and then to tap anywhere on the screen to make a text box appear which they can put text into. Ideally, they should also be able to reposition and delete this text box too.

Does anyone have any clue as to how I can go about this?

I'm completely stuck and cannot find any information online currently.

3 Answers3

1

You can add a text field at the location of the tap (by detecting the tap's x-y coordinate), just as you would add a text field otherwise.

Adding Text Field: Adding UITextField on UIView Programmatically Swift

or you can just try var label = UILabel(frame: CGRect(x: 0, y: 0, width: 1, height: 1))

Find x+y coordinate of a tap/touch: Is it possible to get the x and y coordinates of a touch?

0

When the user taps on "T", you addSubview(textField) and when pressing "Done" on the keyboard or when they tap anywhere outside, you either just remove the borders of the textfield, or you remove the textfield and add a label in the same position and size

0

I suggest you first create a function to get user tap position (CGPosition, X, Y), and also create a bool variable to check if [T] text button is active or inactive (touched or not). Then proceed from these two values to create a subview in that location of a UITextView, or UITextField maybe. I would probably add the position to the subview using the frame property instead of using constraints, so to do this turn off -->

textView.translatesAutoResizingConstraints = false
drfalcoew
  • 615
  • 5
  • 14