【问题标题】:How to update a text within NSView?如何更新 NSView 中的文本?
【发布时间】:2015-03-07 16:08:09
【问题描述】:

我想更新通过drawInRect: withAttributes: 添加到NSView 中的文本。但是,似乎无法仅通过更改其属性来更新文本。

class MyView: NSView {
    var str: NSString!

    override func drawRect(dirtyRect: NSRect) {
        super.drawRect(dirtyRect)

        str = "Sample Text"
        str.drawInRect(NSMakeRect(0, 0, 60, 40), withAttributes: nil)
    }
}

上面的代码为视图创建了文本Sample Text,我将它添加到了故事板上。

但是,当我尝试通过更改其属性或再次调用drawInRect: withAttributes: 来更新文本时,文本根本没有更新。代码如下:

@IBAction func callButton(sender: AnyObject) {
        myView.str = "Test"
        myView.str.drawInRect(CGRectMake(0, 0, 60, 40), withAttributes: nil)
} 

为什么文本没有更新?另外,如何更新文本?

【问题讨论】:

    标签: cocoa swift nsview


    【解决方案1】:

    不要在drawRect 中覆盖str。相反,在初始化时为属性设置一个默认字符串,并在更新str 属性时使用setNeedsDisplay 将视图标记为脏。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-10-28
      • 1970-01-01
      • 2022-12-28
      • 1970-01-01
      • 2016-05-09
      • 2015-02-14
      • 2017-07-23
      相关资源
      最近更新 更多