第五課:
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課程
|
新聞熱點
疑難解答