1 //行的高度。 2 -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ 3 NewNotificationCell *cell = (NewNotificationCell *)[self tableView:tableView cellForRowAtIndexPath:indexPath]; 4 cell.myLabel.text = [self.dataArray objectAtIndex:indexPath.row]; 5 labelSize = [cell.myLabel.text sizeWithFont:[UIFont fontWithName:@"Arial" size:15] constrainedToSize:CGSizeMake(cell.myLabel.frame.size.width, 1000) lineBreakMode:UILineBreakModeWordWrap]; 6 cell.myLabel.lineBreakMode = UILineBreakModeWordWrap; 7 cell.myLabel.numberOfLines = 0; 8 [cell.myLabel setFont:[UIFont fontWithName:@"Arial" size:15]]; 9 cell.myLabel.frame = CGRectMake(0, 0, labelSize.width, labelSize.height); 10 11 return labelSize.height+30; 12 }
1 //自動換行。 2 cell.myLabel.text = [self.dataArray objectAtIndex:indexPath.row]; 3 labelSize = [cell.myLabel.text sizeWithFont:[UIFont fontWithName:@"Arial" size:15] constrainedToSize:CGSizeMake(cell.myLabel.frame.size.width, 1000) lineBreakMode:UILineBreakModeWordWrap]; 4 cell.myLabel.lineBreakMode = UILineBreakModeWordWrap; 5 cell.myLabel.numberOfLines = 0; 6 [cell.myLabel setFont:[UIFont fontWithName:@"Arial" size:15]]; 7 cell.myLabel.frame = CGRectMake(0, 0, labelSize.width, labelSize.height); 8 9 //獲取文字長度和高度。 10 CGSize fontSize =[cell.myLabel.text sizeWithFont:cell.myLabel.font 11 forWidth:cell.myLabel.frame.size.width 12 lineBreakMode:UILineBreakModeWordWrap]; 13 NSLog(@"文字長度=%f",fontSize.width); 14 15 //獲取UILabel上最後一個字符串的位置。 16 CGPoint lastPoint; 17 CGSize sz = [cell.myLabel.text sizeWithFont:cell.myLabel.font constrainedToSize:CGSizeMake(MAXFLOAT, 40)]; 18 19 CGSize linesSz = [cell.myLabel.text sizeWithFont:cell.myLabel.font constrainedToSize:CGSizeMake(cell.myLabel.frame.size.width, MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap]; 20 if(sz.width <= linesSz.width) //判斷是否折行 21 { 22 lastPoint = CGPointMake(cell.myLabel.frame.origin.x + sz.width, cell.myLabel.frame.origin.y); 23 } 24 else 25 { 26 lastPoint = CGPointMake(cell.myLabel.frame.origin.x + (int)sz.width % (int)linesSz.width,linesSz.height - sz.height); 27 } 28 NSLog(@"====%f",lastPoint.x); 29 30 31 [cell.myButton setTitle:@"查看" forState:UIControlStateNormal]; 32 [cell.myButton setBackgroundImage:[UIImage imageNamed:@"button.png"] forState:UIControlStateNormal]; 33 if ([cell.myButton.titleLabel.text isEqualToString:@"查看"]) { 34 cell.myButton.frame = CGRectMake(lastPoint.x+5, labelSize.height-19, 30, 20); 35 } 36 [cell.timeButton setTitle:@"今天" forState:UIControlStateNormal]; 37 cell.timeButton.frame = CGRectMake(260, labelSize.height+8, 50, 20); 38 return cell; 39 }