Swift Component Tour – UITextView

In this lesson

  • UITextView basics 
Kyle Roberts
Swift Guru at Large

Kyle's Series

Transcript

Tap on time to skip ahead

00:10

Full age sex set feel her told. Tastes giving in passed direct me valley as supply. End great stood boy noisey often way taken short. Rent the size our more door. Hey, I was just reading an export of my favorite book called Randomly Generated Text. And that’s because we’ve finally reached the TextView stop on our Swift Components Tour. Kyle here with Brax.tv to talk about the UITextView.

00:41

The UITextView is sort of like a mix of the UILabel and the UITextField with some extra functionality. I say UILabel because it’s good at displaying static text. Some of the extra functionality that is included is that the support for a multiline TextView has a different purpose than support for a multiline label. With the multiline label we only displayed four words on four lines. Good for displaying four words, but what if you have a whole chapter out of a randomly generated text book. Well, the UITextView has support for automatically adjusting the content size of itself so that you can scroll through all the content. Now, the frame of the TextView will stay the same. The top is here, the bottom is at the bottom of the screen. The left and the right are with the left and the right of the screen. It will constantly stay that size. But as you adjust the text, or add more or remove more content, it will automatically adjust its content size so that you can scroll down through all of the content.

02:00

I say that it’s like a text field mainly because it is also editable. You can just tap anywhere, I have this one set to be editable, you can also remove that functionality. You can type in it. You can add lines. You can do whatever. We can see that it automatically adjusted so that we can still scroll to the very bottom and the top even though we added several lines in the center of the content. It may also be important to note that the delegate for the UITextView has some pretty similar methods to the UITextField. We aren’t going to get into that much today but it’s good to know. How about we check out the storyboard?

02:52

Now, this view is a little simpler compared to others in this app and that is because there is only one object in this scene. That one object is the UITextView. All it takes is to drag a TextView from down here onto the scene, adjust the size and position to how you want. Figure out the constraints and all that, then paste the text in here. What I did was I just got a bunch of paragraphs from a random text generator and pasted them in. We can also set whether it is editable or selectable, working with the keyboard, and scrolling. It might also be good to note that the Y value of the Textfield does not start at zero. I actually have it 20 pixels down right here. We can see in the simulator that there’s a little bit of padding here. It’s hard to tell but as you scroll up, there’s this feature in iOS where you can see through the navigation bar a little bit when there’s content under there. What is happening is that iOS 8 is automatically adjusting the text content to be at this position even though the Y value is somewhere up here. But that’s nothing to worry about, it’s just something fancy included in iOS 8 that you can avoid with a little extra work. Let’s look at the code.

04:27

All that’s going on in the code is that I’m adding some observers from NSNotificationCenter on these keyboard events. When the keyboard does show, we can still scroll to the very bottom of the content. And when the keyboard goes away, the bottom of the content will go back to the bottom of the screen.

04:49

There’s also a few more things you can do with the UITextView. The most impressive, I would say of those, is support for inline content. Mainly what that means is that you can add an image into the UITextView and customize how the text will wrap around that image. Say if you have any image in the center of the screen here, the text can be on both sides. The text can stop here and then continue down here after the image. You can do quite a few things with it. It is pretty neat but it can get a little more advanced so we’re not going to worry about that for today. I hope you learned something and thanks for watching!