填充令牌后调整 NSTokenField 的大小
我使用 NSTokenField 作为用户输入标签的方式。一切工作正常,并且当用户添加或删除标签时,它与管理标签的 CoreData 挂钩。
我最近添加了逻辑,以便 NSTokenField 在用户添加标签时垂直调整大小,并使用 Andrew Bowman 的 IFVerticallyExpandingTextField。同样,这一切都运行良好。
问题是,当我最初必须用标签填充 NSTokenField 时,我需要调整它的大小。我通过调用来填充该字段:
[tagField setObjectValue: anArray];
其中 anArray 是代表标签或令牌的一系列对象。这又调用 NSTokenField 委托方法,
tokenField:displayStringForRepresentedObject:
该方法返回前一个数组中传递的对象的字符串表示形式。
在所有对 displayStringForRepresentedObject 的调用之后,我需要调整 NSTokenField 的大小。有谁对通知有任何想法或知道一切都已完成的方法吗?即使在每次调用 displayStringForRepresentedObject 之间调用调整大小的方法也可能有效。
提前致谢。
I am using an NSTokenField as a way for users to enter tags. Everything works fine and it hooks up with CoreData managing the tags both when the user adds or deletes a tag.
I recently added logic so that the NSTokenField would resize vertically as the user adds tags and they break to the next line using Andrew Bowman's IFVerticallyExpandingTextField. Again this all works fine.
The issue is that when I have to initially populate the NSTokenField with tags, I need it to resize. I populate the field by calling:
[tagField setObjectValue: anArray];
Where anArray is a series of objects that represent a tag or a Token. This in turn calls the NSTokenField delegate method
tokenField:displayStringForRepresentedObject:
Which returns the string representation for the object passed in the previous array.
I need to resize the NSTokenField after all of the calls to displayStringForRepresentedObject. Does anyone have any ideas of a notification or a way of finding out that it's all done? Even a way of calling the resize in between each call to displayStringForRepresentedObject would probably work.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以尝试类似于 -setNeedsDisplay: 和 -displayIfNeeded ...即 -setNeedsSizeToFit: 和 -sizeToFitIfNeeded。
您只需要一个“needsSizeToFit”BOOL 标志以及 -setNeedsSizeToFit: 和 -sizeToFitIfNeeded 方法。
设置令牌后,调用 -setNeedsSizeToFit:YES。它反过来会设置实例的needsSizeToFit标志,然后如果该标志为YES,它将调用[self PerformSelector:@selector(sizeToFitIfNeeded) withObject:nil afterDelay:0]。您的 -sizeToFitIfNeeded 方法将检查您的needsSizeToFit标志是否为YES,调用[self sizeToFit],然后将needsSizeToFit标志设置为NO。
更新
这是一个完整的类 (JLNAutoSizingTokenField),它执行基本操作如上所述自动调整大小。唯一的增强是在上述委托方法中调用它:
You might try something similar to -setNeedsDisplay: and -displayIfNeeded ... i.e., -setNeedsSizeToFit: and -sizeToFitIfNeeded.
You'll just need a "needsSizeToFit" BOOL flag and the -setNeedsSizeToFit: and -sizeToFitIfNeeded methods.
After you set your tokens, call -setNeedsSizeToFit:YES. It in turn will set the instance's needsSizeToFit flag, then if the flag is YES, it will call [self performSelector:@selector(sizeToFitIfNeeded) withObject:nil afterDelay:0]. Your -sizeToFitIfNeeded method will check if your needsSizeToFit flag is YES, call [self sizeToFit], then set the needsSizeToFit flag to NO.
Update
Here's a complete class (JLNAutoSizingTokenField) that does basic autosizing as described above. The only augmentation was to call this in the afore-mentioned delegate method: