你不会“擦除”你画的东西。下次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];