[英]Cocoa NSTextField - binding and setting attributes
我可以在控制器類中更改NSString,然后將其反映在標簽中。
我可以在我的控制器類中更改NSFont,我看到它反映在標簽中。
但...
我一輩子都無法弄清楚如何打開和關閉下划線。
如果我調用此函數...
-(void)setUnderlineType:(NSNumber*)underline
{
NSMutableAttributedString* content = [[TheLabel attributedStringValue] mutableCopy];
[content addAttribute:NSUnderlineStyleAttributeName value:underline range:NSMakeRange(0, content.length)];
[TheLabel setAttributedStringValue:content];
}
...我得到一個下划線,但是然后綁定字體被忽略,我得到一些標准字體。 從那時起,更改我的控制器中的NSFont對NSTextField沒有可見的影響。
我嘗試在添加下划線之前從“內容”中刪除屬性...刪除字體屬性...但這也不起作用。
每當我調用此函數時,綁定到NSTextField的字體都會被“忽略”,並且我看到標准字體是標准大小。
任何指導將不勝感激。
您需要設置NSFontAttributeName
更新的字體NSAttributedString
。
NSFont *font = ...;
NSMutableAttributedString* content = [[theLabel attributedStringValue] mutableCopy];
[content addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, content.length)];
[theLabel setAttributedStringValue:content];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.