2015-09-05 19 views
2

想法是做一些像WhatsApp那樣爲每個用戶左右對齊消息的東西。在TableView行內動態對齊UITextView

我想這樣的代碼:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell 
{ 
    var cell = messageTableView.dequeueReusableCellWithIdentifier("messageCell") as! MessageCell 

    // get the items in this section 
    let sectionItems = self.getSectionItems(indexPath.section) 

    // get the item for the row in this section 
    let currentMessage = sectionItems[indexPath.row] 

    var messageTextViewAlignment: NSLayoutFormatOptions 

    if (currentMessage.from == self.currentUserId) 
    { 
     messageTextViewAlignment = NSLayoutFormatOptions.AlignAllRight 
    } 
    else 
    { 
     messageTextViewAlignment = NSLayoutFormatOptions.AlignAllLeft 
    } 

    var newConstraint = NSLayoutConstraint.constraintsWithVisualFormat("H:[messageTextView]", options: NSLayoutFormatOptions.AlignAllCenterX, metrics: nil, views: ["messageTextView": cell.messageTextView]) 

    cell.messageTextView.addConstraints(newConstraint) 

    return cell 
} 
+0

IM想這太,但沒有工作: cell.messageTextView.autoresizingMask = UIViewAutoresizing.FlexibleLeftMargin cell.messageTextView.autoresizingMask = UIViewAutoresizing.FlexibleRightMargin –

+0

我沒有看到你將「messageTextViewAlignment」分配給單元格的位置? – anhtu

+0

在字段選項,我試過但沒有工作太... –

回答

0

我不知道你想達到什麼。對齊的UILabel在屏幕的正確一側或對齊想象要對齊UIlabel已經離開這個涼爽的空白處或從一泡像什麼的App內UILabel

文本。這裏有些東西你可以嘗試:

使用IB

由於您使用IB的自定義的UITableViewCell,似乎只包含消息,不能只是IBOutlets你從UILabel到領先答尾隨限制單元格contentView頁邊距。

它看起來像:

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *labelLeadingConstrain; 
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *labelTrailingConstrain; 

並在代碼

if (currentMessage.from == self.currentUserId) 
{ 
    labelLeadingConstrain = X1; 
    labelTrailingConstrain = X2; 
} 
else 
{ 
    labelLeadingConstrain = X2; 
    labelTrailingConstrain = X1; 
} 

[...] 

return cell 

}

編程

我用這種方式處理的限制不太熟悉,但嘗試

if (currentMessage.from == self.currentUserId) 
{ 
    [cell.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-X1-[constraintUILabel]-X2-|" options:0 metrics:nil views:@{@"constraintUILabel" :cell.messageTextView}]]; 
} 
else 
{ 
    [cell.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-X2-[constraintUILabel]-X1-|" options:0 metrics:nil views:@{@"constraintUILabel" :cell.messageTextView}]]; 
} 

[...] 

return cell 

}

使用AlignAllRight

此約束適用於2次至少。因此,有你的messageTextView並假設你有一個messageView包含它(可能是tableViewCell」內容查看本身)

[NSLayoutConstraint constraintsWithVisualFormat:@"H:[messageTextView][messageView]" options:NSLayoutFormatAlignAllRight metrics:nil views:@{@"messageTextView" :cell.messageTextView},@"messageView" :cell.messageView}] 

在您的代碼段messageTextViewAlignment變量從未使用過,所以我相信你應該使用上述剪斷(斯威夫特風格)與messageTextViewAlignment變種

+0

在我問這裏之前,我做了約束,它的工作原理,但我認爲不是一個goo解決方案 –

+0

好的,在這種情況下,我不明白你爲什麼將你的約束ont-y應用到'messageTextViewAlignment'。 'AlignAllRight'對齊了幾個視圖的右邊緣,所以我相信你在輸入中至少需要2個視圖。自從提出一個選項以後,我會編輯我的回覆 –

0

@「尼古拉斯·布勞恩」更換NSLayoutFormatAlignAllRight以前問這裏,我喜歡你在「使用IB」建議編寫該解決方案,什麼東西,它的工作原理,但我認爲是不是一個很好的解決方案:

func alignTextView(textViewAlignment: NSLayoutFormatOptions) 
{ 
    horizontalSpaceFromContentViewToTextView.constant = 0 

    let spaceX = initialTextViewWidth - finalTextViewWidth 

    if(spaceX > 0) 
    { 
     if (textViewAlignment == NSLayoutFormatOptions.AlignAllRight) 
     { 
      horizontalSpaceFromContentViewToTextView.constant = spaceX 
     } 
    } 
    //println("spcaceX: \(spaceX) --- \(messageTextView.text)") 
} 

我想要做的工作,NSLayoutFormatOptions.AlignAllRight和NSLayoutFormatOptions.AlignAllLeft