2014-09-30 57 views
1

我跟着回答這裏:如何更改自定義NSTextField的大小和字體?

https://stackoverflow.com/a/3233802/3850487

我能夠使用@戴夫代碼,它的偉大工程。

唯一的是我似乎無法找到一種方法來更改我的標籤的字體或大小。

[self.rssLabel setText:fullString]; 
[self.rssLabel setSpeed:0.03f]; 
[[self rssLabel] setFont:[NSFont boldSystemFontOfSize:100]];//NOT WORKING 

什麼都沒有發生,就好像它沒有受到影響。

我得到的最接近是當我加入一些代碼來- (void)drawRect:(NSRect)dirtyRect

- (void)drawRect:(NSRect)dirtyRect { 
    // Drawing code here. 
    [[NSColor grayColor] set];//changed the background of the view 
    NSRectFill(dirtyRect); //not text color 
    ... 

} 

我試圖聯繫戴夫,但我不能評論呢,請指教。

回答

0

這是正確的,您需要在drawRect:(NSRect)dirtyRect中進行更改。

實施例:

- (void)drawRect:(NSRect)dirtyRect { 
    // Drawing code here. 
    NSFont *font = [NSFont fontWithName:@"Courier" size: 15.0f]; 
    //add more custom stuff, then assign attributes 
    NSDictionary *attributes = @{ NSFontAttributeName: font}; 

    if (point.x + stringWidth < 0) { 
     point.x += dirtyRect.size.width; 
    } 

    [text drawAtPoint:point withAttributes:attributes];//assign! 

    if (point.x < 0) { 
     NSPoint otherPoint = point; 
     otherPoint.x += dirtyRect.size.width; 
     [text drawAtPoint:otherPoint withAttributes:attributes];//assign! 
    } 
} 
相關問題