項目練手:簡易微博布局

項目地址:https://github.com/FFFFFFMILK/MessageFall

項目重點:

  1. 自定義可變高度的 cell
  2. 使用 frameModel,減少高度計算的次數(shù),在獲取數(shù)據(jù)的同時計算高度:直接寫在 tableview 的高度獲取代理方法里每一次 cell 出現(xiàn)都會調(diào)用一點,重復(fù)計算。

代碼知識點:

  1. 自定義 cell 的控件內(nèi)容可以分為三步搭建完成:

    • 在獲取數(shù)據(jù)之前,對所有不可變的內(nèi)容進行布局,對可變的內(nèi)容僅僅做實例化操作
    • 獲取數(shù)據(jù)后,對內(nèi)容進行賦值
    • 從 frameModel 中取出每個控件的 frame 值
  2. 也就是說,所有關(guān)于高度的計算在 frameModel 中進行,數(shù)據(jù)獲取之后,轉(zhuǎn)化為 frameModel 進行存儲。在 frameModel 中存儲數(shù)據(jù) Model。

  3. 根據(jù)文本內(nèi)容設(shè)置文本框的大?。?/p>

     // 名字
     CGSize nameLabelMaxSize = CGSizeMake(MAXFLOAT, MAXFLOAT);
    
     CGSize nameLabelRealSize = [messageModel.name boundingRectWithSize:nameLabelMaxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17]} context:nil].size;
    
     CGFloat nameLabelX = CGRectGetMaxX(_userImageViewFrame) + margin;
     CGFloat nameLabelY = (userImageWidth - nameLabelRealSize.height) /2 + margin;
    
     _nameLabelFrame = CGRectMake(nameLabelX, nameLabelY, nameLabelRealSize.width, nameLabelRealSize.height);
    
    • MaxSize:MAXFLOAT 代表的是浮點數(shù)的最大值,如此設(shè)置代表文本的寬度和高度都是可變的,文本框根據(jù)內(nèi)容而變。

    • NSStringDrawingUsesLineFragmentOrigin:

      查看源碼:

      typedef NS_OPTIONS(NSInteger, NSStringDrawingOptions) {
         NSStringDrawingUsesLineFragmentOrigin = 1 << 0, // The specified origin is the line fragment origin, not the base line origin
         NSStringDrawingUsesFontLeading = 1 << 1, // Uses the font leading for calculating line heights
         NSStringDrawingUsesDeviceMetrics = 1 << 3, // Uses image glyph bounds instead of typographic bounds
         NSStringDrawingTruncatesLastVisibleLine NS_ENUM_AVAILABLE(10_5, 6_0) = 1 << 5, // Truncates and adds the ellipsis character to the last visible line if the text doesn't fit into the bounds specified. Ignored if NSStringDrawingUsesLineFragmentOrigin is not also set.
      } NS_ENUM_AVAILABLE(10_0, 6_0);
      
      // NOTE: All of the following methods will default to drawing on a baseline, limiting drawing to a single line.
      // To correctly draw and size multi-line text, pass NSStringDrawingUsesLineFragmentOrigin in the options parameter.
      @interface NSString (NSExtendedStringDrawing)
      - (void)drawWithRect:(CGRect)rect options:(NSStringDrawingOptions)options attributes:(nullable NSDictionary<NSString *, id> *)attributes context:(nullable NSStringDrawingContext *)context NS_AVAILABLE(10_11, 7_0);
      - (CGRect)boundingRectWithSize:(CGSize)size options:(NSStringDrawingOptions)options attributes:(nullable NSDictionary<NSString *, id> *)attributes context:(nullable NSStringDrawingContext *)context NS_AVAILABLE(10_11, 7_0);
      @end
      

    要理解 UsesLineFragmentOrigin,先看看上面兩個方法之前的注釋:

    注意:所有方法默認繪制一條基準線,限制行數(shù)為一行;如果要正確的繪制多行文本,先使用 NSStringDrawingUsesLineFragmentOrigin 參數(shù)。

    從這里就可以看懂,這個參數(shù)注釋所說的 not the base line origin 是哪個線哪個點了。

    也就是說,想要根據(jù)內(nèi)容計算多行文本的 size,必須要帶的就是這個參數(shù)。

  4. CGRectGetMaxX:Return the rightmost x-value of `rect':返回rect 的 x 坐標值,在用來在這種一個控件的 frame 計算另外一個 frame 的情況下比較好用,同樣還有一個 CGRectGetMaxY。

最后編輯于
?著作權(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)容