Domanda

Voglio ridimensionare l'altezza della cella in base all'altezza dell'etichetta e all'altezza dell'etichetta in base al testo. Oppure c'è un modo per ridimensionare l'altezza della cella in base al testo inserito in UITextView?

È stato utile?

Soluzione

QUESTO METODO È DEPRECATO DA iOS 7.0.

Esiste un UITableView metodo delegato chiamato heightForRowAtIndexPath che viene chiamato prima di creare una cella o una tabella.

È possibile utilizzare il NSIndexPath passato ad esso per ottenere il testo in una riga specifica e utilizzare il metodo sizeWithFont da UIStringDrawing.h per calcolare un CGSize per quella riga.

Ad esempio:

CGSize size = [text sizeWithFont:font
                   constrainedToSize:maximumLabelSize
                   lineBreakMode:UILineBreakModeWordWrap];

E infine restituiresti size.height.

Altri suggerimenti

--Per iOS7--

Basando questo sulla risposta di Josh Vera & # 8230; posizionalo in heightForRowAtIndexPath.

Ho i miei dati di tabella memorizzati in un NSDray * tableData.

// set the desired width of the textbox that will be holding the adjusted text, (for clarity only, set as property or ivar)
CGFloat widthOfMyTexbox = 250.0;

-(float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //Get a reference to your string to base the cell size on.
    NSString *cellText = [self.tableData objectAtIndex:indexPath.row];
    //set the desired size of your textbox
    CGSize constraint = CGSizeMake(widthOfMyTextBox, MAXFLOAT);
    //set your text attribute dictionary
    NSDictionary *attributes = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:14.0] forKey:NSFontAttributeName];
    //get the size of the text box
    CGRect textsize = [cellText boundingRectWithSize:constraint options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil];
    //calculate your size
    float textHeight = textsize.size.height +20;
    //I have mine set for a minimum size
    textHeight = (textHeight < 50.0) ? 50.0 : textHeight;

    return textHeight;
}

Non l'ho provato per iOS < 7, ma credo che dovrebbe funzionare anche per quello.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top