標籤:
第五課:
1、UITextView
@property (nonatomic, readonly) NSTextStorage *textStorage;//注意為唯讀屬性,因此不能直接更改內容,NSTextStorage為NSMutableAttributeString的子類,因此可以更改字串屬性內容(而非字串)//例如,添加屬性[self.body.textStorage addAttribute:NSForegroundColorAttributeName value:sender.backgroundColor range:self.body.selectedRange];@property (nonatomic, strong) UIFont *font;//例如添加字型self.body.font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody]; self.headLine.font = [UIFont preferredFontForTextStyle:UIFontTextStyleHeadline];//文本布局功能,文混排等@property (readonly) NSTextContainer *textContainer;@property (readonly) NSLayoutManager *layoutManager;
2、View Controller Lifecycle
- (instancetype)initWithNibName:(NSString *)name bundle:(NSBundle *)bundle;//非storyboard載入方式,指定初始化器- (void)awakeFromNib;//從storyboard中載入UI時調用,此時未設定輸出口//設定輸出口- (void)viewDidLoad;//可以放置控制器初始化代碼(一次性),如init。不可放置與幾何相關的代碼,此時控制器中的UI邊界未確定//確定幾何布局//當視圖frame發生變化時就會被調用,此處適合添加與幾何相關的代碼- (void)viewWillLayoutSubviews;- (void)viewDidLayoutSubviews;//其他關於旋轉螢幕等API...- (void)viewWillAppear:(BOOL)animated;//生命週期中可能會被反覆調用,因此不能放置一次性初始化內容- (void)viewDidAppear:(BOOL)animated;- (void)viewWillDisappear:(BOOL)animated;//可以進行一些當前資料儲存工作- (void)viewDidDisappear:(BOOL)animated;- (void)didReceiveMemoryWarning;//系統在記憶體不足時調用
3、NSNotification
(本節課只涉及到如何收聽通知)
[NSNotificationCenter defaultCenter];//擷取defaultCenter//添加觀察者- (void)addObserver:(id)observer //接收通知的對象 selector:(SEL)methodToInvokeIfSomethingHappens name:(NSString *)name //廣播名 object:(id)sender; //監聽的廣播對象,nil指監聽所有對象//接收到通知後調用方法- (void)methodToInvokeIfSomethingHappens:(NSNotification *)notification{ notification.name // the name passed above notification.object //發送廣播的對象 notification.userInfo // notification-specific information about what happened(取決於寄件者)}//結束收聽通知,通常放於視圖從介面消失時[center removeObserver:self];[center removeObserver:self name:UIContentSizeCategoryDidChangeNotification object:nil];
通知執行個體(系統設定字型改變)
//視圖出現在螢幕上註冊通知- (void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(preferredFontChanged:) name:UIContentSizeCategoryDidChangeNotification object:nil];}//通知出現時調用方法- (void)preferredFontChanged:(NSNotification *)notification{ [self usePreferredFonts];}//重新設定字型- (void)usePreferredFonts{ self.body.font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody]; self.headLine.font = [UIFont preferredFontForTextStyle:UIFontTextStyleHeadline];}//視圖離開介面是移除通知- (void)viewWillDisappear:(BOOL)animated{ [super viewWillDisappear:animated]; [[NSNotificationCenter defaultCenter] removeObserver:self name:UIContentSizeCategoryDidChangeNotification object:nil];
4、作業
無
課程樣本Attributor源碼:https://github.com/NSLogMeng/Stanford_iOS7_Study/commit/242826c2220afe978bc1d060c2dff19578a835c9
課程視頻地址:網易公開課:http://open.163.com/movie/2014/1/L/H/M9H7S9F1H_M9H801GLH.html
或者iTunes U搜尋standford課程
(5/18)重學Standford_iOS7開發_視圖控制器生命週期_課程筆記