WPF, эквивалентный TextRenderer

Я думаю, что ваша вторая строка будет также содержать запятую между словами, если да, то это легко достичь.

вы можете разделить строку 1 и 2 запятой в качестве разделителя, как этот

let firstString = 1st_string.split(',');
let secondString = 2nd_string.split(',');

, после того как вы получите переменные firstString и secondString в виде массива, вы можете выполнить итерацию первый массив и проверка на дубликаты с использованием методов

for (let i in firstString) {
    if(secondString.includes(firstString[i])){
        //you can do whatever you want after finding duplicate here;
    }
}
14
задан Lipis 7 June 2013 в 16:17
поделиться

2 ответа

Посмотрите на класс FormattedText

Если вам нужно более детальное управление, вам нужно перейти к члену типа AdvanceWidths типа GlyphTypeface. Нашел подобное обсуждение здесь с фрагментом кода, который выглядит так, как будто он может работать.

Обновление : Похоже, это может быть дубликатом Измерения текста в WPF .. ОП, пожалуйста, подтвердите.

4
ответ дан 1 December 2019 в 12:02
поделиться

Спасибо, Гишу,

Читая ваши ссылки, я пришел к следующему, и оба из них работают за меня:

    /// <summary>
    /// Get the required height and width of the specified text. Uses FortammedText
    /// </summary>
    public static Size MeasureTextSize(string text, FontFamily fontFamily, FontStyle fontStyle, FontWeight fontWeight, FontStretch fontStretch, double fontSize)
    {
        FormattedText ft = new FormattedText(text,
                                             CultureInfo.CurrentCulture,
                                             FlowDirection.LeftToRight,
                                             new Typeface(fontFamily, fontStyle, fontWeight, fontStretch),
                                             fontSize,
                                             Brushes.Black);
        return new Size(ft.Width, ft.Height);
    }

    /// <summary>
    /// Get the required height and width of the specified text. Uses Glyph's
    /// </summary>
    public static Size MeasureText(string text, FontFamily fontFamily, FontStyle fontStyle, FontWeight fontWeight, FontStretch fontStretch, double fontSize)
    {
        Typeface typeface = new Typeface(fontFamily, fontStyle, fontWeight, fontStretch);
        GlyphTypeface glyphTypeface;

        if(!typeface.TryGetGlyphTypeface(out glyphTypeface))
        {
            return MeasureTextSize(text, fontFamily, fontStyle, fontWeight, fontStretch, fontSize);
        }

        double totalWidth = 0;
        double height = 0;

        for (int n = 0; n < text.Length; n++)
        {
            ushort glyphIndex = glyphTypeface.CharacterToGlyphMap[text[n]];

            double width = glyphTypeface.AdvanceWidths[glyphIndex] * fontSize;

            double glyphHeight = glyphTypeface.AdvanceHeights[glyphIndex]*fontSize;

            if(glyphHeight > height)
            {
                height = glyphHeight;
            }

            totalWidth += width;
        }

        return new Size(totalWidth, height);
    }
21
ответ дан 1 December 2019 в 12:02
поделиться
Другие вопросы по тегам:

Похожие вопросы: