美文网首页
CS193P-2013 Lecture 5 TextView、生

CS193P-2013 Lecture 5 TextView、生

作者: _Patrik_ | 来源:发表于2016-05-03 19:44 被阅读47次

UITextView

  • Like UILable, but multi-line

  • 可以滚动、编辑、选中等

  • @property (nonatomic, readonly) NSTextStorage *textStorage

    是 NSMutableAttributeString 的一个子类,可以通过这个属性来设置字符串的属性

  • 属性 font :直接 set 会导致原有的字体、颜色等属性全部丢失,需要用 for 循环取得原来的所有属性逐个添加回去,再加上要添加的新属性

  • Demo

    - (IBAction)changeBodySelectionColorToMatchBackgroundOfBackgroundOfButton: (UIButton *)sender {
          [self.body.textStorage addAttribute:NSForegroundColorAttributeName
                                    value:sender.backgroundColor
                                        range:self.body.selectedRange]
    }
    
    - (IBAction)outlineBodySelection {
          //设置描边需要设置描边宽度和描边颜色
          [self.body.textStorage addAttributes: @{NSStrokeWidthAttributeName : @-3,
                                                  NSStrokeColorAttributeName : [UIColor blackColor]}
                                     range:self.body.selectedRange]  
    }
    
    - (IBAction)unoutlineBodySelection {
          [self.body.textStorage removeAttribute: NSStrokeWidthAttributeName
                                       range: self.body.selectedRange]
    }
    

视图控制器生命周期

  • viewDidLoad
    • 视图被加载后调用
    • 在整个视图生命周期中只会被调用一次
    • 适合放置初始化代码
    • 不允许执行视图形状相关的代码,因为此时视图的边界还没有被确定
  • viewWillAppear
    • 视图将要出现在屏幕上之前被调用
    • 不要添加一次性初始化内容(需要放到 viewDidLoad 里),因为 viewWillAppear 会被多次调用
    • 如果要执行的某些初始化基于某些在其他模型里的会改变的数据,就把这些初始化放在这里
    • 可以执行一些几何的初始化
  • viewWillDisappear
    • 可以停止一些动画
  • viewWillLayoutSubviews/viewDidLayoutSubviews
    • 布局视图之前/后被调用。比如旋转屏幕之前/后
    • 添加几何相关的代码(尤其在 did)
  • awakeFromNib
    • 包括控制器在内的所有元素,当他们被从 storyboard 中唤醒时,这个方法会被调用(发生在 outlet 被设置之前,即发生在 MVC 加载之前)
    • 可以放置某些无法放在 viewDidLoad 里的初始化代码
  • initWithNibName
    • UIViewController 的指定初始化方法

UIButton

  • 没有像 UITextView 那样的 textStorage 属性,如果要修改 button 中文字的属性,必须拷贝一个可变的版本,改变它的属性再设置回去

    NSMutableAttribuetedString *title = [[NSMutableAttributedString alloc] initWithString:self.button.currentTitle];
    [title setAttributes: @{NSStrokeWidthAttributeName : @3,
                           NSStrokeColorAttributeName : self.button.tintColor}
                  range: NSMakeRange(0, [title length])];
    //这里要用 NSMakeRange 来创建一个 NSRange(一个C 结构体)表示范围
    [self.button setAttributedTitle: title forState: UIControlStateNormal];
    

广播站机制

  • MVC 的广播站机制,在 iOS7 中被称为通知

  • [NSNotificationCenter defaultlCenter] 返回一个类似于 NSUserDefault 的全局共享实例,使用这个对象来收听广播。方法是:

    - (void)addOvserver: (id)observer
              //想要收听广播的对象,在 controller 中一般是 self
             selector: (SEL)methodToInvokeIfSomethingHappens
            //observer 中的方法,当广播上出现内容时会调用它
                   name: (NSString *)name
            //广播站的名称
                 object: (id)sender
            //是否指向收听某个特定对象的广播(一般是 nil,表示收听频率上的任何广播)
                   
    - (void)methodToInvokeIfSomethingHappens: (NSNotification *)notification {
      //该方法一定有一个如上的参数
          notification.name   //上面的 name
        notification.object   //上面的 sender
        notification.userInfo //取决于广播站
    }
    
    //当停止收听时,要记得 tune out
    [center removeObserver: self]
    //停止收听所有广播
    [center removeObserver: self name:UIContentSizeCategoryDidChangeNoticifation object:nil] 
    //停止收听某个特定 sender 发送的广播
    
    • 一定要 tune out 的理由:

      通知中心始终用一个指针指向观察者,称为 Unsafe Retained 不安全保留类型。意思是,如果你还未调用 removeObserver 方法就离开了堆,通知中心可能会给你发送消息,导致应用崩溃。

    • tune out 的时机:

      • MVC 要离开当前视图时(比如 viewWillDisappear)

      • dealloc 方法(所有的对象都有这样一个方法,当对象要被销毁的最终阶段会被调用),如果处于某些原因无法在 MVC 要离开视图时调用 removeObserver 方法,那就在这里调用

        - (void) {
          [[NSNotificationCenter defaultCenter] removeObserver:self]  
        }
        
    • Demo

      //写在 viewWillAppear 里
      [self usePreferredFonts];
      //如果 MVC 不是当前视图,又改变了字体大小,要在这里同步一下
      NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
      [center addObserver : self
                  selector: @selector(preferredFontSizeChanged:)
                     name : UIContentSizeCategoryDidChangeNotification
                  object : nil];
      
      - (void)preferredFontSizeChanged: (NSNotification *)notification {
          [self usePreferredFonts];
      }
      
      - (void)usePreferredFonts {
          self.body.font = [UIFont preferredFontForTextStyle: UIFontTextStyleBody];
          self.headline.font = [UIFont preferredTextStyleHeadline]
      }
      
      //记得在 viewWillDisappear 里 removeObserver
      

相关文章

网友评论

      本文标题:CS193P-2013 Lecture 5 TextView、生

      本文链接:https://www.haomeiwen.com/subject/eyhrrttx.html