2012-05-08 48 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類型?我使用錯誤的方法? –

相關問題