1

I need to give space between letter of text-block as 34.
text-Block has property with Font-Stretch property
but it has its own (Ultraexpanded,condensed, ....such and such)

       <Style x:Key="diningcode" TargetType="TextBlock">


            <Setter Property="FontStretch" Value="UltraExpanded"/>

        </Style>   

I want to change this 'ultraExpanded' property to 34 any solution?

2 Answers 2

2

You may try this :

public class AdvancedStretchTextBlock : TextBlock
{
    /// <summary>
    /// Defines charachter/letter spacing
    /// </summary>
    public int Tracking
    {
        get => (int)GetValue(TrackingProperty);
        set => SetValue(TrackingProperty, value);
    }

    public static readonly DependencyProperty TrackingProperty =
        DependencyProperty.Register("Tracking", typeof(int), typeof(AdvancedStretchTextBlock),
            new UIPropertyMetadata(0,
                TrackingPropertyChanged));

    static void TrackingPropertyChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
    {
        if (!(o is AdvancedStretchTextBlock tb) || String.IsNullOrEmpty(tb.Text))
            return;

        tb._tracking.X = (int)e.NewValue;
        tb._trackingAlignment.X = -(int)e.NewValue * tb.Text.Length;

        if (tb._lastTrackingTextLength == tb.Text.Length)
            return; // Avoid re-creating effects when you don't have to..

        // Remove unused effects (string has shortened)
        while (tb._trackingEffects.Count > tb.Text.Length)
        {
            tb.TextEffects.Remove(tb._trackingEffects[tb._trackingEffects.Count - 1]);
            tb._trackingEffects.RemoveAt(tb._trackingEffects.Count - 1);
        }

        tb._lastTrackingTextLength = tb.Text.Length;

        // Add missing effects (string has grown)
        for (int i = tb._trackingEffects.Count; i < tb.Text.Length; i++)
        {
            var fx = new TextEffect()
            {
                PositionCount = i,
                Transform = tb._tracking
            };
            tb._trackingEffects.Add(fx);
            tb.TextEffects.Add(fx);
        }

        // Ugly hack to fix overall alignment
        tb.RenderTransform = tb._trackingAlignment;

    }

    private readonly TranslateTransform _tracking = new TranslateTransform();
    private readonly TranslateTransform _trackingAlignment = new TranslateTransform();
    private readonly List<TextEffect> _trackingEffects = new List<TextEffect>();
    int _lastTrackingTextLength;

}

And for the xaml use :

<local:AdvancedStretchTextBlock Text=""... Tracking="-10"/>

ref : https://social.msdn.microsoft.com/Forums/vstudio/en-US/789c3e1b-e3ae-476f-b37f-d93ef6d0cb7b/character-spacing-in-textblocktextelement?forum=wpf

Sign up to request clarification or add additional context in comments.

3 Comments

Thx, seemed like it working. actually I tried to create the class CondensedTextBlock : TextBlock but it does not worked inside of the canvas. but your 'AdvancedStretchTextBlock ' work fine in canvas. what make it work fine in canvas ?
if I want to understand your code any resource or tutorial u suggest?
0

I believe you can try it out yourself to find out the answer, but obviously you cannot assess -10 to FontStretch property as the property only accept a collection of options as input value. The option you're looking for may be FontStretches.ExtraCondensed. I suggest you go to MSDN to read: https://msdn.microsoft.com/en-us/library/system.windows.fontstretches.extracondensed(v=vs.110).aspx

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.