[英]How to set a default title in UIButton subclass that works in Interface Builder (Xcode 9)
[英]Attributed Title attributes set to UIButton in Interface Builder are ignored in app
我有一個UIButton,我在Interface Builder中給出了一個屬性標題:它有一個特定的字體,字符串的一部分是不同的顏色。
當我加載我的應用程序時,顏色保持不變,但應用程序將字體恢復為系統默認值...
有人經歷過這個或知道修復的方法嗎? 試圖避免在代碼中設置屬性標題。
如果重要,我使用的字體是Open Sans,它在我在代碼中指定字體的其他標簽上正確顯示。
您是否嘗試在更改按鈕字體之前選擇文本? ?
我一周前也有同樣的問題。 我也在使用Open Sans。 但是每次我嘗試構建時,都會遇到一個無處可尋的編譯錯誤。
無論如何..我只是在按鈕上添加了一個標簽,並按照我想要的方式進行配置。
NSMutableParagraphStyle *style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
[style setAlignment:NSTextAlignmentCenter];
[style setLineBreakMode:NSLineBreakByWordWrapping];
NSDictionary *dict1 = @{NSFontAttributeName:[UIFont fontWithName:@"Varela Round" size:15.0f],
NSForegroundColorAttributeName:[UIColor whiteColor],
NSParagraphStyleAttributeName:style};
NSDictionary *dict2 = @{NSFontAttributeName:[UIFont fontWithName:@"Varela Round" size:10.0f],
NSForegroundColorAttributeName:[UIColor whiteColor],
NSParagraphStyleAttributeName:style};
NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] init];
[attString appendAttributedString:[[NSAttributedString alloc] initWithString:@"Expensed\n" attributes:dict1]];
[attString appendAttributedString:[[NSAttributedString alloc] initWithString:@"Tap to edit" attributes:dict2]];
[cell.expenseTripButton setAttributedTitle:attString forState:UIControlStateNormal];
[[cell.expenseTripButton titleLabel] setNumberOfLines:0];
[[cell.expenseTripButton titleLabel] setLineBreakMode:NSLineBreakByWordWrapping];
我遇到了同樣的問題,但是UIButton,我的UILable已經改為新的字符串,我確定它在Interface builder上有所改變。 但是構建時它仍然是舊字符串。 我在源代碼中打開了Interface builder。 搜索舊字符串,它仍然在那里。 剛刪除它來解決問題。 (我不知道為什么會這樣)。 也許你應該打開IB以確保。
希望它對你有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.