2012-05-08 49 views
1

是否可以在TextEdit(通过ObjC或Carbon)中检索选定文本的字体名称? 我tryed这个代码,但 “价值” 为空:以文本编辑方式获取选定文本字体名称

AXUIElementRef systemWideElement = AXUIElementCreateSystemWide(); 
AXUIElementRef focussedElement = NULL; 
AXError error = AXUIElementCopyAttributeValue(systemWideElement, 
    kAXFocusedUIElementAttribute, (CFTypeRef*)&focussedElement); 
CFTypeRef value; 
AXUIElementCopyAttributeValue(focussedElement, kAXFontTextAttribute, &value); 

感谢。

+0

'AXUIElementCopyAttributeValue'返回后'focussedElement'的值是什么? –

+0

AXUIElementCopyAttributeValue返回后focussedElement的值有效(非空)。 –

回答

1

请注意,在AXTextAttributedString.h中声明kAXFontTextAttribute。它不是UI元素的属性;它是属性字符串中文本的属性。

尝试使用kAXAttributedStringForRangeParameterizedAttribute代替,将您获得的值传递给kAXSelectedTextRangeAttribute。 (假设关注的UI元素是一个AXTextArea,你不应该这样假设)。这将返回一个AXTextAttributedString,你可以通过kAXFontTextAttribute属性获得一个字体字典。

+0

Thankyou,它的工作原理,但我不知道如何从返回值检索字体名称。你可以帮我吗? –

+0

@mhtaqia:从属性字符串或字体字典? –

+0

我试过这个:CFAttributedStringRef attr_str; AXUIElementCopyAttributeValue(focussedElement,kAXAttributedStringForRangeParameterizedAttribute,(CFTypeRef *)&attr_str); attr_str返回(不为空),我尝试通过CFAttributedStringGetLength(attr_str)获得长度,但出现错误,也尝试CFDictionaryRef dic = CFAttributedStringGetAttributes(attr_str,0,NULL)和错误发生,似乎attr_str不是一个AttributedString类型?我使用错误的方法? –

相关问题