CS193P-2013 Lecture 5 TextView、生命周期、廣播站機制

UITextView

  • Like UILable, but multi-line

  • 可以滾動、編輯、選中等

  • @property (nonatomic, readonly) NSTextStorage *textStorage

    是 NSMutableAttributeString 的一個子類,可以通過這個屬性來設(shè)置字符串的屬性

  • 屬性 font :直接 set 會導致原有的字體、顏色等屬性全部丟失,需要用 for 循環(huán)取得原來的所有屬性逐個添加回去,再加上要添加的新屬性

  • Demo

    - (IBAction)changeBodySelectionColorToMatchBackgroundOfBackgroundOfButton: (UIButton *)sender {
          [self.body.textStorage addAttribute:NSForegroundColorAttributeName
                                    value:sender.backgroundColor
                                        range:self.body.selectedRange]
    }
    
    - (IBAction)outlineBodySelection {
          //設(shè)置描邊需要設(shè)置描邊寬度和描邊顏色
          [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
    • 視圖被加載后調(diào)用
    • 在整個視圖生命周期中只會被調(diào)用一次
    • 適合放置初始化代碼
    • 不允許執(zhí)行視圖形狀相關(guān)的代碼,因為此時視圖的邊界還沒有被確定
  • viewWillAppear
    • 視圖將要出現(xiàn)在屏幕上之前被調(diào)用
    • 不要添加一次性初始化內(nèi)容(需要放到 viewDidLoad 里),因為 viewWillAppear 會被多次調(diào)用
    • 如果要執(zhí)行的某些初始化基于某些在其他模型里的會改變的數(shù)據(jù),就把這些初始化放在這里
    • 可以執(zhí)行一些幾何的初始化
  • viewWillDisappear
    • 可以停止一些動畫
  • viewWillLayoutSubviews/viewDidLayoutSubviews
    • 布局視圖之前/后被調(diào)用。比如旋轉(zhuǎn)屏幕之前/后
    • 添加幾何相關(guān)的代碼(尤其在 did)
  • awakeFromNib
    • 包括控制器在內(nèi)的所有元素,當他們被從 storyboard 中喚醒時,這個方法會被調(diào)用(發(fā)生在 outlet 被設(shè)置之前,即發(fā)生在 MVC 加載之前)
    • 可以放置某些無法放在 viewDidLoad 里的初始化代碼
  • initWithNibName
    • UIViewController 的指定初始化方法

UIButton

  • 沒有像 UITextView 那樣的 textStorage 屬性,如果要修改 button 中文字的屬性,必須拷貝一個可變的版本,改變它的屬性再設(shè)置回去

    NSMutableAttribuetedString *title = [[NSMutableAttributedString alloc] initWithString:self.button.currentTitle];
    [title setAttributes: @{NSStrokeWidthAttributeName : @3,
                           NSStrokeColorAttributeName : self.button.tintColor}
                  range: NSMakeRange(0, [title length])];
    //這里要用 NSMakeRange 來創(chuàng)建一個 NSRange(一個C 結(jié)構(gòu)體)表示范圍
    [self.button setAttributedTitle: title forState: UIControlStateNormal];
    

廣播站機制

  • MVC 的廣播站機制,在 iOS7 中被稱為通知

  • [NSNotificationCenter defaultlCenter] 返回一個類似于 NSUserDefault 的全局共享實例,使用這個對象來收聽廣播。方法是:

    - (void)addOvserver: (id)observer
              //想要收聽廣播的對象,在 controller 中一般是 self
             selector: (SEL)methodToInvokeIfSomethingHappens
            //observer 中的方法,當廣播上出現(xiàn)內(nèi)容時會調(diào)用它
                   name: (NSString *)name
            //廣播站的名稱
                 object: (id)sender
            //是否指向收聽某個特定對象的廣播(一般是 nil,表示收聽頻率上的任何廣播)
                   
    - (void)methodToInvokeIfSomethingHappens: (NSNotification *)notification {
      //該方法一定有一個如上的參數(shù)
          notification.name   //上面的 name
        notification.object   //上面的 sender
        notification.userInfo //取決于廣播站
    }
    
    //當停止收聽時,要記得 tune out
    [center removeObserver: self]
    //停止收聽所有廣播
    [center removeObserver: self name:UIContentSizeCategoryDidChangeNoticifation object:nil] 
    //停止收聽某個特定 sender 發(fā)送的廣播
    
    • 一定要 tune out 的理由:

      通知中心始終用一個指針指向觀察者,稱為 Unsafe Retained 不安全保留類型。意思是,如果你還未調(diào)用 removeObserver 方法就離開了堆,通知中心可能會給你發(fā)送消息,導致應(yīng)用崩潰。

    • tune out 的時機:

      • MVC 要離開當前視圖時(比如 viewWillDisappear)

      • dealloc 方法(所有的對象都有這樣一個方法,當對象要被銷毀的最終階段會被調(diào)用),如果處于某些原因無法在 MVC 要離開視圖時調(diào)用 removeObserver 方法,那就在這里調(diào)用

        - (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
      
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容