NSTextField 的属性字符串渲染延迟

发布于 2024-12-23 10:54:31 字数 1398 浏览 2 评论 0原文

我在 IB 中有一个标签(NSTextField),它绑定到控制器。 控制器在 awakeFromNIB 上设置标签的 attributeStringValue 以包含一些彩色文本和一个或两个链接。

当您看到标签时,它包含正确的字符串值,但某些格式会丢失 - 直到您单击标签,它会更新以包含正确的格式。

我使用此代码来设置值:

[self.testTextField setAllowsEditingTextAttributes:YES];
[self.testTextField setSelectable:YES];
NSMutableAttributedString *linkString = [[NSMutableAttributedString alloc] initWithString:@"hit this "];

[linkString beginEditing];

NSMutableAttributedString* attrString = [[NSMutableAttributedString alloc] initWithString:@"link"];
NSRange range = NSMakeRange(0, [attrString length]);

[attrString addAttribute:NSLinkAttributeName value:[[NSURL URLWithString:@"http://google.com"] absoluteString] range:range];
[attrString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInt:NSUnderlinePatternDot] range:range];
[attrString addAttribute:NSForegroundColorAttributeName value:[NSColor blackColor] range:range];
[linkString appendAttributedString:attrString];

[linkString appendAttributedString:[[NSAttributedString alloc] initWithString:@" to search"]];

[linkString addAttribute:NSForegroundColorAttributeName value:[NSColor redColor] range:NSMakeRange(0, [linkString length])];

[linkString endEditing];

[self.testTextField setAttributedStringValue:linkString];

根据此示例,您将看到红色且采用默认标签字体的字符串。 然后,当您单击标签时,字体大小和外观会发生变化,并且链接会神奇地呈现。

关于如何让字符串第一次正确渲染有什么想法吗?

I've got an Label (NSTextField) in IB that's bound to a controller.
The controller, on awakeFromNIB, sets the attributedStringValue of the label to contain some coloured text and a link or two.

When you see the label it contains the correct string value, but some of the formatting is lost - until you click on the label, and it updates to contain the correct formatting.

I'm using this code to set the value:

[self.testTextField setAllowsEditingTextAttributes:YES];
[self.testTextField setSelectable:YES];
NSMutableAttributedString *linkString = [[NSMutableAttributedString alloc] initWithString:@"hit this "];

[linkString beginEditing];

NSMutableAttributedString* attrString = [[NSMutableAttributedString alloc] initWithString:@"link"];
NSRange range = NSMakeRange(0, [attrString length]);

[attrString addAttribute:NSLinkAttributeName value:[[NSURL URLWithString:@"http://google.com"] absoluteString] range:range];
[attrString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInt:NSUnderlinePatternDot] range:range];
[attrString addAttribute:NSForegroundColorAttributeName value:[NSColor blackColor] range:range];
[linkString appendAttributedString:attrString];

[linkString appendAttributedString:[[NSAttributedString alloc] initWithString:@" to search"]];

[linkString addAttribute:NSForegroundColorAttributeName value:[NSColor redColor] range:NSMakeRange(0, [linkString length])];

[linkString endEditing];

[self.testTextField setAttributedStringValue:linkString];

Based on this example, you'll see the string coloured red and in the default Label font.
Then when you click on the label the font changes size and face and the link magically renders.

Any ideas on how to get the string to render correctly the first time?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

执着的年纪 2024-12-30 10:54:31

我遇到了同样的问题。我找到的解决方案是在属性字符串上显式设置 NSFontAttributeName 。我创建了一个 NSFont 对象,该对象与我在 IB 中为文本字段设置的字体相匹配,并像这样设置该属性:

NSFont *font = [NSFont fontWithName:@"Lucida Grande" size:(CGFloat)13.0];
[attrString addAttribute:NSFontAttributeName value:font range:range];

I ran into this same problem. The solution I found was to explicitly set the NSFontAttributeName on the attributed string. I created an NSFont object that matched the font I had set in IB for my textfield and set that attribute like so:

NSFont *font = [NSFont fontWithName:@"Lucida Grande" size:(CGFloat)13.0];
[attrString addAttribute:NSFontAttributeName value:font range:range];
恰似旧人归 2024-12-30 10:54:31

据我所知,这只是 AppKit 的正常怪异之处。

我已经成功地使用此自定义类将文本字段呈现为链接,您只需将其添加到界面生成器中并像平常一样设置其属性字符串值:

DSClickableURLTextField / Swift DSClickableURLTextField 分叉。

您还可以选择使用 NSButton,尽管这比较麻烦,而且不做额外的工作就无法获得手形光标。

As far as I know that's just normal AppKit weirdness.

I've had success using this custom class to render text fields as links, you just add it in interface builder and set its attributed string value like normal:

DSClickableURLTextField / Swift DSClickableURLTextField fork.

You also have the option of using an NSButton, though that's more of a pain, and you don't get the hand cursor without extra work.

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