2017-06-02 2 views
1

Ich habe eine UITableview in einer Zelle sind viele Etiketten.Ich möchte Schriftgröße nach dem Gerät in CellForRowAt Methode festlegen, aber die Schriftgröße ist nicht das erste Mal festgelegt.Wenn ich blättern Tableview wird dann sind dann die Schriftgröße zu screenen zurück set.How dieses issue.Below Code lösen verwendet in cellForRowAtUITableview Zelle Etikett Schriftgröße nicht erstmals

  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

vielleicht können Sie einige Ihrer Codes zeigen? – koropok

+0

Ich habe den Code hinzugefügt. –

+1

ist dieser genaue Code, was verwenden Sie in Ihrer Codebasis? Sie verwenden nicht ** Pause; ** nach jeder Fall-Anweisung ... es kann ein Grund sein, dass Sie erneut überprüfen können – gEeKyMiNd

Antwort

1

Try Schriftart in willDisplay Methode zu ändern.

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

    } 
+0

Ich benutze es aber nicht ändern –

+0

@GauravGupta Setzen Sie die Schriftart auf den 'cell' Eingangsparameter dieser Methode? Wenn Sie die Zelle mit der 'tableView.dequeueReusableCell (...)' bekommen, wird es nicht funktionieren. Siehe auch meine Antwort hier: https://stackoverflow.com/a/47040564/1245231 – petrsyn

1

Nach jeder case-Anweisung sollten Sie eine Pause verwenden; bitte versuchen Sie es so etwas wie unten

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

    default: 
     break; 
}    
+0

Ich habe Pause hinzugefügt, aber nicht funktionieren –

0

mit diesem Code Versuchen:

unten Linien Legen Sie in einer Constant-Datei,

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) 

Jetzt in Ihrem cellForRowAt diesen Code schreiben,

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

Wünschen Sie dies wo Rk für dich.

0

Versuchen Sie folgendes:

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

ich über Code verwenden und es funktioniert gut für mich.