【问题标题】:Wrapping word with Hyphen not work用连字符换行不起作用
【发布时间】:2014-08-14 06:17:38
【问题描述】:

我有一个文本块,其中包含一条带有连字符的消息。我希望所有包含连字符的单词都被解释为一个真正的单词,比如带有不间断空格的单词。 我暂时得到了这个:

test test test test Test-
test

我想要

test test test test 
Test-test

我觉得连字符是一个单词中用于分隔的特殊字符,这就是为什么我不能把这个词换行。但是这样做有什么想法吗? 我尝试使用这个 IsHyphenationEnabled 但我真的不知道他的工作......

!!!注意!!!我不想打断这个词,当一个带连字符的词不能插入一行时,我想要一个新行,因为它没有任何空间供这个词使用

谢谢你们, 最好的问候。

【问题讨论】:

  • IsHyphenationEnabled 可以反其道而行之,它可以帮助您打破常规。
  • IsHyphenationEnabled 只是分隔一个不能像这样包装的单词:带有 isIsHyphenationEnabled 的段成为段。这不是我想要的。我不想打断这个词,我只想要一个换行符,当带连字符的单词不能像我的例子一样出现在同一行时
  • 我理解您的担忧。 IsHyphenationEnabled 可帮助您打断一个长单词,但您想将打断的单词保持在一起。顺便说一句,您如何看待删除连字符以使其成为一个单词。我不确定这是否适用于所有情况。让我们看看如何解决这个问题。
  • 是的,如果我删除连字符很好,但我不能删除。该消息是一个项目规范。而且我们不能使用 => 在显示之前删除连字符以获得良好的换行并在之后重新发送连字符。
  • 你可能会使用转换器只是为了显示目的,所以不需要修改原始值。

标签: c# wpf word-wrap


【解决方案1】:

这是一个转换值仅用于显示的示例。

<Grid xmlns:l="clr-namespace:CSharpWPF"
      xmlns:sys="clr-namespace:System;assembly=mscorlib">
    <Grid.Resources>
        <sys:String x:Key="testString">some string with non-breaking space</sys:String>
        <l:HyphenRemover x:Key="HyphenRemover" />
    </Grid.Resources>
    <StackPanel>
        <TextBlock>
            <Run Text="Nomal string: " />
            <Run Text="{StaticResource testString}" />
        </TextBlock>
        <TextBlock>
            <Run Text="Hyphen removed: " />
            <Run Text="{Binding Source={StaticResource testString}, Converter={StaticResource HyphenRemover},Mode=OneWay}" />
        </TextBlock>
    </StackPanel>
</Grid>

上面的例子是为了说明目的,也可以直接绑定到文本块

例如

<TextBlock Text="{Binding Source={StaticResource testString}, Converter={StaticResource HyphenRemover}}" />

HyphenRemover 转换器

namespace CSharpWPF
{
    class HyphenRemover : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            string text = value as string;
            if (!string.IsNullOrWhiteSpace(text))
            {
                return text.Replace("-", string.Empty);
            }
            return value;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}

结果

您可以看到转换器能够在不修改源的情况下修改显示的值。

【讨论】:

  • 是的,这是一个很好的方法,我会试试这个。目前,我在文本中的好位置添加了 \n ,但这确实不是最好的。
  • 嗯,当您说:“您可以看到转换器能够在不修改源的情况下修改显示值。”你是什​​么意思?您可以看到转换器删除了连字符,这就是全部。我认为唯一的解决方案是添加 \n :(
  • 我的意思是说,通过使用转换器,我们没有修改源值,而是仅用于在文本块中显示的临时值。
【解决方案2】:

我已经通过手动确定应该在哪里放置换行符来解决这个问题。在我的例子中,我在包含我的 TextBlock 的控件上有一个方法,我将文本输入其中,但如果你想在 XAML 中设置文本,这也可以在依赖属性中使用。

public void ProcessText(TextBlock target, string text)
{
    //get display info about text block
    var maxLength = target.ActualWidth - target.Padding.Left - target.Padding.Right;
    var textBlockTypeFace = new Typeface(target.FontFamily, target.FontStyle, target.FontWeight,
        target.FontStretch);

    var words = text.Split(' ');
    var currentLine = "";

    for (var i = 0; i < words.Length; i++)
    {
        var word = words[i];

        //account for spaces between words if it's not the last word
        if (i == words.Length - 1) {
            word += " ";
        }

        var formattedText = new FormattedText(currentLine + word, CultureInfo.InvariantCulture,
            FlowDirection.LeftToRight, textBlockTypeFace, target.FontSize, target.Foreground);

        //if the line we're building fits, add the word to the text block and keep building the current line
        //otherwise, add a line break to the text block before adding the word and reset the current line
        if (formattedText.Width < maxLength)
        {
            currentLine += word;
            target.Text += word;
        }
        else
        {
            target.Text += Environment.NewLine + word;
            currentLine = word;
        }
    }
}

【讨论】:

    猜你喜欢
    • 2021-12-25
    • 1970-01-01
    • 2014-09-12
    • 2017-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-30
    • 2021-01-26
    相关资源
    最近更新 更多