Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Making some lines of text un-editable #27

Open
mbcoder17 opened this issue Jul 10, 2015 · 3 comments
Open

Making some lines of text un-editable #27

mbcoder17 opened this issue Jul 10, 2015 · 3 comments

Comments

@mbcoder17
Copy link

When the user selects a line, or tries to edit/delete text, I check the current line against a rule of mine, and use the textView delegate method shouldChangeTextInRange (returning false) to ensure that specific lines of text cannot be changed by he user. However, I have run into some issues with line numbers becoming negative and/or incorrect in the process. For example, let's say I want the last 2 lines of my textview to be un-editable, I save the total number of lines, and I fetch the current line in the shouldChangeTextInRange method. If the current line is greater-than or equal to the total number of lines-1, return false. Now, this works if I select the last two lines and try to edit, it will stop me. But if I select the line above (which happens to take up 2 lines on the device) and start deleting, the line numbers become all messed up when I start to add new lines. Here is my code:

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

    let myCharRange = self.textView.myLayoutManager.characterRangeForGlyphRange(range, actualGlyphRange: nil)
    let myParaNumber = self.textView.myLayoutManager._paraNumberForRange(myCharRange)
    let myInt = self.textView.myLastLine.integerValue

    if myParaNumber+1 <= 12 || Int(myParaNumber+1) >= myInt-1 {

        return false
    } else {

        return true
    }
}
@mbcoder17
Copy link
Author

Here is a sample project I made that shows the issue: https://www.dropbox.com/s/crpbw49rvv5s4q0/CYRTextView-master-2.zip?dl=0

@mbcoder17
Copy link
Author

Here are steps to reproduce on a simulator and device:

Simulator:

  1. Run the example (iPhone 5s) and select the line that contains the text “on one chart"
  2. Press enter and then use the up arrow to move the cursor up one line, you should see that the first line number is now “0”
  3. Now select the line that contains the text “on one chart” again, and press enter and press the up arrow key, and the line numbers will now start with -1.

Device:

  1. Run the example and select the line that contains the text “on one chart"
  2. Press enter try to enter text
  3. Now, tap once on the line and press “Select” from the menu that pops up, and the line numbers should now start at 0 instead of 1.
  4. Repeat to move into the negatives.

@mbcoder17
Copy link
Author

@illyabusigin were you able to figure this out?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant