2017-06-02 67 views
1

我在單元格中有一個UITableview是很多標籤。我想根據cellForRowAt方法中的設備設置字體大小,但字體大小沒有設置第一次。當我滾動然後將實現代碼如下回屏幕,然後字體大小set.How解決這一issue.Below代碼使用cellForRowAtUITableview單元格標籤字體大小未設置第一次

  cell.lbl_desc.adjustsFontSizeToFitWidth=true 
      cell.lbl_price.adjustsFontSizeToFitWidth=true 
      cell.lbl_qty.adjustsFontSizeToFitWidth=true 

     let bounds = UIScreen.main.bounds 
     let height = bounds.size.height 
     switch height 
     { 
     case 568.0: 
      print("iPhone 5") 

      cell.lbl_desc.font = cell.lbl_desc.font.withSize(13) 
      cell.lbl_price.font = cell.lbl_price.font.withSize(13) 
      cell.lbl_qty.font = cell.lbl_qty.font.withSize(13) 
     case 667.0: 
      print("iPhone 6") 

      cell.lbl_desc.font = cell.lbl_desc.font.withSize(15) 
      cell.lbl_price.font = cell.lbl_price.font.withSize(15) 
      cell.lbl_qty.font = cell.lbl_qty.font.withSize(15) 
     case 736.0: 
      print("iPhone 6+") 

      cell.lbl_desc.font = cell.lbl_desc.font.withSize(16) 
      cell.lbl_price.font = cell.lbl_price.font.withSize(16) 
      cell.lbl_qty.font = cell.lbl_qty.font.withSize(16) 
     default: 
      print("iPad") 

      cell.lbl_desc.font = cell.lbl_desc.font.withSize(18) 
      cell.lbl_price.font = cell.lbl_price.font.withSize(18) 
      cell.lbl_qty.font = cell.lbl_qty.font.withSize(18) 
     } 
+0

也許你可以顯示你的一些代碼? – koropok

+0

我添加了代碼。 –

+1

是這個確切的代碼你在你的代碼庫中使用什麼?您不會在每個案例陳述後使用** break; **這可能是您可以重新檢查的原因 – gEeKyMiNd

回答

1

嘗試改變字體在willDisplay方法。

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) { 

    } 
+0

我正在使用它,但沒有更改 –

+0

@GauravGupta您是否在此方法的'cell'輸入參數上設置字體?如果使用'tableView.dequeueReusableCell(...)'得到單元格,它將不起作用。請參閱我的答案在這裏:https://stackoverflow.com/a/47040564/1245231 – petrsyn

1

在每個案例陳述後,您應該使用break;請嘗試類似下面

switch (mode) { 
    case kEditGameModeEdit: 
     // ... 
     break; 
    case kEditGameModeNewGame: 
     // ... 
     break; 

    default: 
     break; 
}    
+0

我增加了休息但不工作 –

0

使用此代碼嘗試:

在一個恆定的文件放在下面的線條,

let IS_IPHONE_5 = (IS_IPHONE && SCREEN_MAX_LENGTH == 568.0) 
let IS_IPHONE_6 = (IS_IPHONE && SCREEN_MAX_LENGTH == 667.0) 
let IS_IPHONE_6P = (IS_IPHONE && SCREEN_MAX_LENGTH == 736.0) 
let IS_IPAD = (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiom.pad) 

現在,在您cellForRowAt寫這樣的代碼,

if IS_IPAD { 
     cell.lbl_desc.font = cell.lbl_desc.font.withSize(18) 
     cell.lbl_price.font = cell.lbl_price.font.withSize(18) 
     cell.lbl_qty.font = cell.lbl_qty.font.withSize(18) 
    } 
    else { 
     if IS_IPHONE_5 { 
      cell.lbl_desc.font = cell.lbl_desc.font.withSize(13) 
      cell.lbl_price.font = cell.lbl_price.font.withSize(13) 
      cell.lbl_qty.font = cell.lbl_qty.font.withSize(13) 
     } 
     else if IS_IPHONE_6 { 
      cell.lbl_desc.font = cell.lbl_desc.font.withSize(15) 
      cell.lbl_price.font = cell.lbl_price.font.withSize(15) 
      cell.lbl_qty.font = cell.lbl_qty.font.withSize(15) 
     } 
     else if IS_IPHONE_6P { 
      cell.lbl_desc.font = cell.lbl_desc.font.withSize(16) 
      cell.lbl_price.font = cell.lbl_price.font.withSize(16) 
      cell.lbl_qty.font = cell.lbl_qty.font.withSize(16) 
     } 
    } 

希望這會窩窩rk給你。

0

試試這個:

#define SCREEN_WIDTH ([[UIScreen mainScreen] bounds].size.width) 
#define SCREEN_HEIGHT ([[UIScreen mainScreen] bounds].size.height) 
#define SCREEN_MAX_LENGTH (MAX(SCREEN_WIDTH, SCREEN_HEIGHT)) 
#define SCREEN_MIN_LENGTH (MIN(SCREEN_WIDTH, SCREEN_HEIGHT)) 

#define IS_IPHONE_4_OR_LESS (IS_IPHONE && SCREEN_MAX_LENGTH < 568.0) 
#define IS_IPHONE_5 (IS_IPHONE && SCREEN_MAX_LENGTH == 568.0) 
#define IS_IPHONE_6 (IS_IPHONE && SCREEN_MAX_LENGTH == 667.0) 
#define IS_IPHONE_6P (IS_IPHONE && SCREEN_MAX_LENGTH == 736.0) 

if (IS_IPHONE_5) { 
    [cell.lblProductName setFont:[UIFont systemFontOfSize:36]]; 
} 
else if (IS_IPHONE_6){ 
    [cell.lblProductName setFont:[UIFont systemFontOfSize:37]]; 
} 
else{ 
    [cell.lblProductName setFont:[UIFont systemFontOfSize:38]]; 
} 

我米使用上面的代碼,它的工作對我罰款。

相關問題