UITableview单元格标签字体大小未设置第一次

问题描述:

我在单元格中有一个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

尝试改变字体在willDisplay方法。

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

    } 
+0

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

+0

@GauravGupta您是否在此方法的'cell'输入参数上设置字体?如果使用'tableView.dequeueReusableCell(...)'得到单元格,它将不起作用。请参阅我的答案在这里:https://*.com/a/47040564/1245231 – petrsyn

在每个案例陈述后,您应该使用break;请尝试类似下面

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

    default: 
     break; 
}    
+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给你。

试试这个:

#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]]; 
} 

我米使用上面的代码,它的工作对我罚款。