簡體   English   中英

setAttributedText不能在UIButton上運行?

[英]setAttributedText is not working on UIButton?

我試圖在UIButton上設置屬性字符串標題,但它似乎不起作用。

[self.emailSignInButton.titleLabel setAttributedText:[[NSAttributedString alloc] initWithString:NSLocalizedString(@"LOGIN_WITH_EMAIL", nil) attributes:@{ NSUnderlineStyleAttributeName : @(NSUnderlineStyleSingle)}]];

雖然這完全正常。

[self.emailSignInButton.titleLabel setText:NSLocalizedString(@"LOGIN_WITH_EMAIL", nil)];

奇怪的是另一個按鈕文本與屬性文本很好。 例如。

[self.emailSignupButton.titleLabel setAttributedText:[[NSAttributedString alloc] initWithString:NSLocalizedString(@"CREATE_ACCOUNT_WITH_EMAIL", nil) attributes:@{ NSUnderlineStyleAttributeName : @(NSUnderlineStyleSingle)}]]`;

知道這里發生了什么嗎?

您需要設置按鈕狀態的文本。 UIButton可以有幾種不同的狀態。

  • 正常
  • 突出
  • 重點
  • 應用
  • 保留的

您很可能需要Normal ,這將為所有情況設置默認標題。

的UIButton

  • setAttributedTitle:forState:

設置要用於指定狀態的樣式標題。

以下是您的代碼示例

NSAttributedString *string = [[NSAttributedString alloc] initWithString:NSLocalizedString(@"LOGIN_WITH_EMAIL", nil) 
                                                            attributes:@{ NSUnderlineStyleAttributeName : @(NSUnderlineStyleSingle)}]

[self.emailSignInButton setAttributedTitle:string forState:UIControlStateNormal];

引用:

UIControlState

UIButton - setAttributedTitle:forState:

暫無
暫無

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

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