在 NSCollectionView 中,具有自动调整文本大小的 NSTextField 看起来很糟糕
我这里遇到一个小问题。我有一个 NSCollectionView ,其中包含一个视图集合,其中有一个 NSTextField 显示这些字体中的字体名称。这是一个屏幕截图:
如您所见,某些字体看起来有问题,它们后面有 Interface Builder 字体大小。我使用此代码在 NSTextField 的 initWithFrame:
中设置字体大小:
float targetWidth = rect.size.width - 10;
float targetHeight = rect.size.height - 10;
int i;
for (i = 10; i < 100; i++) {
NSDictionary *attrs = [[NSDictionary alloc] initWithObjectsAndKeys:[NSFont fontWithName:[self.font fontName] size:i], NSFontAttributeName, nil];
NSSize strSize = [[self stringValue] sizeWithAttributes:attrs];
[attrs release];
if (strSize.width > targetWidth || strSize.height > targetHeight) {
break;
}
}
[self setFont:[NSFont fontWithName:[self.font fontName] size:(i - 1)]];
如何解决此问题?看起来很糟糕。提前致谢。
I am having a little problem here. I have an NSCollectionView with a collection of views which have an NSTextField that shows fontnames in those fonts. Here is a screenshot:
As you can see, some fonts look glitched, they have their Interface Builder font-size behind them. I use this code to set the font size in initWithFrame:
of the NSTextField:
float targetWidth = rect.size.width - 10;
float targetHeight = rect.size.height - 10;
int i;
for (i = 10; i < 100; i++) {
NSDictionary *attrs = [[NSDictionary alloc] initWithObjectsAndKeys:[NSFont fontWithName:[self.font fontName] size:i], NSFontAttributeName, nil];
NSSize strSize = [[self stringValue] sizeWithAttributes:attrs];
[attrs release];
if (strSize.width > targetWidth || strSize.height > targetHeight) {
break;
}
}
[self setFont:[NSFont fontWithName:[self.font fontName] size:(i - 1)]];
How can I fix this? It's looking terrible. Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这看起来更像是集合视图如何动画化的问题。您是否为原型集合项目视图启用了wantsLayers?尝试将其关闭。还可以尝试在文本字段后面放置一个不透明视图,这样它后面就没有透明度,直到集合视图本身,看看这是否有所改善。
如果这不起作用,请澄清为什么以及如何使用文本字段的 -initWithFrame: 方法......似乎是一种奇怪的做事方式。
This almost seems like it's more a problem with how the collection view is animated. Have you turned on wantsLayers for your prototype collection item view? Try turning it off. Also try putting an opaque view behind the text field so there's no transparency behind it all the way through to the collection view itself and see that improves things.
If that doesn't work, please clarify why and exactly how you're using the -initWithFrame: method of the text field ... seems to be a strange way of doing things.