我的应用程序(完全对用户有利)使用辅助功能 API 来获取其他应用程序拥有的文本字段的内容。
我正在使用 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。
最佳答案
How to get global screen coordinates of currently selected text via Accessibility APIs.
此方法使用当前选定的文本来获取选定文本矩形的边界。比通过字体名称/大小计算位置要好得多。
棘手的部分是您无法获取空选择(也称为裸文本插入点插入符)的边界。因此,您需要伪造一个选择,然后将其传递给计算边界矩形的方法。
关于objective-c - 通过辅助功能 API 从其他应用程序的文本字段获取 NSAttributedString?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9336981/