我的应用程序(完美用户仁慈)使用辅助功能app来抓取其他应用程序拥有的文本字段的内容。
我正在使用Apple提供的优秀的辅助功能检查器来获取帮助和灵感
我已经包含了该项目的UIElementUtilities
类,我可以使用它来执行以下操作:
//Get focused App
AXUIElementRef frontMostApp;
frontMostApp = getFrontMostApp();
//Get focused UIElement (Such as a textfield)
AXUIElementRef focusedUIElement;
AXUIElementCopyAttributeValue(frontMostApp, kAXFocusedUIElementAttribute, (CFTypeRef *)&focusedUIElement);
NSString *textfieldContents = [UIElementUtilities descriptionForUIElement:focusedUIElement attribute:(NSString *)kAXValueAttribute beingVerbose:NO];
//descriptionForUIElement:attribute:beingVerbose uses `AXUIElementCopyAttributeValue`
NSLog(@"TEXT FIELD CONTENTS: %@", textfieldContents);
因此,我可以以纯文本NSString
的形式获取kAXValueAttribute
(也称为应用程序文本字段的文本内容)。
有没有办法做同样的事情,但是得到一个NSAttributedString
?我需要提取当前在文本字段中使用的字体名称和字体大小。
最终目标是使用字体名称和字体大小来计算插入符号在文本字段中的位置。(给定字体名称/大小,我已经可以这样做了)。但如果有更好的方法来计算插入符号的位置,那将是一个更好的答案。在这种情况下,我不需要获取NSAttributedString。
发布于 2012-02-24 03:24:09
How to get global screen coordinates of currently selected text via Accessibility APIs.
此方法使用当前选定的文本来获取选定文本矩形的边界。比通过字体名称/大小计算位置要好得多。
棘手的部分是你不能得到一个空选择的边界(也就是一个纯文本插入点插入符号)。因此,您需要分解一个selection,然后将它传递给selection以计算边界矩形的方法。
https://stackoverflow.com/questions/9336981
复制相似问题