Skip to content

Making some lines of text un-editable #27

@mbcoder17

Description

@mbcoder17

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
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions