Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

UITextView Vertical Alignment

By: a guest on Apr 13th, 2012  |  syntax: Objective C  |  size: 1.25 KB  |  views: 502  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  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. }