我想实现一个跨越多行的NSTokenField
。例如:
多线NSTokenField
我在互联网上找到了一些多行NSTokenField
的示例代码:
IBOutlet NSTokenField *tokenField;
- (void)awakeFromNib
{
[[tokenField cell] setWraps:YES];
}
使我的类成为NSTokenField
的委托并实现以下方法
- (void)controlTextDidChange:(NSNotification *)obj {
NSRect oldTokenFieldFrame = [tokenField frame];
NSRect tokenFieldBounds = [tokenField bounds];
float height = oldTokenFieldFrame.size.height;
tokenFieldBounds.size.height = CGFLOAT_MAX;
NSSize cellSize = [[tokenField cell] cellSizeForBounds:tokenFieldBounds];
float y = oldTokenFieldFrame.origin.y + height - cellSize.height;
[tokenField setFrame:NSMakeRect(oldTokenFieldFrame.origin.x,
y,
oldTokenFieldFrame.size.width,
cellSize.height)];
}
但是此代码无法正常工作。
你能帮我解决这个问题吗?
提前谢谢你。
对于每个只是寻找工作代码答案的人来说,我找到了这个超短的解决方案。来吧:
#import "MyExpandingTokenField.h"
@implementation MyExpandingTokenField
- (NSSize)intrinsicContentSize {
NSSize intrinsicContentSize = [self sizeThatFits:NSMakeSize(self.frame.size.width, CGFLOAT_MAX)];
intrinsicContentSize = NSMakeSize(intrinsicContentSize.width, intrinsicContentSize.height + 5);
return intrinsicContentSize;
}
- (void)textDidChange:(NSNotification *)notification {
[super textDidChange:notification];
[self invalidateIntrinsicContentSize];
}
@end