【问题标题】:UITableview cell label font size not set first time第一次未设置 UITableview 单元格标签字体大小
【发布时间】:2017-11-03 09:23:23
【问题描述】:

我有一个UITableview 在一个单元格中有很多标签。我想在cellForRowAt方法中根据设备设置字体大小,但第一次没有设置字体大小。当我滚动 tableview 然后将返回屏幕然后设置字体大小。如何解决这个问题。下面的代码在cellForRowAt中使用

           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)
        }

【问题讨论】:

  • 也许你可以展示一些你的代码?
  • 我添加了代码。
  • 这是您在代码库中使用的确切代码吗?您不要在每个 case 语句之后使用 break; ...这可能是您可以重新检查的原因
  • 是的,这是确切的代码。
  • switch ([error code]) { case kPFErrorObjectNotFound: // Handle error. break; case kPFErrorConnectionFailed: // Handle error. break; default: // Handle error. }

标签: ios swift uitableview uifont


【解决方案1】:

尝试在 willDisplay 方法中更改字体。

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

    }

【讨论】:

  • 我正在使用它但没有改变
  • @GauravGupta 您是否在此方法的cell 输入参数上设置字体?如果您使用tableView.dequeueReusableCell(...) 获取单元格,它将不起作用。请在此处查看我的回答:stackoverflow.com/a/47040564/1245231
【解决方案2】:

在每个 case 语句之后,您应该使用 break; 请尝试类似下面的操作

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

    default:
        break;
}              

【讨论】:

    【解决方案3】:

    试试这个代码:

    将下面的行放在一个常量文件中,

    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)
            }
        }
    

    希望这对你有用。

    【讨论】:

      【解决方案4】:

      试试这个:

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

      我正在使用上面的代码,它对我来说很好。

      【讨论】:

        猜你喜欢
        • 2011-12-18
        • 1970-01-01
        • 2019-01-15
        • 1970-01-01
        • 1970-01-01
        • 2016-03-30
        • 2017-02-24
        • 2017-12-27
        • 2011-07-16
        相关资源
        最近更新 更多