1

If I have a UITextView with the following text:

"Please finish this sent..."

And the aim is to allow the user to edit the textview so that they can finish the sentence, how can I make it so that they can't remove the text that was there before they start editing aka "Please finish this sent"?

UPDATE: I have been able to make it so that users cannot remove the initial text, but is there any way to make it so that they cannot type before the initial text or inside it? Thanks for everyone who has answered so far!

UPDATE: Solved. To stop people editing before and inside the string I simply added

if range.location == 0 {
            return false
        }

if (0 < range.location && range.location < count(string.utf16)) == true {
            return false
        }

to the shouldChangeTextInRange function. Thanks everyone for the help!

zacreme
  • 169
  • 1
  • 12
  • If you just want them to continue to add to this text view, just set this text. or do you want to prevent them from removing this initial text. – Shamas S Jul 22 '15 at 17:20
  • I want to prevent them from removing the initial text, and only be able to add on to it. – zacreme Jul 22 '15 at 17:23

2 Answers2

2

This way you can stop user to remove the initial text:

import UIKit

class ViewController: UIViewController, UITextViewDelegate {

    @IBOutlet weak var textV: UITextView!

    override func viewDidLoad() {
        super.viewDidLoad()
        textV.text = "Please finish this sent"  //Set your default text.
        textV.delegate = self
    }

    func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {

        let  char = text.cStringUsingEncoding(NSUTF8StringEncoding)!
        let isBackSpace = strcmp(char, "\\b")

        if (isBackSpace == -92) {
            // If backspace is pressed this will call
            if textV.text == "Please finish this sent" {
                return false
            }
        }
        return true
    }
}
Dharmesh Kheni
  • 71,228
  • 33
  • 160
  • 165
  • Is there a way to make it so they cannot write before or inside the initial text? – zacreme Jul 22 '15 at 18:21
  • Yes, just add one more condition for index. – Dharmesh Kheni Jul 22 '15 at 18:24
  • I understand how I would do that to stop them writing at the start (check if the index is 0 and if it is then don't allow them to write), but how would I stop them from writing inside the initial text? Thanks! – zacreme Jul 22 '15 at 18:25
0

Use UITextFieldDelegate's function - textField:shouldChangeCharactersInRange:replacementString:. When this function is called, get text from the UITextField and see if the start of it contains the string you want. If yes, return true, else return false

Shamas S
  • 7,507
  • 10
  • 46
  • 58
  • Does this work with UITextViews, and if so, how do I check what the start of a string is? – zacreme Jul 22 '15 at 17:32
  • @zacreme UITextViewDelegate has the same function as well. For string comparison here http://stackoverflow.com/a/24161872/1891327 – Shamas S Jul 22 '15 at 17:36
  • This would only apply as long as the textview contains that sentence somewhere, but how would I make sure it is at the start of the string, and not just anywhere inside the string? – zacreme Jul 22 '15 at 17:41
  • @zacreme this function would be called right when the first edit begins. But I get your point. You can split the array by index using the code here. http://stackoverflow.com/a/24045156/1891327. Get string for the first X number of characters and compare with your own string. – Shamas S Jul 22 '15 at 17:47
  • 1
    @zacreme The `UITextView` equivalent is [`- textView:shouldChangeTextInRange:replacementText:`](https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UITextViewDelegate_Protocol/index.html#//apple_ref/occ/intfm/UITextViewDelegate/textView:shouldChangeTextInRange:replacementText:). In regards to making sure it's at the start of the text, use `rangeOfString:` on the modified contents of the field and make sure it's found at location 0. – Mean Dinosaur Jul 22 '15 at 18:21