时间:2019-03-08 标签:c#wpftextblock
c# wpf textblock
我需要在文本块的字母之间给出 space 作为 34。
文本块有 属性 和 Font-Stretch 属性
但它有自己的(超扩展,浓缩,......等等)
<Style x:Key="diningcode" TargetType="TextBlock">
<Setter Property="FontStretch" Value="UltraExpanded"/>
</Style>
我想把这个'ultraExpanded'属性改成34
有什么解决办法吗?
我相信你可以自己尝试找出答案,但显然你不能评估 -10
到 FontStretch
属性 因为 属性 只接受一个选项集合作为输入值。
您要查找的选项可能是 FontStretches.ExtraCondensed
。建议你去MSDN阅读:https://msdn.microsoft.com/en-us/library/system.windows.fontstretches.extracondensed(v=vs.110).aspx
你可以试试这个:
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;
}
对于 xaml 使用:
<local:AdvancedStretchTextBlock Text=""... Tracking="-10"/>
我需要在文本块的字母之间给出 space 作为 34。
文本块有 属性 和 Font-Stretch 属性
但它有自己的(超扩展,浓缩,......等等)
<Style x:Key="diningcode" TargetType="TextBlock">
<Setter Property="FontStretch" Value="UltraExpanded"/>
</Style>
我想把这个'ultraExpanded'属性改成34 有什么解决办法吗?
我相信你可以自己尝试找出答案,但显然你不能评估 -10
到 FontStretch
属性 因为 属性 只接受一个选项集合作为输入值。
您要查找的选项可能是 FontStretches.ExtraCondensed
。建议你去MSDN阅读:https://msdn.microsoft.com/en-us/library/system.windows.fontstretches.extracondensed(v=vs.110).aspx
你可以试试这个:
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;
}
对于 xaml 使用:
<local:AdvancedStretchTextBlock Text=""... Tracking="-10"/>