【问题标题】:Programmatically getting selected text font name in TextEdit以编程方式在 TextEdit 中获取选定的文本字体名称
【发布时间】:2012-05-08 04:15:42
【问题描述】:

是否可以在 TextEdit 中检索所选文本的字体名称(通过 ObjC 或 Carbon)? 我尝试了这段代码,但“值”为空:

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

谢谢。

【问题讨论】:

  • AXUIElementCopyAttributeValue返回后focussedElement的值是多少?
  • AXUIElementCopyAttributeValue返回后focussedElement的值有效(非空)。

标签: macos macos-carbon textedit


【解决方案1】:

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

改用kAXAttributedStringForRangeParameterizedAttribute,传递您为kAXSelectedTextRangeAttribute 获得的值。 (假设聚焦的 UI 元素是一个 AXTextArea。您不应该假设。)这将返回一个 AXTextAttributedString,您可以从中通过 kAXFontTextAttribute 属性获取字体字典。

【讨论】:

  • 谢谢,它有效,但我不知道如何从返回值中检索字体名称。你能帮帮我吗?
  • @mhtaqia:来自属性字符串还是字体字典?
  • 我试过这个:CFAttributedStringRef attr_str; AXUIElementCopyAttributeValue(focussedElement, kAXAttributedStringForRangeParameterizedAttribute, (CFTypeRef*)&attr_str); attr_str 返回(非空),我尝试通过 CFAttributedStringGetLength(attr_str) 获取长度,但发生错误,还尝试了 CFDictionaryRef dic = CFAttributedStringGetAttributes(attr_str, 0, NULL) 并发生错误,看来 attr_str 不是 AttributedString类型?我是不是用错了方法?
  • @mhtaqia:由于是参数化属性,所以需要使用不同的函数来获取属性字符串:AXUIElementCopyParameterizedAttributeValue
  • 我试了UIBrowser,这个程序显示AttributedString是空的;似乎 TextEdit 没有实现 AttributedString。不过谢谢彼得。
猜你喜欢
  • 2012-07-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-15
  • 1970-01-01
  • 2015-11-15
  • 2014-01-17
  • 1970-01-01
相关资源
最近更新 更多