SHARE
TWEET

UITextView Vertical Alignment

a guest Apr 13th, 2012 605 Never
  1. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  2. {
  3.     static NSString *CellIdentifier = @"QuestionCell";
  4.     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  5.     if (cell == nil) {
  6.         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
  7.     } else { // if there is already a subview, remove it
  8.         while ([[cell.contentView subviews] count] > 0) {
  9.             UIView *labelToClear = [[cell.contentView subviews] objectAtIndex:0];
  10.             [labelToClear removeFromSuperview];
  11.         }
  12.     }
  13.    
  14.     UIFont *myFont = [UIFont fontWithName:@"Arial" size:18.0];
  15.     UITextView *lblQuestionTitle = [[UITextView alloc] init];// initialise your textView here, including setting its contentOffset
  16.     lblQuestionTitle.text = [[questions objectAtIndex:indexPath.row] objectForKey:@"questionTitle"];
  17.  
  18.     CGSize textSize = [lblQuestionTitle.text sizeWithFont:myFont constrainedToSize:lblQuestionTitle.frame.size lineBreakMode:UILineBreakModeWordWrap];
  19.     lblQuestionTitle.contentOffset = CGPointMake(textSize.width, textSize.height);
  20.     [cell.contentView addSubview:lblQuestionTitle];
  21.    
  22.     return cell;
  23. }
RAW Paste Data
Top