在 NSCollectionView 中,具有自动调整文本大小的 NSTextField 看起来很糟糕

发布于 2024-10-30 08:08:10 字数 866 浏览 7 评论 0原文

我这里遇到一个小问题。我有一个 NSCollectionView ,其中包含一个视图集合,其中有一个 NSTextField 显示这些字体中的字体名称。这是一个屏幕截图: screenie

如您所见,某些字体看起来有问题,它们后面有 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:
screenie

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

相权↑美人 2024-11-06 08:08:10

这看起来更像是集合视图如何动画化的问题。您是否为原型集合项目视图启用了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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文