Macos 我能知道部分NSTextField文本的准确矩形吗?
例如,我有一个NSTextField显示为标签,其内容是:“你好,苹果!我来了!” 我想找到“Apple”部分,并对上面的字符执行一些操作(例如,检查是否触发了-mouseDown:event)。我能知道这个词的位置和大小吗 提前谢谢你Macos 我能知道部分NSTextField文本的准确矩形吗?,macos,cocoa,label,nstextfield,Macos,Cocoa,Label,Nstextfield,例如,我有一个NSTextField显示为标签,其内容是:“你好,苹果!我来了!” 我想找到“Apple”部分,并对上面的字符执行一些操作(例如,检查是否触发了-mouseDown:event)。我能知道这个词的位置和大小吗 提前谢谢你 根据alastair的回答,我编写了一个.m文件来测试它。以下是摘要: /* Some definations to identify the complete text and the link */ #define _CFG_AMCHyperLinkTes
根据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
:我自己的工具,用于打印某些实例或数据结构的一些调试信息
-字符串: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.0
link 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.0
link rect: (54.0,443.0), 36.3*17.0
>>-[AMCHyperLinkTestViewController oneTimeInit]
2014-01-23 11:26:36.974美国测试[6665:303]
前文本矩形:(17.0443.0),199.0*17.0
链接矩形:(64.0,47.0),92.0*17.0
>>-[AMCHyperLinkTestViewController oneTimeInit]
2014-01-23 11:26:36.979美国测试[6665:303]
字符串大小:194.3*17.0
>>第74行,[AMCHyperLinkTestViewController oneTimeInit]
2014-01-23 11:26:36.995美国测试[6665:303]
字符范围:(18->23)
实际范围:(5->5)
>>-[AMCHyperLinkTestViewController oneTimeInit]
2014-01-23 11:26:37.003美国测试[6665:303]
边界矩形:(37.0,0.0),0.0*14.0>-[AMCHyperLinkTestViewController oneTimeInit]
2014-01-23 11:26:37.007美国测试[6665:303]
文本矩形:(17.0443.0),199.0*17.0
链接矩形:(54.0443.0),36.3*17.0
它没有像我预期的那样工作。如果您只是对放入超链接感兴趣,您可以简单地将属性字符串值设置为包含适当超链接的属性字符串。(要执行此操作,请在要创建链接的范围上设置类型为
NSLinkAttributeName
的属性,将值设置为NSURL
对象。您还可以设置nscursortributename
属性以更改用户的鼠标指针,也可以设置其他属性以向用户突出显示链接。)
如果您尝试执行更复杂的操作,首先请注意,NSTextField
在编辑时不会自动呈现(因为它实际上是在编辑时显示的字段编辑器),因此对可编辑的NSTextField
s执行此类操作更为复杂
否则,您可以使用NSLayoutManager
为您计算布局,它将告诉您所讨论的单词的位置和大小。这相当复杂(因为文本呈现相当复杂),但实际上您需要:
NSLayoutManager
NSTextStorage
,并将字段中的文本放入其中NSTextContainer
;对于单行字段,应使其非常宽以避免换行,但对于多行字段,应使其与字段大小相同(减少任何边框)-setTextStorage:
,-addTextContainer:
)-glyphRangeForCharacterRange:actualCharacterRange:
将子字符串的文本范围转换为字形范围-boundingrectfolyprange:inTextContainer:
获取边界矩形不过,不要只是为了制作一个超链接而这么做。只需使用内置支持即可。上次返回“bounding rect”的含义是什么?无论我为整个文本设置了什么(“Hello,Apple”或“Hello,that is Apple”),它总是将rect返回为“0.0*14.0”,顺便说一句,我使用-initWithContainerSize:sizeWithSystemFontSize:初始化NSTextContainer实例。非常感谢!这回答了我的问题。我认为您需要将
NSTextStorage
的内容设置为文本(即\u CFG\u AMCHyperLinkTest\u text
)。您的意思是-mutableString
?它是“\u CFG\u AMCHyperLinkTest\u Text”否,我的意思是您的\u textStorage
对象需要设置为\u CFG\u AMCHyperLinkTest\u Text
。目前您已将其设置为@“Apple”
。