2011-06-14 63 views
5

我有一個多行標籤。我希望標籤上的文本總是從左上角開始,與標籤的高度和行數無關。標籤多行文本對齊

現在我使用的是財產

[question1Label setContentMode: UIViewContentModeTopLeft]; 

但它不工作

感謝

回答

6

我注意到contentMode屬性的UILabel的不影響其文字的對準。使用textAlignment屬性。

label.textAlignment = UITextAlignmentLeft; 

編輯:這將對齊文本中心左。爲了顯示來自左上方的文本,您需要使用sizeWithFont:方法NSString找到文本的高度。 See this SO post知道如何去做。

作爲替代,您可以使用UITextFieldUIControl子類,它繼承UIControl的contentVerticalAlignment財產。

textField.contentVerticalAlignment = UIControlContentVerticalAlignmentTop; 

您可以使用此屬性對齊頂部的文本。您可以通過使用屬性userInteractionEnabled屬性禁用用戶編輯文本。

+1

我使用這個屬性,但它只對齊左側的文本,但我想從頂部開始我的文本。 – Rachit 2011-06-15 04:39:31

+0

@Rachit Taneja,看到我更新的答案 – EmptyStack 2011-06-15 05:06:48

0

接受的答案不再適用於我,因爲UITextAlignmentLeft已折舊。

下面的工作很棒!

// Allow multiline label centered 
[label setNumberOfLines:0]; 
[label setTextAlignment:NSTextAlignmentCenter];