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

时间:2017-06-02 04:38:32

标签: ios swift uitableview uifont

我在单元格中有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)
        }

4 个答案:

答案 0 :(得分:1)

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

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

    }

答案 1 :(得分:1)

在每个案例陈述之后,你应该使用休息; 请尝试类似下面的内容

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

    default:
        break;
}              

答案 2 :(得分: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)

现在在你的cellForRow中编写这段代码,

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

希望这对你有用。

答案 3 :(得分: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]];
}

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