2017-04-18 144 views
0

我可以根據文本調整自定義標籤的寬度和高度UITablleViewCell。它看起來像下面。根據UITableViewCell調整背景圖像的大小標籤的高度和寬度

enter image description here

現在我想設置低於聊天氣泡圖像標籤作爲背景,使泡沫調整基於標籤文本的大小,並給出類似於其他使者的泡沫效應。

enter image description here

下面是我對圖像設置爲標籤的後臺代碼。

-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath { 

    return UITableViewAutomaticDimension; 
} 
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

     return UITableViewAutomaticDimension; 


} 

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



      static NSString *simpleTableIdentifier = @"ChatConversationTableViewCell"; 

      ChatConversationTableViewCell *cell = (ChatConversationTableViewCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier]; 
      if (cell == nil) 
      { 
       NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ChatConversationTableViewCell" owner:self options:nil]; 
       cell = [nib objectAtIndex:0]; 
      } 

      cell.selectionStyle = UITableViewCellSelectionStyleNone; 

      cell.chatmsgLabel.text = [chatHistoryArr objectAtIndex:indexPath.row]; 
      cell.timeAndDateMsgLabel.text = [timeAndDateMsgArr objectAtIndex:indexPath.row]; 

UIImage *img = [UIImage imageNamed:@"bubble2.png"]; 
CGSize imgSize = cell.timeAndDateMsgLabel.frame.size; 

UIGraphicsBeginImageContext(imgSize); 
[img drawInRect:CGRectMake(0,0,imgSize.width,imgSize.height)]; 
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

cell.timeAndDateMsgLabel.backgroundColor = [UIColor colorWithPatternImage:newImage]; 

      return cell; 


} 

輸出類似於

enter image description here

我的預期輸出是

enter image description here

欲瞭解更多信息下面是我的CustomTableViewCell和它的約束

enter image description here

enter image description here

我嘗試了很多實現了輸出,但我無法弄清楚。是否有任何其他方法來實現輸出,我準備好遵循任何適合我的要求的方法。任何幫助將非常感激。

+0

看到這個鏈接它可能會幫助你:http://stackoverflow.com/questions/31823130/how-to-let-a-uiimage-only-stretch-in-a-specific-area –

+0

你可以查看這個項目作爲參考:https://github.com/jessesquires/JSQMessagesViewController。可能對你有幫助。該控件以良好的方式拉伸圖像。 – Aashish1aug

+0

你需要設置相同的顏色背景來標籤,而不是圖像。並且給予該標籤更嚴格的等同限制。 – KKRocks

回答

0

你可以通過下面的方法解決問題;

  1. 創建一個UIView。
  2. 設置背景。
  3. 在這個UIView中添加你的標籤。

將所有約束條件設置爲零。

這可以解決你的問題

-1

做下面的步驟:

  1. 取一個UIView(說,MyView的),並給它的約束和明確的背景色。

    • 頂部尾部到超視圖。
    • 導致與關係<=和中等優先級的超級查看。
    • 寬度常數與關係>=和更高的優先級,然後領先。
    • 身高與關係>=
  2. 現在MyView的添加ImageView的帶有約束,

    • 領先,尾隨頂部,底部,上海華。
  3. 將text_mesage,time的標籤添加到myview中,並相應地給出約束條件。

現在運行您的項目並檢查。如果還有問題,請告訴我。

1

要解決此問題,您可以將所有imageView約束設置爲零。 然後你可以在.xassets文件中添加圖像然後使用切片圖像。這將在必要時拉伸圖像並縮小圖像。 Image

切片取決於您。

Image of my storyboard

Output

希望它會幫助你。

+0

有趣的我想實現上述你提到的用戶界面,但我沒有任何imageview在我的tableviewcell設置約束。我已經將imageview設置爲UITableViewCell中標籤的背景。任何建議你如何設置imageview? – Madhu

+0

查看答案我更新了。我給了故事板的圖像。我在單元格中使用了圖像視圖,在圖像視圖中使用了文本級別。 – mumu

+0

請注意,您可以一勞永逸地創建一個「切片」圖像(查看UIImage的文檔(帶有「Stretchable」的部分),這應該可以避免您隨時使用UIGraphicsBeginImageContext()來重新創建圖像 – Larme

相關問題