我已经想出如何使用NSBezierPath类在我的自定义视图类的drawRect函数中绘制形状,但我似乎无法弄清楚如何绘制文本.下面的代码是我到目前为止绘制文本(位于drawRect函数中):
NSText *text = [NSText new]; [text setTextColor: [NSColor yellowColor]]; [text setText: @"Hello!"];
我猜我可能需要提供一个NSRect或NSPoint来告诉NSText对象在哪里绘制自己,但我在Cocoa文档中找不到有关如何执行此操作的任何内容.
你可以尝试这些方面:
//note we are using the convenience method, so we don't need to autorelease the object NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:[NSFont fontWithName:@"Helvetica" size:26], NSFontAttributeName,[NSColor blackColor], NSForegroundColorAttributeName, nil]; NSAttributedString * currentText=[[NSAttributedString alloc] initWithString:@"Cat" attributes: attributes]; NSSize attrSize = [currentText size]; [currentText drawAtPoint:NSMakePoint(yourX, yourY)];