獲取UILabel上最后一個(gè)字符串的位置。獲取文字長(zhǎng)度和高度,自動(dòng)換行

//行的高度。

-(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:15] constrainedToSize:CGSizeMake(cell.myLabel.frame.size.width, 1000) lineBreakMode:UILineBreakModeWordWrap];

cell.myLabel.lineBreakMode = UILineBreakModeWordWrap;

cell.myLabel.numberOfLines = 0;

[cell.myLabel setFont:[UIFont fontWithName:@"Arial" size:15]];

cell.myLabel.frame = CGRectMake(0, 0, labelSize.width, labelSize.height);

return labelSize.height+30;

}

//顯示每行的內(nèi)容。

- (NewNotificationCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

static NSString *CellIdentifier = @"Cell";

NewNotificationCell*cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (!cell) {

cell=[[[NewNotificationCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]autorelease];

cell = [[[NSBundle mainBundle]loadNibNamed:@"NewNotificationCell" owner:self options:nil]objectAtIndex:0];

}

//自動(dòng)換行。

cell.myLabel.text = [self.dataArray objectAtIndex:indexPath.row];

labelSize = [cell.myLabel.text sizeWithFont:[UIFont fontWithName:@"Arial" size:15] constrainedToSize:CGSizeMake(cell.myLabel.frame.size.width, 1000) lineBreakMode:UILineBreakModeWordWrap];

cell.myLabel.lineBreakMode = UILineBreakModeWordWrap;

cell.myLabel.numberOfLines = 0;

[cell.myLabel setFont:[UIFont fontWithName:@"Arial" size:15]];

cell.myLabel.frame = CGRectMake(0, 0, labelSize.width, labelSize.height);

//獲取文字長(zhǎng)度和高度。

CGSize fontSize =[cell.myLabel.text sizeWithFont:cell.myLabel.font

forWidth:cell.myLabel.frame.size.width

lineBreakMode:UILineBreakModeWordWrap];

NSLog(@"文字長(zhǎng)度=%f",fontSize.width);

//獲取UILabel上最后一個(gè)字符串的位置。

CGPoint lastPoint;

CGSize sz = [cell.myLabel.text sizeWithFont:cell.myLabel.font constrainedToSize:CGSizeMake(MAXFLOAT, 40)];

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+5, labelSize.height-19, 30, 20);

}

[cell.timeButton setTitle:@"今天" forState:UIControlStateNormal];

cell.timeButton.frame = CGRectMake(260, labelSize.height+8, 50, 20);

return cell;

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容