MacOS app : How to center an attributed string?

asked 2021-04-15T10:44:55.43+00:00
Gilbert Tordeur 116 Reputation points

Hello.
MacOS 11.2.3. Visual Studio for Mac community 8.9.5. All updates on stable channel done.
I have written a method to display an attributed string in a textfield. This method should set a color for some characters, and optionally a paragraph alignment. No problem with color, but the statement "ps.Alignment = alignment;" generates a System.NotImplementedException. Why? How could I fix this problem?
Thank you.

    /// <summary>
    /// Affiche dans un NSTextField un texte où ♠, ♥, ◊ et ♣ sont colorés.
    /// </summary>
    /// <param name="textField">NSTextField à garnir</param>
    /// <param name="text">Texte source</param>
    public static void AfficherTexteColore(NSTextField textField, string text, NSTextAlignment alignment = NSTextAlignment.Natural)
    {
        if (string.IsNullOrWhiteSpace(text))
        {
            textField.StringValue = string.Empty;
        }
        else
        {
            var attrString = new NSMutableAttributedString(text);
            for (int i = 0; i < text.Length; i++)
            {
                NSStringAttributes sa = new NSStringAttributes();
                NSRange range;

                switch (text[i])
                {
                    case CgtCarte.ChrPiqueGr:
                        range = new NSRange(i, 1);
                        sa.ForegroundColor = CgtCteNSCouleur.ClrPique;
                        attrString.AddAttributes(sa, range);
                        break;

                    case CgtCarte.ChrCoeurGr:
                        range = new NSRange(i, 1);
                        sa.ForegroundColor = CgtCteNSCouleur.ClrCoeur;
                        attrString.AddAttributes(sa, range);
                        break;

                    case CgtCarte.ChrCarreauGr:
                        range = new NSRange(i, 1);
                        sa.ForegroundColor = CgtCteNSCouleur.ClrCarreau;
                        attrString.AddAttributes(sa, range);
                        break;

                    case CgtCarte.ChrTrefleGr:
                        range = new NSRange(i, 1);
                        sa.ForegroundColor = CgtCteNSCouleur.ClrTrefle;
                        attrString.AddAttributes(sa, range);
                        break;

                    default:
                        break;
                }
            }

            if (alignment != NSTextAlignment.Natural)
            {
                NSParagraphStyle ps = new NSParagraphStyle();
                ps.Alignment = alignment;
                NSRange range = new NSRange(0, text.Length);
                attrString.AddAttribute(new NSString("ParagraphAlignment"), ps, range);
            }

            textField.AttributedStringValue = attrString;
        }
    }
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
6,922 questions
No comments
{count} votes

0 additional answers

Sort by: Most helpful