I'm trying to colorize a substring in my NSMutableAttributedString in Xamarin, but it seems to be missing the proper constants,
What should I put there?
Update. This gets closer:
var s = new NSMutableAttributedString ("hello");
s.AddAttribute (CTStringAttributeKey.ForegroundColor , NSColor.Red, new NSRange (0, 3));
wordLabel.AttributedStringValue = s;
and gives
though the color on screen is still black text!
Update2 Maybe CTStringAttributeKey is the wrong one, but there is no NSStringAttributeKey
In Visual Studio for Mac, right-click on the existing Xamarin. Forms solution and choose Add > Add New Project... In the New Project window choose Mac > App > Cocoa App and press Next. Type an App Name (and optionally choose a different name for the Dock Item), then press Next.
With Xamarin. Forms, you can use C# or XAML to build cross-platform user interfaces for iOS, Android, and macOS.
What is Xamarin on Mac? You can think of Xamarin as an open-source platform for building applications. You can create cross-platform applications for Windows, Android, iOS using. NET.
In case people are wondering what the equivalent is for Xamarin iOS, here it is: The foreground color attribute name can be found here: UIStringAttributeKey.ForegroundColor
Also NSColor.Red
should be UIColor.Red
So adding an attribute should look like:
s.AddAttribute(UIStringAttributeKey.ForegroundColor, UIColor.Red, new NSRange(0,3));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With