Stanford CS193p: Developing Applications For iOS Fall 2013-14
Stanford CS193p: Developing Applications For iOS Fall 2013-14
Today
UITextView
Scrollable, editable/selectable view of a mutable attributed string.
Finding out whats happening as a VC is created, hooked up to the View, appears/disappears, etc. The radio station communication mechanism of MVC. Today well just talk about tuning in.
Demo
UITextView NSAttributedString, NSMutableAttributedString, NSTextStorage UIFont, UIColor NSNotification
UITextView
UITextView
Like UILabel, but multi-line, selectable/editable, scrollable, etc. Obtain the NSMutableAttributedString representing the text in the UITextView using ...
@property (nonatomic, readonly) NSTextStorage *textStorage; NSTextStorage is a subclass of NSMutableAttributedString. You can simply modify it and the UITextView will automatically update. New in iOS 7.
Fonts can, of course, vary from character to character in UITextView. But there is also a property that applies a font to the entire UITextView ...
@property (nonatomic, strong) UIFont *font;
Changing the font of every character will not reset other attributes (e.g. color, underline, etc.). However, you will lose your symbolic traits!
Stanford CS193p Fall 2013
UITextView
Advanced layout in UITextView with TextKit
This property denes where text can be in the UITextView ...
@property (readonly) NSTextContainer *textContainer;
This object will read characters from textStorage and lays down glyphs into textContainer ...
@property (readonly) NSLayoutManager *layoutManager;
These objects are quite powerful. For example, textContainer can have exclusion zones specied in it (owing around images). Check them out if youre interested in typography.
Demo
Attributor
UITextView NSAttributedString, NSMutableAttributedString, NSTextStorage UIFont, UIColor
What then?
But be careful because the geometry of your view (its bounds) is not set yet! At this point, you cant be sure youre on an iPhone 5-sized screen or an iPad or ???. So do not initialize things that are geometry-dependent here.
Your view will only get loaded once, but it might appear and disappear a lot. So dont put something in this method that really wants to be in viewDidLoad. Otherwise, you might be doing something over and over unnecessarily. Do something here if things you display are changing while your MVC is off-screen. Later we will use this to optimize performance by waiting until this method (as opposed to viewDidLoad) to kick off an expensive operation (probably in another thread). Views geometry is set here, but there are other (better?) places to react to geometry.
And you get notied when you will disappear off screen too
/ / call super in all the viewWill/Did... methods / / lets be nice to the user and remember the scroll position they were at ... [self rememberScrollPosition]; / / well have to implement this, of course / / do some other clean up now that weve been removed from the screen [self saveDataToPermanentStore]; / / maybe do in did instead? / / but be careful not to do anything time-consuming here, or app will be sluggish / / maybe even kick off a thread to do what needs doing here (again, well cover threads later)
[super viewWillDisappear:animated]; }
Geometry changed?
Autorotation
This property will have the current orientation when each of the above is called ...
@property UIInterfaceOrientation interfaceOrientation;
Its pretty rare to implement these. Autolayout and viewWillLayoutSubviews usually sufce.
Stanford CS193p Fall 2013
Summary
awakeFromNib viewDidLoad
didReceiveMemoryWarning
Demo
Attributor
View Controller Lifecycle How (and when in the VCL) would we outline the text on the Outline button?
NSNotification
Notications
The radio station from the MVC slides.
NSNotificationCenter
Get the default notication center via [NSNotificationCenter defaultCenter] Then send it the following message if you want to listen to a radio station: - (void)addObserver:(id)observer / / you (the object to get notied)
selector:(SEL)methodToInvokeIfSomethingHappens name:(NSString *)name / / name of station (a constant somewhere) object:(id)sender; / / whose changes youre interested in (nil is anyones)
NSNotification
Be sure to tune out when done listening
[center removeObserver:self];
or
[center removeObserver:self name:UIContentSizeCategoryDidChangeNotification object:nil];
Failure to remove yourself can sometimes result in crashers. This is because the NSNotificationCenter keeps an unsafe retained pointer to you. A good place to remove yourself is when your MVCs View goes off screen. Well see how to nd out about that later in this lecture. Or you can remove yourself in a method called dealloc (called when you leave the heap).
- (void)dealloc {
/ / be careful in this method! cant access properties! you are almost gone from heap!
[[NSNotificationCenter defaultCenter] removeObserver:self]; }
Stanford CS193p Fall 2013
NSNotification
Watching for changes in the size of preferred fonts (user can change this in Settings) ...
NSNotificationCenter *center = [NSNotificationCenter defaultCenter]; [center addObserver:self selector:@selector(preferredFontsSizeChanged:) name:UIContentSizeCategoryDidChangeNotification
Example
Demo
Attributor
NSNotification
Next Time
Wednesday
Multiple MVCs in one application
UITabBarController UINavigationController
Using inheritance with Controllers Assignment 3 Handed Out (due next Wednesday)
Friday
No Section