【问题标题】:How to change size and font of custom NSTextField?如何更改自定义 NSTextField 的大小和字体?
【发布时间】:2014-09-30 16:41:59
【问题描述】:

我在这里跟着回答:

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

我能够使用@Dave 代码并且效果很好。

唯一的问题是我似乎找不到更改标签字体或大小的方法。

[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
    ...

}

我试图联系戴夫,但我还不能发表评论,请告知。

【问题讨论】:

    标签: macos nstextfield nsfont


    【解决方案1】:

    没错,您需要在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!
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-03
      相关资源
      最近更新 更多