2012-02-17 25 views
1

我的應用程序(完全用戶友好)使用輔助功能API來獲取其他應用程序擁有的文本字段的內容。通過可訪問性API從其他應用的文本字段獲取NSAttributedString?

我使用的是優秀的Apple提供的輔助功能檢查器幫助和啓發

https://developer.apple.com/library/mac/#documentation/Accessibility/Conceptual/AccessibilityMacOSX/OSXAXTesting/OSXAXTestingApps.html

我已經包含該項目的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); 

因此,我可以將kAXValueAttribute(又名應用程序文本字段的文本內容)作爲明文NSString

有沒有辦法做同樣的事情,但得到NSAttributedString?我需要提取當前文本字段中使用的字體名稱和字體大小。

最終目標是使用字體名稱和字體大小來計算脫字符在文本字段中的位置。 (我已經可以做到這一點,給定字體名稱/大小)。但如果有更好的方法來計算插入位置,那將是一個更好的答案。在這種情況下,我不需要獲取NSAttributedString。

+0

剛發現這個:http://lists.apple.com/archives/accessibility-dev/2010/Nov/msg00044.html - 看起來像一個問題一樣的問題。現在通過它。 – cksubs 2012-02-18 05:46:35

回答

相關問題