2016-04-28 47 views
0

我已經試過一切都像的UILabel視圖不對齊到右

textLabel.textAlignment =NSTextAlignmentRight; 

但沒有什麼似乎工作波紋管是我的代碼。裏面cellforrawatindexpath

- (UITableViewCell *)tableView:(UITableView *)myTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    UITableViewCell *cell = (UITableViewCell *)[self.messageList dequeueReusableCellWithIdentifier:@"ChatListItem"]; 

    if (cell == nil) { 
      NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"OutGoing" owner:self options:nil]; 
     } 
     cell = (UITableViewCell *)[nib objectAtIndex:0]; 
    } 
    textLabel = (UILabel *)[cell viewWithTag:1]; 
      textLabel.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"chat_b2.png"]]; 
      textLabel =[[UILabel alloc] initWithFrame:CGRectMake(21,12, 241,30)]; 
    NSString * test=[[rssOutputData objectAtIndex:indexPath.row]xml_msg]; 
    textLabel.text = test; 
    textLabel.numberOfLines = 0; 
    [textLabel sizeToFit]; 
    [cell addSubview:textLabel]; 
    return cell; 
} 

這裏是截圖:App ScreenShot

在這裏你可以看到(紅色)文本左對齊我希望它右對齊,請幫我解決這個問題。

+0

你在哪裏放置該標籤。 ?而你正在使用Autolayout/AutoResizing ..? –

+0

使用AutoResizing –

+0

此方法已過時。它使用autoLayout的時間很長。請檢查[apple文檔](https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/AutolayoutPG/)。試試吧! – GoGreen

回答

1

隨着[textLabel sizeToFit]標籤框架的大小將使文本適合它,因此您的標籤寬度將與文本相同並且與右對齊將使文本保持原位。評論這一行,它會起作用。

編輯:

[textLabel sizeToFit]; 
[textLabel setFrame:CGRectMake(21,12,241,textLabel.frame.size.height)]; 
+0

trind它,但仍然無法正常工作。謝謝 –

+0

@VixHunk模擬器去下拉菜單調試並打開「彩色混合層」,這會給你線索在哪裏準確,它的大小是什麼 – gvuksic

+0

是的,你是對的,但當我評論[textLabel sizeToFit]它工作每完整的信息在一行中。 –

0

如果你正在使用sizetoFit那麼你的標籤大小將根據內容的大小減小。這不是一致的問題。刪除此行,然後添加textLabel.textAlignment =NSTextAlignmentRight;並檢查它應該工作,我認爲。

0

你爲textLabel幀總是設置爲以下框架:

textLabel =[[UILabel alloc] initWithFrame:CGRectMake(21,12, 241,30)]; 

所以只有標籤顯示在左側。嘗試根據發送/接收消息更改標籤幀。

希望它可以幫助..

0

你的標籤是約束,你已經給

textLabel =[[UILabel alloc] initWithFrame:CGRectMake(21,12, 241,30)]; 

所以它是右對齊到您所提供的幀的幀那是你的標籤對準那個特殊的寬度是30 ......你應該給標籤的寬度等於單元格的寬度,然後它將根據你的單元格對齊。