Smoky: UITextView, UITextViewDelegate, UITextInputTraits

   

A UITextView is like the UITextField we saw here, except that a UITextView can hold many lines of text. It can even scroll up and down because it is derived from class UIScrollView. I gave the UITextView a view controller, and a tab bar controller on top of that, because everyone expects to see a navigation bar with a Done button.

Source code in Smoky.zip

  1. main.m
  2. Class SmokyAppDelegate
  3. Class ViewController also acts as the UITextView’s delegate. It contains the right bar button item, “Done”.

The layout

The navigation bar is is 44 pixels (or pixel pairs) high in portrait orientation, 32 in landscape. The keyboard is 216 = 4 × 54 pixels high in portrait orientation, 162 = 3 × 54 in landscape.

The delegate

The delegate of the UITextView is the ViewController. See the textViewShouldEndEditing: and textViewDidEndEditing: methods of class ViewController.

Write the string into a file

The textViewDidEndEditing: method of class ViewController writes the text of the UITextView into a file named lyrics.txt. I had to use NSUTF8StringEncoding because NSASCIIStringEncoding gave me the error message “The file "lyrics.txt" could not be saved using text encoding Western (ASCII).”

You can search for lyrics.txt in the finder, or in Terminal.app with

find / -name lyrics.txt 2> /dev/null
/Users/nyuuser/Library/Application Support/iPhone Simulator/5.0/Applications/4179C056-DD8E-4F38-BCB7-38672121A658/lyrics.txt

We have to cat the file with 'single quotes' because the filename contains white space.

cat '/Users/nyuuser/Library/Application Support/iPhone Simulator/5.0/Applications/4179C056-DD8E-4F38-BCB7-38672121A658/lyrics.txt'

Things to try

  1. Use NSTemporaryDirectory instead of NSHomeDirectory.