例如,我有一个NSTextField显示为标签,其内容为:“你好,苹果!我来了!”。我想找到“ Apple”部分,并对它们上方的字符进行一些操作(例如,检查是否在它们上面触发了-mouseDown:事件)。我可以得到这个单词的位置和大小吗?先感谢您!根据alastair的回答,我编写了一个.m文件进行测试。这是摘要:/* Some definations to identify the complete text and the link */#define _CFG_AMCHyperLinkTest_Text @"Hello, this is an Apple website!"#define _CFG_AMCHyperLinkTest_Link @"Apple"以及实际的测试代码:/* test */- (void)oneTimeInit{ AMCDebug(@"Pre Text rect: %@\nlink rect: %@", [AMCTools descriptionWithNSRect:[_labelHyperlink frame]], [AMCTools descriptionWithNSRect:[_labelLink frame]]); NSRange linkRange = [_CFG_AMCHyperLinkTest_Text rangeOfString:_CFG_AMCHyperLinkTest_Link]; NSRect boundingRect; [_labelHyperlink setStringValue:_CFG_AMCHyperLinkTest_Text]; _layoutManager = [[NSLayoutManager alloc] init]; _textStorage = [[NSTextStorage alloc] initWithString:@"Apple"]; _textContainer = [[NSTextContainer alloc] initWithContainerSize:[_labelHyperlink frame]]; [_layoutManager setTextStorage:_textStorage]; [_layoutManager addTextContainer:_textContainer]; linkRange = [_layoutManager glyphRangeForCharacterRange:linkRange actualCharacterRange:NULL]; AMCDebug(@"Char range: %@\nActual range: %@", [AMCTools descriptionWithNSRange:[_CFG_AMCHyperLinkTest_Text rangeOfString:_CFG_AMCHyperLinkTest_Link]], [AMCTools descriptionWithNSRange:linkRange]); boundingRect = [_layoutManager boundingRectForGlyphRange:linkRange inTextContainer:_textContainer]; AMCDebug(@"Bounding rect: %@", [AMCTools descriptionWithNSRect:boundingRect]); /* Now, we should set the link rect */ NSRect linkRect; linkRect.size = [AMCTools string:_CFG_AMCHyperLinkTest_Link sizeWithSystemFontSize:13.0F]; linkRect.origin.x = [_labelHyperlink frame].origin.x + boundingRect.origin.x; linkRect.origin.y = [_labelHyperlink frame].origin.y + boundingRect.origin.y; // I have another text field who used to catch some mouse event and acted as an hyperlink. // There are some codes to apply the "linkRect" to it. ...... /* Some codes less important */ AMCDebug(@"Text rect: %@\nlink rect: %@", [AMCTools descriptionWithNSRect:[_labelHyperlink frame]], [AMCTools descriptionWithNSRect:linkRect]);}并且上面出现了一些自定义方法的描述:AMCTools:我自己的工具,用于打印某些实例或数据结构的某些调试信息-string:sizeWithSystemFontSize::一种确定系统字体的最小文本大小的方法。AMCDebug():就像“ NSLog”一样,不要介意。这是输出:>> -[AMCHyperLinkTestViewController oneTimeInit] 2014-01-23 11:26:36.974 AMCTest[6665:303]Pre Text rect: (17.0,443.0), 199.0*17.0link rect: (64.0,47.0), 92.0*17.0>> -[AMCHyperLinkTestViewController oneTimeInit] 2014-01-23 11:26:36.979 AMCTest[6665:303]String size: 194.3*17.0>> Line 74, -[AMCHyperLinkTestViewController oneTimeInit] 2014-01-23 11:26:36.995 AMCTest[6665:303]Char range: (18 -> 23 <5>)Actual range: (5 -> 5 <0>)>> -[AMCHyperLinkTestViewController oneTimeInit] 2014-01-23 11:26:37.003 AMCTest[6665:303]Bounding rect: (37.0,0.0), 0.0*14.0 <-- This does not look correct>> -[AMCHyperLinkTestViewController oneTimeInit] 2014-01-23 11:26:37.007 AMCTest[6665:303]Text rect: (17.0,443.0), 199.0*17.0link rect: (54.0,443.0), 36.3*17.0它没有按我预期的那样工作。 最佳答案 如果您只是想添加超链接,则可以简单地将属性字符串值设置为包含合适的超链接的属性字符串。 (为此,请在要链接的范围内设置类型为NSLinkAttributeName的属性,并将值设置为NSURL对象。您可能还希望设置NSCursorAttributeName属性以更改用户的鼠标指针以及其他属性,以突出显示与用户的链接。)如果您尝试做一些更复杂的事情,请首先注意NSTextField在编辑时不会呈现自身(因为它实际上是在编辑时出现的字段编辑器),因此对可编辑的 s更复杂。否则,您可以使用NSTextField为您计算布局,该布局将告诉您所讨论单词的位置和大小。这非常复杂(因为文本呈现非常复杂),但实际上您需要:创建一个NSLayoutManager。创建一个NSLayoutManager并将字段中的文本放入其中。创建一个适当大小的NSTextStorage;对于单行字段,应将其设置得非常宽,以避免换行,但对于多行字段,则要使其与字段的大小相同(减去边框)。将文本存储和容器附加到布局管理器(NSTextContainer,-setTextStorage:)。使用-addTextContainer:将子字符串的文本范围转换为字形范围。使用例如获取边界矩形-glyphRangeForCharacterRange:actualCharacterRange:。但是,不要仅仅为了建立超链接而做所有这些事情。只需使用内置支持即可。
10-04 17:22