NSAttributedString Class
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Strings that can be annotated with a set of attributes.
[Foundation.Register("NSAttributedString", true)]
public class NSAttributedString : Foundation.NSObject, AppKit.INSPasteboardReading, AppKit.INSPasteboardWriting, Foundation.INSMutableCopying, Foundation.INSSecureCoding, IDisposable
type NSAttributedString = class
inherit NSObject
interface INSCoding
interface INativeObject
interface IDisposable
interface INSCopying
interface INSMutableCopying
interface INSSecureCoding
interface INSPasteboardReading
interface INSPasteboardWriting
- Inheritance
- Derived
- Attributes
- Implements
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, MonoTouch 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:
//
// 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 C:Foundation.NSAttributedString(string,UIKit.UIFont,UIKit.UIColor,UIKit.UIColor,UIKit.UIColor,UIKit.NSParagraphStyle,Foundation.NSLigatureType,float,Foundation.NSUnderlineStyle,UIKit.NSShadow,float,Foundation.NSUnderlineStyle constructor with C# named parameters for quickly creating attributed strings inline.
//
// 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.
//
// 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);
Constructors
Properties
Class | (Inherited from NSObject) |
ClassHandle |
The handle for this class. |
ContainsAttachments | |
DebugDescription |
A developer-meaningful description of this object. (Inherited from NSObject) |
Description |
Description of the object, the Objective-C version of ToString. (Inherited from NSObject) |
Handle |
Handle (pointer) to the unmanaged object representation. (Inherited from NSObject) |
IsDirectBinding | (Inherited from NSObject) |
IsProxy | (Inherited from NSObject) |
Length |
The number of characters in this string. |
LowLevelValue |
Low-level version of the Value property, returns a handle to the underlying Objective-C NSString. |
ReadableTypeIdentifiers | |
RetainCount |
Returns the current Objective-C retain count for the object. (Inherited from NSObject) |
Self | (Inherited from NSObject) |
Size | |
Superclass | (Inherited from NSObject) |
SuperHandle |
Handle used to represent the methods in the base class for this NSObject. (Inherited from NSObject) |
TextLayoutSectionOrientation | |
TextLayoutSectionRange | |
TextLayoutSectionsAttribute | |
UnderlineByWordMaskAttributeName | |
Value |
Contents of the object as a string. |
WritableTypeIdentifiers | |
WritableTypeIdentifiersForItemProvider | |
Zone | (Inherited from NSObject) |