2014-09-30 58 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! 
    } 
} 
相关问题