簡體   English   中英

UIButton標題對齊和多行支持

[英]UIButton title alignment and multiline support

如何將UIButton的標題設置為左對齊,以及如何在UIButton顯示多行文本?

這也是在代碼中進行UIButton對齊的代碼:-

[myButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];

要設置UIButton的對齊UIButton ,請在Interface Builder中的“屬性”檢查器中查找按鈕。 有一個稱為對齊的部分。 將其設置為左側。 或正確,或任何您想要的。

在此處輸入圖片說明

為此,請使用UIControlcontentHorizontalAlignment屬性。 您可以將其設置為以下任何屬性:

UIControlContentHorizontalAlignmentCenter
UIControlContentHorizontalAlignmentLeft
UIControlContentHorizontalAlignmentRight
UIControlContentHorizontalAlignmentFill

[myButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];

這些選項都沒有一個看起來特別好(如您在上面看到的),使用UIButtoncontentEdgeInsets屬性來重新定位內容可能會帶來更多的運氣。

UIButton設置多行標題請查看此論壇帖子 ,其中描述了如何更改按鈕標簽。

或者,您可以使用此代碼快速創建2行按鈕:

myButton.titleLabel.textAlignment = UITextAlignmentCenter;
myButton.titleLabel.lineBreakMode = UILineBreakModeCharacterWrap;
[myButton setTitle:@"Siegfried\nRoy" forState:UIControlStateNormal];

在此處輸入圖片說明

Raju,別忘了將問題標記為已接受。

實際上,您可以將lineBreakMode設置為UILineBreakModeWordWrapUILineBreakModeCharacterWrap 如有必要,這會將文本分成多行。

嘗試這個

    myButton.titleLabel.textAlignment = UITextAlignmentLeft;
    myButton.titleLabel.lineBreakMode = UILineBreakModeCharacterWrap;
    myButton.titleLabel.numberOfLines = 0;

要添加段落效果,您可以更改標題的大小。

對於阿琳娜效應

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM