Editar

Compartir a través de


TextPattern.FontNameAttribute Field

Definition

Identifies the FontName attribute of a text range.

public: static initonly System::Windows::Automation::AutomationTextAttribute ^ FontNameAttribute;
public static readonly System.Windows.Automation.AutomationTextAttribute FontNameAttribute;
 staticval mutable FontNameAttribute : System.Windows.Automation.AutomationTextAttribute
Public Shared ReadOnly FontNameAttribute As AutomationTextAttribute 

Field Value

Examples

The following example shows how to obtain an attribute value for a range of text in Microsoft Notepad.

Note

Microsoft Notepad is used as the text provider for example purposes only. Microsoft Notepad is not guaranteed to support the attribute value being requested.

/// -------------------------------------------------------------------
/// <summary>
/// Outputs the FontNameAttribute value for a range of text.
/// </summary>
/// <param name="targetTextElement">
/// The AutomationElment that represents a text control.
/// </param>
/// -------------------------------------------------------------------
private void GetFontNameAttribute(AutomationElement targetTextElement)
{
    TextPattern textPattern = 
        targetTextElement.GetCurrentPattern(TextPattern.Pattern) as TextPattern;

    if (textPattern == null)
    {
        // Target control doesn't support TextPattern.
        return;
    }

    // If the target control doesn't support selection then return.
    // Otherwise, get the text attribute for the selected text.
    // If there are currently no selections then the text attribute 
    // will be obtained from the insertion point.
    TextPatternRange[] textRanges;
    if (textPattern.SupportedTextSelection == SupportedTextSelection.None)
    {
        return;
    }
    else
    {
        textRanges = textPattern.GetSelection();
    }

    foreach (TextPatternRange textRange in textRanges)
    {
        Object textAttribute =
            textRange.GetAttributeValue(
            TextPattern.FontNameAttribute);

        if (textAttribute == TextPattern.MixedAttributeValue)
        {
            // Returns MixedAttributeValue if the value of the 
            // specified attribute varies over the text range. 
            Console.WriteLine("Mixed fonts.");
        }
        else if (textAttribute == AutomationElement.NotSupported)
        {
            // Returns NotSupported if the specified attribute is 
            // not supported by the provider or the control. 
            Console.WriteLine(
                "FontNameAttribute not supported by provider.");
        }
        else
        {
            Console.WriteLine(textAttribute.ToString());
        }
    }
}
''' -------------------------------------------------------------------
''' <summary>
''' Outputs the FontNameAttribute value for a range of text.
''' </summary>
''' <param name="targetTextElement">
''' The AutomationElement. that represents the text provider.
''' </param>
''' -------------------------------------------------------------------
Private Sub GetFontNameAttribute( _
ByVal targetTextElement As AutomationElement)
    Dim targetTextPattern As TextPattern = _
        DirectCast(targetTextElement.GetCurrentPattern( _
        TextPattern.Pattern), TextPattern)

    If (targetTextPattern Is Nothing) Then
        ' Target control doesn't support TextPattern.
        Return
    End If

    ' If the target control doesn't support selection then return.
    ' Otherwise, get the text attribute for the selected text.
    ' If there are currently no selections then the text attribute 
    ' will be obtained from the insertion point.
    Dim textRanges() As TextPatternRange
    If (targetTextPattern.SupportedTextSelection = SupportedTextSelection.None) Then
        Return
    Else
        textRanges = targetTextPattern.GetSelection()
    End If

    Dim textRange As TextPatternRange
    For Each textRange In textRanges
        Dim textAttribute As Object = _
            textRange.GetAttributeValue( _
            TextPattern.FontNameAttribute)

        If (textAttribute = TextPattern.MixedAttributeValue) Then
            ' Returns MixedAttributeValue if the value of the 
            ' specified attribute varies over the text range. 
            Console.WriteLine("Mixed fonts.")
        ElseIf (textAttribute = AutomationElement.NotSupported) Then
            ' Returns NotSupported if the specified attribute is 
            ' not supported by the provider or the control. 
            Console.WriteLine( _
            "FontNameAttribute not supported by provider.")
        Else
            Console.WriteLine(textAttribute.ToString())
        End If
    Next
End Sub

Remarks

Example values for this attribute include "Arial Black" and "Arial Narrow".

The values for this attribute are non-localized.

This identifier is used by UI Automation client applications. UI Automation providers should use the equivalent field in TextPatternIdentifiers.

UI Automation clients get the value of the attribute by calling GetAttributeValue.

Values for this attribute are of type String. The default value is an empty string.

Applies to

See also