question

GilbertTordeur-1363 avatar image
0 Votes"
GilbertTordeur-1363 asked Viorel-1 edited

MacOS app : How to center an attributed string?

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;
         }
     }
dotnet-csharp
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

Viorel-1 avatar image
0 Votes"
Viorel-1 answered Viorel-1 edited

According to https://xamarin.github.io/bugzilla-archives/41/41994/bug.html, try NSMutableParagraphStyle.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.