Ios UITableview单元格标签字体大小未首次设置
我有一个Ios UITableview单元格标签字体大小未首次设置,ios,swift,uitableview,uifont,Ios,Swift,Uitableview,Uifont,我有一个UITableview在一个单元格中有许多标签。我想根据cellForRowAt方法中的设备设置字体大小,但字体大小不是第一次设置的。当我滚动tableview时,将返回屏幕,然后设置字体大小。如何解决这个问题。下面的代码在cellForRowAt中使用 cell.lbl_desc.adjustsFontSizeToFitWidth=true cell.lbl_price.adjustsFontSizeToFitWidth=true
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)
}
尝试更改willDisplay方法中的字体
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
}
在每个案例陈述之后,您应该使用中断请尝试下面的方法
switch (mode) {
case kEditGameModeEdit:
// ...
break;
case kEditGameModeNewGame:
// ...
break;
default:
break;
}
尝试使用此代码:
将以下行放入一个常量文件中
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)
现在在你的手机里写下这段代码
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)
}
}
希望这对你有用。试试这个:
#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]];
}
#定义屏幕宽度([[UIScreen mainScreen]界限].size.WIDTH)
#定义屏幕高度([[UIScreen mainScreen]界限].size.HEIGHT)
#定义屏幕最大长度(最大(屏幕宽度、屏幕高度))
#定义屏幕最小长度(最小(屏幕宽度、屏幕高度))
#定义是IPHONE 4还是更小(是IPHONE和屏幕最大长度<568.0)
#定义IS_IPHONE_5(IS_IPHONE&&SCREEN_MAX_LENGTH==568.0)
#定义IS_IPHONE_6(IS_IPHONE&&SCREEN_MAX_LENGTH==667.0)
#定义IS_IPHONE_6P(IS_IPHONE&&SCREEN_MAX_LENGTH==736.0)
如果(是IPHONE 5){
[cell.lblProductName setFont:[UIFont-systemFontOfSize:36];
}
否则如果(是IPHONE 6){
[cell.lblProductName setFont:[UIFont-systemFontOfSize:37];
}
否则{
[cell.lblProductName setFont:[UIFont-systemFontOfSize:38];
}
我正在使用上面的代码,它对我来说很好。也许你可以显示一些代码?我添加了代码。这就是你在代码库中使用的代码吗??你不用休息;在每个案例陈述之后。。。这可能是您可以重新检查代码的原因。switch([error code]){case-kPFErrorObjectNotFound://Handle-error.break;case-kPFErrorConnectionFailed://Handle-error.break;default://Handle-error.}
我正在使用它,但没有change@GauravGupta您是否在该方法的单元格
输入参数上设置字体?如果使用tableView.dequeueReusableCell(…)
获取单元格,它将无法工作。请参见我的回答: