macosnstextfieldnsfont

How to change size and font of custom NSTextField?


I followed answer here:

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

I was able to use @Dave code and it works great.

The only thing is I cannot seem to find a way to change font or size of my label.

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

Nothing happens at all , it's like it is not being affected.

The closest I got was when I added some code to - (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
    ...

}

I tried to contact dave, but I cannot comment yet, please advise.


Solution

  • That's right you need to do the change in drawRect:(NSRect)dirtyRect.

    Example:

    - (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!
        }
    }