2015-05-04 14 views
6

Ich habe versucht, eine dynamische Zelle mit Etikett mit swift, um IOS7 in heightForRowAtIndexPath zu unterstützen, aber ich finde nur objective-c-Code, ist es möglich, jemand anderen zu helfen ich diesen Code in swift umschreiben?Dynamische benutzerdefinierte UITableViewCell Höhe basierend auf Etikett Textlänge in Swift

// Fetch yourText for this row from your data source.. 
    NSString *yourText = [yourArray objectAtIndex:indexPath.row]; 

    CGSize lableWidth = CGSizeMake(300, CGFLOAT_MAX); // 300 is fixed width of label. You can change this value 
    CGSize requiredSize = [yourText sizeWithFont:[UIFont fontWithName:@"Times New Roman" size:19] constrainedToSize:lableWidth lineBreakMode:NSLineBreakByWordWrapping]; // You can put your desire font 

    // Here, you will have to use this requiredSize and based on that, adjust height of your cell. I have added 10 on total required height of label and it will have 5 pixels of padding on top and bottom. You can change this too. 
    int calculatedHeight = requiredSize.height+10; 
    return (float)calculatedHeight; 

genau diese Aussage (wie man es zügig konvertieren):

CGSize requiredSize = [yourText sizeWithFont:[UIFont fontWithName:@"Times New Roman" size:19] constrainedToSize:lableWidth lineBreakMode:NSLineBreakByWordWrapping] 

Ich habe versucht, es zu konvertieren (aber es funktioniert nicht wie das ursprüngliche Ziel-c):

var requiredSize: CGSize = originalString.sizeWithAttributes([NSFontAttributeName: UIFont.systemFontOfSize(19.0)]) 
+0

ist hier beste Beispiel dafür gehen gerade durch it.http: //www.raywenderlich.com/87975/dynamic-table-view-cell-height-ios-8-swift –

+0

Folgen Sie diesem Link: http://stackoverflow.com/questions/9827126/change-the-guidableviewcell-height-according-to-amount-of-text/42111135#42111135> – mumu

Antwort

6

Vielleicht ist es möglich, es ohne die UILabel zu tun, aber das funktioniert gut.

let label = UILabel(frame: CGRect(x: 0, y: 0, width: 300, height: 10000)) 
label.text = someText 
label.numberOfLines = 100 
label.font = UIFont(name: "Times New Roman", size: 19.0) 
label.sizeToFit() 
return label.frame.height + 10 
Verwandte Themen