Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating title from first line of UITextView (Think Apple Notes app) Swift

I'm trying to set the title of my notes using the first line out of a UITextView. Only the last section of my code isn't working.

(If you're wondering why I'm using an "else" in addition to the 30 character statement it's because if I don't put in a note with at least 30 characters there's an error)

 override func viewWillDisappear(animated: Bool) {
        detailItem!.noteText = self.textView.text


        if !self.textView.text.isEmpty {
            var textViewString:String = self.textView.text

            if let range = self.textView.text.rangeOfString("\n") {
                let rangeOfString = self.textView.text.startIndex ..< range.endIndex
                let firstLine = self.textView.text.substringWithRange(rangeOfString)

                detailItem?.noteTitle = firstLine
            } else {
                // take up to the first 30 characters as the title
                let length = count(self.textView.text)
                if length > 30 {
                    let firstLine = (textView.text as NSString).substringFromIndex(30)
                    detailItem?.noteTitle = firstLine
                } else {
                    let firstLine = (textView.text as NSString).substringFromIndex(length)
                    detailItem?.noteTitle = firstLine
                }
            }
        }

So the code that doesn't work is this last part:

                    } else {
                        let firstLine = (textView.text as NSString).substringFromIndex(length)
                        detailItem?.noteTitle = firstLine
                    }

Questions:

1) What is the difference between a plain old int and a variable that's an int?

2) What is a work around to achieve the same result if my method is impossible?

like image 483
Wesley Cho Avatar asked Aug 06 '15 00:08

Wesley Cho


2 Answers

Use .substringToIndex( ) instead of .substringFromIndex( ), and you will then get the right result.

like image 181
haopeng Avatar answered Nov 20 '22 16:11

haopeng


I had the same problem and I found this way that works really well. You have the explanation in the code comments:

//If it's empty will return this values

    if self.txtText.text.isEmpty {

        title = "(Empty Note)"
        texto = "(Empty Note)"
    }

    //If it's not...
    else
    {
        //We provide the value "\n" to say which will be te last character
        let till: Character = "\n"
        if let idx = texto.characters.indexOf(till) {

        //Get the position of this character
            let pos = texto.startIndex.distanceTo(idx)

        //Substract 1 to not add the \n to the title
            firstLine = (txtText.text as NSString).substringToIndex(pos-1)
        }
        else {

            //If it's longer than 30 chars, get 30
            if texto.characters.count >= 30{
                firstLine = (txtText.text as NSString).substringToIndex(30)
            }

            //If it's shorter, count the chars and get till the last of this line
            else{
                firstLine = (txtText.text as NSString).substringToIndex(texto.characters.count)
            }
        }
        title = firstLine
    }
like image 1
Victor Rius Avatar answered Nov 20 '22 16:11

Victor Rius