获取UILabel上最后一个字符串的位置。获取文字长度和高度,自动换行

 //行的高度。
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
NewNotificationCell *cell = (NewNotificationCell *)[self tableView:tableView cellForRowAtIndexPath:indexPath];
cell.myLabel.text = [self.dataArray objectAtIndex:indexPath.row];
labelSize = [cell.myLabel.text sizeWithFont:[UIFont fontWithName:@"Arial" size:] constrainedToSize:CGSizeMake(cell.myLabel.frame.size.width, ) lineBreakMode:UILineBreakModeWordWrap];
cell.myLabel.lineBreakMode = UILineBreakModeWordWrap;
cell.myLabel.numberOfLines = ;
[cell.myLabel setFont:[UIFont fontWithName:@"Arial" size:]];
cell.myLabel.frame = CGRectMake(, , labelSize.width, labelSize.height); return labelSize.height+;
}
     //自动换行。
cell.myLabel.text = [self.dataArray objectAtIndex:indexPath.row];
labelSize = [cell.myLabel.text sizeWithFont:[UIFont fontWithName:@"Arial" size:] constrainedToSize:CGSizeMake(cell.myLabel.frame.size.width, ) lineBreakMode:UILineBreakModeWordWrap];
cell.myLabel.lineBreakMode = UILineBreakModeWordWrap;
cell.myLabel.numberOfLines = ;
[cell.myLabel setFont:[UIFont fontWithName:@"Arial" size:]];
cell.myLabel.frame = CGRectMake(, , labelSize.width, labelSize.height); //获取文字长度和高度。
CGSize fontSize =[cell.myLabel.text sizeWithFont:cell.myLabel.font
forWidth:cell.myLabel.frame.size.width
lineBreakMode:UILineBreakModeWordWrap];
NSLog(@"文字长度=%f",fontSize.width); //获取UILabel上最后一个字符串的位置。
CGPoint lastPoint;
CGSize sz = [cell.myLabel.text sizeWithFont:cell.myLabel.font constrainedToSize:CGSizeMake(MAXFLOAT, )]; CGSize linesSz = [cell.myLabel.text sizeWithFont:cell.myLabel.font constrainedToSize:CGSizeMake(cell.myLabel.frame.size.width, MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap];
if(sz.width <= linesSz.width) //判断是否折行
{
lastPoint = CGPointMake(cell.myLabel.frame.origin.x + sz.width, cell.myLabel.frame.origin.y);
}
else
{
lastPoint = CGPointMake(cell.myLabel.frame.origin.x + (int)sz.width % (int)linesSz.width,linesSz.height - sz.height);
}
NSLog(@"====%f",lastPoint.x); [cell.myButton setTitle:@"查看" forState:UIControlStateNormal];
[cell.myButton setBackgroundImage:[UIImage imageNamed:@"button.png"] forState:UIControlStateNormal];
if ([cell.myButton.titleLabel.text isEqualToString:@"查看"]) {
cell.myButton.frame = CGRectMake(lastPoint.x+, labelSize.height-, , );
}
[cell.timeButton setTitle:@"今天" forState:UIControlStateNormal];
cell.timeButton.frame = CGRectMake(, labelSize.height+, , );
return cell;
}
上一篇:SqlServer获取两个日期时间差


下一篇:关于QT下资源使用和资源占用…