IOS7笔记-5、视图控制器生命周期

1、字体属性的添加方法

 1 - (IBAction)changeBodySelectedColorMatchBackgroundOfButton:(UIButton *)sender {
 2     [self.body.textStorage addAttribute:NSForegroundColorAttributeName
 3                                   value:sender.backgroundColor
 4                                   range:self.body.selectedRange];
 5 }
 6 
 7 - (IBAction)outlineBodySelection {
 8     [self.body.textStorage addAttributes:@{NSStrokeWidthAttributeName: @-3,
 9                                            NSStrokeColorAttributeName: [UIColor blackColor]} range:self.body.selectedRange];
10 }
11 - (IBAction)unOutlineBodySelection {
12     [self.body.textStorage removeAttribute:NSStrokeWidthAttributeName
13                                      range:self.body.selectedRange];
14 }

2、按钮字体描边设置,一般在viewDidLoad中实现

 1 - (void)viewDidLoad
 2 {
 3     [super viewDidLoad];
 4     // Do any additional setup after loading the view, typically from a nib.
 5     NSMutableAttributedString *title =
 6     [[NSMutableAttributedString alloc] initWithString:self.outlineButton.currentTitle];
 7     [title setAttributes:@{NSStrokeWidthAttributeName: @3,
 8                            NSStrokeColorAttributeName: self.outlineButton.tintColor}
 9                    range:NSMakeRange(0, [title length])];
10     [self.outlineButton setAttributedTitle:title forState:UIControlStateNormal];
11 }

3、使用用户设置的字体

1 -(void)usePerferredFont
2 {
3     self.body.font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
4     self.headline.font = [UIFont preferredFontForTextStyle:UIFontTextStyleHeadline];
5 }

4、添加广播与去除广播一般分别在viewWillAppear和viewWillDisappear中设置

 1 -(void)viewWillAppear:(BOOL)animated
 2 {
 3     [super viewWillAppear:animated];
 4     [self usePerferredFont];
 5     [[NSNotificationCenter defaultCenter] addObserver:self
 6                                              selector:@selector(perferredFontChanged:)
 7                                                  name:UIContentSizeCategoryDidChangeNotification object:nil];
 8 }
 9 
10 -(void)viewWillDisappear:(BOOL)animated
11 {
12     [super viewWillDisappear:animated];
13     [[NSNotificationCenter defaultCenter] removeObserver:self
14                                                     name:UIContentSizeCategoryDidChangeNotification
15                                                   object:nil];
16 }
17 
18 -(void)perferredFontChanged:(NSNotification *)notification
19 {
20     [self usePerferredFont];
21 }
22 
23 -(void)usePerferredFont
24 {
25     self.body.font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
26     self.headline.font = [UIFont preferredFontForTextStyle:UIFontTextStyleHeadline];
27 }

5、viewDidLoad中设置初始化实例相关,viewWillAppear则表示与界面相关已经初始化完毕,若有需要耗时资源则亦放置在viewWillAppear中实现!

posted @ 2016-06-05 15:57  Faint@LastStep  阅读(155)  评论(0编辑  收藏  举报