Mac Guides | Samples

MonoMac.Foundation.NSAttributedString Class

Strings that can be annotated with a set of attributes.

See Also: NSAttributedString

Syntax

[MonoMac.Foundation.Register("NSAttributedString", true)]
public class NSAttributedString : NSObject

Remarks

The NSAttributedString type represents a string that has a series of attributes applied uniformly.

The companion NSMutableAttributedString type can be used to create attributed strings that have overlapping attributes and whose contents can be modified after creation.

These types typically are used to attach information to elements of the string that dictate the font and colors to use as well as rendering attributes like kerning or how to render ligatures on a specific run of code.

These classes do not dictate the meaning or behavior of the attributes on a string, they merely keep track of the attributes. The actual meaning of these attributes is interpreted by the consumer of these objects.

NSAttributedStrings are created with a string and a set of attributes. The default constructor takes a string and an NSDictionary object where the keys represent the attributes and the values on each element represent the value of that attribute.

To simplify many common scenarios, MonoMac provides constructors with strong-types to easily create attributed strings for use with CoreText or UIKit. These constructors provide type-safety and eliminate programming errors caused by accidentally creating attributes that are not recognized by a backend.

To create NSAttributedStrings that you can use with CoreText's rendering, you create an instance of the CTStringAttributes class, set its properties to the attributes that you desire, and then invoke the NSAttributedString constructor with it.

To create NSAttributedStrings that you can use with UIKit's rendering, you create an instance of the UIStringAttributes class, set its properties to the attributes that you desire, and then invoke the NSAttributedString constructor with it.

The examples below show how to use the C# object initializer syntax to initialize the CTStringAttributes and the UIStringAttributes to setup your attributes. You can later use these attributes multiple times with different attributed strings:

c# Example

// 
// Using NSAttributedString with CoreText
//
var attributedString = new NSAttributedString ("Hello, world",
       new CTStringAttributes () {
              ForegroundColorFromContext =  true,
              Font = new CTFont ("Arial", 24)
       });

// Pass the NSAttributedString to a CTLine and draw the CTLine.
using (var textLine = new CTLine (attributedString)) {
       textLine.Draw (gctx);
}

// 
// Using NSAttributedString with UIKit
//
var myText = new NSAttributedString ("Hello, world",
       new UIStringAttributes () {
              ForegroundColor =  UIColor.Red,
	      KerningAdjustment = 3
       });
label.AttributedText = myText;

While both CTStringAttributes and UIStringAttributes are useful to create attributes that can later be used with attributed strings, it is also possible to use the NSAttributedString.NSAttributedString(string+str,UIFont+font,UIColor+foregroundColor,UIColor+backgroundColor,UIColor+strokeColor,NSParagraphStyle+paragraphStyle,NSLigatureType+ligatures,float+kerning,NSUnderlineStyle+underlineStyle,NSShadow+shadow,float+strokeWidth,NSUnderlineStyle+strikethroughStyle) constructor with C# named parameters for quickly creating attributed strings inline.

c# Example

//
// This example shows how to create an NSAttributedString for
// use with UIKit without creating the attributes separately
//
var text = new NSAttributedString (
    "Hello, World",
    font: UIFont.FromName ("HoeflerText-Regular", 24.0f),
    foregroundColor: UIColor.Red,
    strokeWidth: 4
);

It is also possible to create NSAttributedStrings with the NSDictionary API, although that does not offer any type safety nor prevents common errors. To use it, you typically use the various NSString properties that end in "AttributedName" that are part of this class.

c# Example

//
// This example shows how to create an NSAttributedString for
// use with UIKit using NSDictionaries
//
var dict = new NSMutableDictionary () {
    { NSAttributedString.FontAttributeName, UIFont.FromName ("HoeflerText-Regular", 24.0f), },
    { NSAttributedString.ForegroundColorAttributeName, UIColor.Black }
};

var text = new NSAttributedString (
	"Hello, World", dict);

Related content

Requirements

Namespace: MonoMac.Foundation
Assembly: XamMac (in XamMac.dll)
Assembly Versions: 0.0.0.0

The members of MonoMac.Foundation.NSAttributedString are listed below.

See Also: NSObject

Public Constructors

Default constructor that initializes a new instance of this class with no parameters.
A constructor that initializes the object from the data stored in the unarchiver object.
Constructor to call on derived classes when the derived class has an [Export] constructor.
A constructor used when creating managed representations of unmanaged objects; Called by the runtime.
Creates an NSAttributedString with user defined attributes specified in a dictionary.

Public Properties

[read-only]
static
AttachmentAttributeNameNSString.
[read-only]
static
BackgroundColorAttributeNameNSString.
[read-only]
static
BaselineOffsetAttributeNameNSString.
[read-only]
override
ClassHandleIntPtr. The handle for this class.
[read-only]
static
CursorAttributeNameNSString.
[read-only]
static
ExpansionAttributeNameNSString.
[read-only]
static
FontAttributeNameNSString.
[read-only]
static
ForegroundColorAttributeNameNSString.
[read-only]
static
KernAttributeNameNSString.
[read-only]
LengthInt32.
[read-only]
static
LigatureAttributeNameNSString.
[read-only]
static
LinkAttributeNameNSString.
[read-only]
static
MarkedClauseSegmentAttributeNameNSString.
[read-only]
static
ObliquenessAttributeNameNSString.
[read-only]
static
ParagraphStyleAttributeNameNSString.
[read-only]
static
ShadowAttributeNameNSString.
[read-only]
SizeSizeF.
[read-only]
static
StrikethroughColorAttributeNameNSString.
[read-only]
static
StrikethroughStyleAttributeNameNSString.
[read-only]
static
StrokeColorAttributeNameNSString.
[read-only]
static
StrokeWidthAttributeNameNSString.
[read-only]
static
SuperscriptAttributeNameNSString.
[read-only]
static
ToolTipAttributeNameNSString.
[read-only]
static
UnderlineColorAttributeNameNSString.
[read-only]
static
UnderlineStyleAttributeNameNSString.
[read-only]
ValueString.
[read-only]
static
VerticalGlyphFormAttributeNameNSString.
[read-only]
static
WritingDirectionAttributeNameNSString.

Public Methods