从drawAtPoint绘制的屏幕中删除字符串

时间:2013-07-26 06:36:24

标签: objective-c macos cocoa

我只想问一下如何从屏幕上删除由NSString的-drawAtPoint方法绘制的字符串。

以下是一些细节:

绘制功能

  void  DrawString (NSString * str)
  {
      [str drawAtPoint: NSMakePoint(x,y) withAttributes: MyAttributes];
  }

清除功能

 void EraseString (NSString * str)
 {
       //Code to delete str from the screen.
 }

1 个答案:

答案 0 :(得分:1)

你不会“抹掉”你画的东西。您只是在下次调用drawRect:时不绘制它。例如,在视图中设置字符串的状态时,请调用setNeedsDisplay:例如:

-(void)setShowString:(BOOL)showString
{
    _showString = showString;
    [self setNeedsDisplay:YES];
}

然后在drawRect:方法中,检查状态:

if ([self showString])
    [str drawAtPoint: NSMakePoint(x,y) withAttributes: MyAttributes];

当您调用setShowString:

时,字符串将显示或隐藏字符串

或者,您可以设置字符串值(假设非ARC项目):

-(void)setStr:(NSString*)newString
{
    [newString retain];
    [_str release];
    _str = newString;

    [self setNeedsDisplay:YES];
}

并检查drawRect:中的字符串:

if ([[self str] length] > 0)
    [_str drawAtPoint: NSMakePoint(x,y) withAttributes: MyAttributes];