YouSub
Untitled

en English

Untitled English subtitles

0 downloads · 1,627 lines · added Nov 30, 2015

Report a problem
What's wrong?
Advertisement

English transcript

>> Stanford University.
>> Okay. Well, welcome to CS 193P fall of 2013/2014.
Today we're going to continue our discussion
of attributed string from last time and talk about UITextView,
which is basically a mutable attributed string viewer, okay?
It's kind of like a UILabel but much more powerful.
And we'll talk about that.
Then we're going to talk about a very important kind
of conceptual thing in iOS 7 -- or in iOS in general --
which is view controller lifecycle.
So that's just the lifecycle of the controller part of your MVC
and how it gets notified at different times in its lifecycle
about what's going on.
Then we're going to talk about the radio station
that I referred to in MVC.
We're going to talk about it in a little different context
that we see in MVC because I just want
to introduce it to you now.
And then throughout today's thing --
so I'm going to be -- slides here.
I'm going to be stopping every once and while
and we'll do a little demo.
And then we will move back to the slides and back and forth
as we cover various topics.
Okay? So let's start with UITextView.
Like UILabel in that it displays text but way more powerful
because the text is multiline.
UILabel, if you want it to be multiline, you kind of have
to say how many lines it is in advance, whereas UITextView,
as many lines as it needs.
It's also scrollable, editable, or just selectable if you want.
And of course, you get all the mutable attributed string
setting of various things like colors and all that stuff.
So UITextView, super powerful object but very easy to use.
The way you use it is that it has a property called
"text storage," which is an NSTextStorage.
NSTextStorage is a subclass of NS mutable attributed string.
So you get this text storage,
and you can just start setting attributes or if it's editable,
the user will start editing it
and the attributes will just show up on this mutable string.
It's a super great programming interface new in iOS 7.
Okay. It's having a mutable attributed string be just vended
like this by UITextView and editable on the fly
by both the user and by you is all new
and it's really incredibly awesome and easy to use.
There are some methods on UITextView like, you know,
property font where you can just set the font.
Understand, though, that when you set the font,
all it's doing is going through every character
in the mutable string
and setting the font attribute name --
font, you know, that thing -- to this font.
So remember that bold and italic are attributes of the font.
Okay? And so is the size, actually.
So if you call this method set font --
you know, call the set font setter there --
it's going to blast all of your bolds,
and italics, and size, okay?
So you be a little careful of that.
If you want to set the font, though, of every character
and have that stuff preserved, just do a little for loop,
go through all the attributes in the mutable string,
get the font that's already there.
You can grab the symbolic traits that are off
of it using the symbolic traits method in UIFontDescriptor
and then create a new font that is the font you're trying
to set, plus those traits,
and then set it back as the attribute.
So a little for loop there to do that.
But I just want you to be careful about methods
like set font, which are going to set the font attribute
of all the characters, okay?
UITextView has incredibly advanced text layout mechanisms.
For example, you can specify the container that the text is going
to be in and it can even have exclusion zones.
So if you had an image that was dropped in the middle
of your text and you wanted text to flow around it,
even if it's funny-shaped thing -- you know, image --
you can flow around it.
And you just -- it's so easy to do it.
Unbelievable.
You just create a Bezier path
that encompasses the thing you want it to flow around.
You just set that as an exclusion zone
in the text container property here of UITextView,
and it will just flow around.
So we're not going to talk about that advanced stuff.
It's all part of Text Kit, which is new for iOS 7.
The layout manager is the thing that lays out the glyphs.
So it takes all the characters and all the attributes and stuff
and it laying out the glyphs one by one.
For those of you who don't know what glyphs are, they're kind
of how you represent a character or a sequence of characters
on screen with some things.
So the layout manager is the thing that lays the glyphs
out inside the text container.
So if you're interested in typography or you're going
to do a final project that has a lot of text layout and stuff
in it, this is where you get started there.
Okay? So let's have a demo where we kind
of combine all the things we saw
in last lecture in this UITextView.
I'm going to call this demo "Attributer" because we're going
to be doing attributed strings here.
And it's going to look like this.
It's going to be a totally new app,
totally unrelated to Machismo.
So I'm going to say create new project.
Let's go ahead and also hide others here.
Well, we'll do it in a second.
So I'm going to do a single view application.
We almost always start with that, a single MVC.
This demo I'm eventually going to make
into a multi-MVC application at the next lecture,
but we're going to start here with this.
So I'm going to call it Attributer.
I'm going to have the class prefix be attributer
so that our controller is called "attributor view controller."
Just do that.
I'm going to put it in Developer,
same place I have Machismo.
This is my home directory, Developer.
I'm not going to use source control.
And here we go.
So this is just a regular app just as you're used to.
Again, I like to move these delegate things
into supporting files because we're not really going
to be doing the delegate.
This quarter we might actually do the delegates
when we start talking about multitasking in iOS 7.
But for most of the time you can just kind
of move that out of the way.
Here's my blank storyboard.
Again, I'm going to go down to small size instead
of the tall size just so it fits.
Okay, there's no reason to do that, except for that it fits.
By the way, you're welcome to use the larger size
in your homework if you want because as I ask you to do more
and more in the homework, then you kind of need more
and more space, which is fine.
Of course, we need to think a little bit
when we design user interfaces that this application might run
on some users on smaller phones and on some users
on larger phones, right?
So we kind of need a user interface that can stretch.
And next week we'll be talking about autolayout,
which is a mechanism in iOS for you to design a user interface
and specify how it stretches and shrinks, okay,
which is really important.
And not just on different size iPhones but different devices --
iPads, you might be using them
in a little window inside an iPad, etc. We want
to make our views of our MVCs be shrinkable
to at least some extent.
And of course, don't forget device ration.
When you turn your device,
it all of a sudden gets wider than it is tall.
Okay? Well, everything needs to move
around to deal with that, too.
Okay? So we'll talk about all of that next week.
So let me show you the user interface I want
to build here first so that you can kind of understand
where I'm headed, and then we'll write the code to do it.
It's going to be primarily centered around a UITextView.
So here I am looking at all my various things
that I can drag out.
So I'm going to go down here to UITextView,
which is right -- where is that thing?
Too far down.
UITextView -- I can't find it.
It's right there.
Okay? So UITextView displays multiple lines of text.
So I drag it out.
By default it wants to be the whole size of the screen.
But I'm actually going to put some other buttons and stuff
in here, so I'm going to resize this to be smaller.
I also want it to have a little bit of edges here.
Now, notice that as I do everything the UI,
I'm using these blue lines.
And I haven't really told you exactly why that is,
but when we start talking
about having these flexible-sized views
that are going to want to resize,
these blue lines play a crucial role.
And so when you're building your UIs, use the blue lines as much
as possible because then when we start doing autolayout,
the blue lines are going to help the autolayout system kind
of get a clue as to what they should be doing.
All right.
So I'm going to have this text view here.
And what I want to do is have some text.
And I'll just use the default text.
If you look over here when you create a text view,
you see you get this kind of pseudo-Latin text here.
Then I'll just use that as the default.
So that's going to be the text that's going
to be in this one I run.
And if I run this, I'll just get this text.
But what I want to be able to do is select some of the text.
So I'm going to make this text selectable, okay --
not editable, though; just selectable.
And so I can select words in here.
And I want to be able to change the color of the words,
maybe put a little outline around the characters, okay?
Do some fun stuff that we can do with attributed string in here.
And what am I going to allow us to do?
Well, let's see.
Let me go get some buttons down here at the bottom.
How about let's allow outlining.
So I'm going to create a button down here called "outline."
And of course, let's also let us unoutline.
So I'll create another button up here.
I'll call it "unoutline."
That's probably not a word, but we know what we want.
Again, I'm going to use the blue lines to try and get things,
you know, lined up as much as possible.
This guy, let's put him right in the corner.
These guys will line the base lines up.
That's what the double line right there means,
that the baseline of the other button line up.
And we got, also, this right line was lined up.
So they're going to stick to the edges and stay lined up.
So we'll allow outlining and unoutlining.
And then also, I'm going to allow setting colors.
And the way I'm going to do that is kind of fun.
So let's get a button out here.
This is a good way to talk about if I'm resizing,
I want this button to be 64 by 64.
I know that that's a good size.
And I could try and get it exactly the size I want.
But it's actually much easier -- if you know the size you want --
to go over here in the size inspector.
You see this size inspector?
And you can just type in, "I want 64 by 64," okay?
So this size inspector, we're going to see
when we start talking about autolayout a lot there's going
to be a lot of stuff down here,
describing how this thing gets constrained
when things change size.
But it's also good for setting the size.
And then once the size is set,
I'll move into a nice spot like that.
Now, I could put, like, the word "red" here
and make this a button that has the word "red" on it.
And I could make the text be red.
That's what I want this button to do.
When I click here, I want whatever's selected to turn red.
But instead of using words, I'm actually going to use the color.
I'm just going to go to the inspector.
And since a button inherits from control and control inherits
from view -- see how that inheritance hierarchy is working
in the inspector -- I'm going to go down here
and change its background color, which is the same thing we did
when we set the background of the whole view to green.
And I'm going to set this one to -- let's say this one's red.
Okay? So now I have a nice red button here.
And when I click on this, I'm going to have to wire it
up to make this turn red.
So let's make some more buttons here of different colors.
Again, I'm using the blue lines.
See how they're snapping in place?
Okay. Getting blue lines --
definitely want blue lines as much as possible.
So here let's make this background be green.
I'll make this one be orange, make this one be --
oh, I don't know -- purple.
Okay? So now we have some colors and some outlines.
And then let's also make this thing --
well, let's put a headline at the top here, too.
So let me grab another button --
actually, let's do a label for this one
because we're not going to make it clickable.
So I'm going to put a label here at the top.
I'm going to put something like "CS 193P Rocks."
Okay. So that's just going to be my title of this thing.
I want it nice and centered.
Let's move this up so that it's locked.
Let's resize it so it's maximum space.
It locks. And so now I've got this nice user interface.
I've used blue guidelines for everything;
everyone's sticking on a blue guideline.
That's going to be really advantageous for us when we try
to do autolayout with it.
Now, let's talk a little bit
about the fonts I want to use here.
And I told you in the slides
that when you're displaying user content, okay,
you want to use the preferred fonts of the system.
Okay? And that's what I'm going to do, both for this guy.
This is clearly the user content.
These two buttons are not user content.
They're going to stay the system fonts, okay,
because they're buttons.
But this title and this body are kind of user contents.
You could argue that the title is not quite user content,
but it kind of is.
It might get localized to another language
or it might change, depending on what's going on.
I don't know.
But it's really kind of a headline.
So I'm going to use the headline font
to display this guy right here --
actually, I also want this centered.
All right.
So let's make sure that's centered.
And so the way you pick the headline font,
the preferred font which is headline,
is you inspect the label and you go here to the fonts.
And you can see that it's currently set
to be a system font.
And I'm going to go down here to text styles and pick headline.
And that's basically in the code like saying preferred font
of a text style headline.
And you can see it change there.
Kind of got a little bold to it.
It's now the headline font.
And the same thing here, the text view, I'm also going
to set it to use a preferred font.
But in this case I'm going to use the body font
because this is clearly, like,
the body of this content in this window.
So now I've set these things to have the preferred fonts rather
than the system fonts.
And that's pretty much all that's required to set that up.
So that's my UI.
All right?
So yeah, question?
>> In iOS is there an easy way to round out the corners
of those buttons, or do you have to go through,
like, a lot of masking?
>> Yeah, so the question is: If I wanted these to be, like,
rounded rect buttons -- which it's not really a thing in iOS 7
to have rounded rect buttons, by the way; it was a thing
in iOS 6, but not so much a thing in iOS 7 --
how difficult would this be?
And the answer is it wouldn't be that bad.
It wouldn't be that hard.
There's a mechanism.
Rounded rect is a one-liner to create a rounded rect.
And probably we wouldn't make these UI buttons;
we'd just make them views.
And it's really a one-liner to have a tap gesture, to tap on it
and cause it to do something.
So, you know, a few lines
of code is really not that difficult.
In fact, you can make it not just rounded rect but any shape
that you wanted there, whatever.
Question?
>> Is there any way to include, like, CSS in it though or no?
>> So the question is: Is there any way to include CSS?
Not really directly.
However, these preferred fonts, for example,
are plugged into the CSS system.
So if you have content in your app that is coming
from CSS source, you know, on a web view or something --
we haven't talked about any of that yet, but you could do
that -- the fonts can match up
and all synchronize with each other.
So there's some synchronicity there,
but you can't directly display.
Okay. So let's look at the code
to make this user interface do what we want.
So I'm going to get my system editor up here.
Let's put that right on the edge.
Let's make some more space so you can see more code.
And you can see that my default view controller has this method
here, viewDidLoad, which is part of the view controller lifecycle
that we're going to talk about in a second
and also did receive memory warning,
which is not strictly speaking part
of the view controller lifecycle but I'm going to talk about it
as part of the view controller lifecycle.
I'm not going to demo that today,
but I am later going to demo viewDidLoad.
So we'll leave that in there for now.
Right now it doesn't do anything except for call [inaudible].
Okay?
So what do I need to do here?
Couple of things.
One, I'm going to want
to be setting the mutable string attributes of this thing,
so I need an outlet to this.
So I'm just going to control drag here to create an outlet.
I'm going to call it "body," okay, because it's kind
of like my body text view.
And you can see that it's a UITextView.
And I'm going to be able to send a message to this body
to get its text storage mutable string.
And then I'm just going to start setting attributes.
Okay? It's as simple as that.
You know, I'm also going to grab one to this,
and you'll see why later.
I'm going to call this my "headline," okay?
And so I could call that my "header," or my "heading,"
or "title label," or something like that.
I'm going to call it "headline" just here to emphasize
that we're talking about preferred fonts
because I'm going to be showing that in the demo as well.
So we've got this body in the headline.
We could conceivably set things about the headline.
I don't really have time for that,
but we will be setting things in here.
We've got these colored buttons.
Let's do those first.
So I'm going to control drag here to have an action sent
when we press on that button.
I'm going to call this action "change body selection color
to match background of button."
Okay? Now, you might laugh and say, "Whoa,
that's a long name method."
But long name methods are kind of preferred generally
in Objective-C and especially in iOS because Xcode's going
to help you escape complete these things.
And if you can have a method name like this
that really matches and says exactly what it does,
that can be a good thing.
It's an art of programming thing.
I'm going to change the argument to be a UIButton,
just like we did before in Connect.
So what does this method do?
Well, hopefully with that long name it's clear what
that does, right?
It changes whatever the selection is
in our body right here, it changes the color of that
to match the background view of this button.
Okay? That's what this method is going to do.
This method is one line of code.
Okay? So let's look at that one line of code.
We need to set attributes of the mutable string of our body.
So I'm going to say self.body.text storage.
Now I have the NS mutable attributed string --
text storage is a subclass of it -- but it is an NSMutableString.
And I just want to add an attribute,
which is the foreground color of whatever's selected there.
So the attribute is called
"NS foreground color attribute name."
Okay? The value -- that's the color we want to set it to --
well, I said we're going to have it match the background
of the button.
So I'm going to say sender,
which is the button that's sending this thing,
background color, okay?
And then the range is what range
of this text view's storage do I want to set to be that color?
Well, I said I want it to be the selection --
the body selection -- so there's a method
in text view called "selected range."
And it returns an NSRange of what's selected, okay,
what the user selection is.
Okay? So you can see one line of code, and we've got our color --
all four color buttons --
working as long as they're all wired up, which we know
that this one is, right?
Okay, because we wired that one up.
There it is right there.
We haven't wired this one up, but let's do that.
And this one.
Okay? So let's go ahead and run.
We'll try and see if we can run it on the device
over here if that works today.
Hopefully no technical issues will arise.
Now, this is an iPhone app that we're running here,
and we're running it on an iPad.
My demo machine happens to be an iPad.
So this i
…

Need subtitles from another video?