iOS Guides | Samples

MonoTouch.UIKit.UITextView Class

A UIControl that displays a scrollable multi-line text editor.

See Also: UITextView


[MonoTouch.Foundation.Register("UITextView", true)]
public class UITextView : UIScrollView, IUIContentSizeCategoryAdjusting, IUITextDraggable, IUITextDroppable, IDisposable


The UITextView is a scrollable, multi-line text view that can display styled text and can be editable.


If UITextView.Editable is true, the text view will be editable by the application user. When the application user taps on the view, it becomes the first responder and displays the system keyboard. It is the application developer's responsibility to ensure that the keyboard does not obscure functionality (e.g., by scrolling or rearranging views). To make the keyboard disappear, the application developer must have the UITextView resign first responder status (by calling UIResponder.ResignFirstResponder).

Application developers can use Notifications to calculate the necessary scrolling or rearranging of views associated with the appearance and disappearance of the keyboard.

Since the UITextView is multiline, unlike the UITextField, the keyboard's return key cannot be replaced by a done key and there is no equivalent to the UITextField.ShouldReturn property. A common idiom for a UITextView is to "end editing when the user touches anywhere outside the text field." This can be done by overriding UIResponder.TouchesBegan in the containing UIView and calling UIView.EndEditing, as shown in the following example:

C# Example

public class MyView : UIView
	UITextView textView;

	public MyView()
		textView = new UITextView(new RectangleF(10, 44, UIScreen.MainScreen.Bounds.Width - 20, 300)){
			Editable = true

	public override void TouchesBegan(NSSet touches, UIEvent evt)

Text Kit

iOS 7 introduced "Text Kit," a broad set of APIs and modifications of existing classes, built on MonoTouch.CoreText, that greatly expands the typographical flexibility of iOS.

UITextViews are intended to display large amounts of text. The text is stored in NSTextStorage objects and layout of the text is managed by a NSLayoutManager, which lays out the text in an area defined by NSTextContainer objects.

Mapped to the Model-View-Controller pattern, the NSLayoutManager is the Controller, the UITextView is the View, and NSTextStorage and NSTextContainers are Model elements.

NSTextStorage is a subclass of NSMutableAttributedString and is responsible for holding the string, with various text styles. NSTextContainer objects are responsible for modeling the geometric layout of the page. The NSLayoutManager translates characters in the NSTextStorage into glyphs, lays those out in lines according to the constraints of the NSTextContainers, and coordinates the display of one or more UITextView objects.

The following example shows the basic use of two important Text Kit features: multiple text styles and exclusion paths:

C# Example

	var size = UIScreen.MainScreen.Bounds.Size;

	var atts = new UIStringAttributes();
	atts.ForegroundColor = UIColor.Blue;
	var txt = "\nText Kit.\n Lorem ipsum dolor ...  auctor.";
	var attributedString = new NSMutableAttributedString(txt, atts);
	attributedString.AddAttribute(UIStringAttributeKey.ForegroundColor, UIColor.Red, new NSRange(0, 10));
	attributedString.AddAttribute(UIStringAttributeKey.Font, UIFont.PreferredFontForTextStyle(UIFontTextStyle.Headline), new NSRange(0, 10));

	//NSTextStorage == MVC Model (partial)
	var storage = new NSTextStorage();

	//NSLayoutManager == MVC Controller
	var layoutManager = new NSLayoutManager();
	//NSTextContainer defines a logical block (page, column)
	var container = new NSTextContainer(size);

	//UITextView == MVC View
	TextView = new UITextView(new RectangleF(new PointF(0, 0), size), container);
	TextView.AttributedText = attributedString;
	TextView.ScrollEnabled = false;
	TextView.Editable = false;
	TextView.UserInteractionEnabled = false;

	//Add an image overlay, with exclusion path..
	var logoRect = new RectangleF(96, 195, 95, 90);
	var img = UIImage.FromBundle("xam.png");
	var imgView = new UIImageView(logoRect);
	imgView.Image = img;

	//Note exclusion path in container coordinate system...
	var xRect = TextView.ConvertRectFromView(logoRect, this);
	var hexPath = HexPath(xRect);
	container.ExclusionPaths = new UIBezierPath[] { hexPath };

iOS 6 added multiple text-style capability to UITextView. To use multiple styles, application developers must use the UITextView.AttributedText property. The UITextView.Font, UITextView.TextColor, and UITextView.TextAlignment properties apply to all the text in the UITextView.

The MonoTouch API supports two styles of event notification: the Objective-C style that uses a delegate class or the C# style using event notifications.

The C# style allows the user to add or remove event handlers at runtime by assigning to the events of properties of this class. Event handlers can be anyone of a method, an anonymous methods or a lambda expression. Using the C# style events or properties will override any manual settings to the Objective-C Delegate or WeakDelegate settings.

The Objective-C style requires the user to create a new class derived from UITextViewDelegate class and assign it to the UIKit.Delegate property. Alternatively, for low-level control, by creating a class derived from NSObject which has every entry point properly decorated with an [Export] attribute. The instance of this object can then be assigned to the UITextView.WeakDelegate property.

Related content


Namespace: MonoTouch.UIKit
Assembly: monotouch (in monotouch.dll)
Assembly Versions:

The members of MonoTouch.UIKit.UITextView are listed below.

See Also: UIScrollView

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 to skip initialization and merely allocate the object.
Initializes the UITextView with the specified frame.
A constructor used when creating managed representations of unmanaged objects; Called by the runtime.
Instantiates a new UITextView with the specified frame and textContainer.

Public Properties

AllowsEditingTextAttributesBoolean. Whether the application user can change the style of the text.
AppearanceUITextView+UITextViewAppearance. Strongly-typed property that returns the UIAppearance class for this class.
AttributedTextNSAttributedString. Contents of the text view as an attributed string.
AutocapitalizationTypeUITextAutocapitalizationType. How input is converted to all capitalization.
AutocorrectionTypeUITextAutocorrectionType. A property that controls when words will be automatically corrected.
BeginningOfDocumentUITextPosition. This property holds the text position for the beginning of the document. Read-only.
ClassHandleIntPtr. The handle for this class.
ClearsOnInsertionBoolean. Whether inserting new text erases the current contents.
CurrentInputModeDidChangeNotificationNSString. Notification constant for CurrentInputModeDidChange
DataDetectorTypesUIDataDetectorType. This property holds a value that determines what text in the document can be converted into clickable links.
DelegateUITextViewDelegate. An instance of the MonoTouch.UIKit.UITextViewDelegate model class which acts as the class delegate.
EditableBoolean. This property determines if the text view is editable or not.
EnablesReturnKeyAutomaticallyBoolean. If true, then the return key on the keyboard will be disabled when there is no text in the text view.
EndOfDocumentUITextPosition. This property holds the text position for the end of the document. Read-only.
FontUIFont. This property holds the font that will be used to display the text.
HasTextBoolean. A value representing if there is text in the view.
InputAccessoryViewUIView. A custom view to display when this control becomes the first responder.
InputDelegateUITextInputDelegate. A delegate object that can respond to text and selection changes.
InputViewUIView. A custom view to be used when the controll becomes the first responder.
KeyboardAppearanceUIKeyboardAppearance. The style of the keyboard that is used with the view.
KeyboardTypeUIKeyboardType. The type of keyboard to use with the view.
LayoutManagerNSLayoutManager. An NSLayoutManager to control the layout of the text.
MarkedTextRangeUITextRange. The range of marked text in the document. Read-only.
MarkedTextStyleNSDictionary. A NSDictionary of attributes describing style of the UITextField.MarkedTextRange.
ReturnKeyTypeUIReturnKeyType. The property is used to get or set the UIReturnKeyType of the "return" key. The "return" key will display different text depending on this property.
SecureTextEntryBoolean. This property controls if the text being displayed should be hidden.
SelectableBoolean. Whether the application user can select content and interact with links and text attachments.
SelectedRangeNSRange. This property returns the range of text that is selected.
SelectedTextRangeUITextRange. This property returns the range of text that is selected.
SelectionAffinityUITextStorageDirection. Sets a value that controls whether the cursor is displayed at the start of the last line or end of the second-to-last line of a multiline selection.
ShouldBeginEditingUITextViewCondition. Delegate invoked by the object to get a value.
ShouldChangeTextUITextViewChange. Delegate invoked by the object to get a value.
ShouldEndEditingUITextViewCondition. Delegate invoked by the object to get a value.
ShouldInteractWithTextAttachmentFunc<UITextView, NSTextAttachment, MonoTouch.Foundation.NSRange, bool>. Delegate invoked by the object to get a value.
ShouldInteractWithUrlFunc<UITextView, MonoTouch.Foundation.NSUrl, MonoTouch.Foundation.NSRange, bool>. Delegate invoked by the object to get a value.
ShouldScrollToTopUIScrollViewCondition. Delegate invoked by the object to get a value.
SpellCheckingTypeUITextSpellCheckingType. This property controls if spell checking will be enabled or disabled during input.
TextString. This property will set the text to display, or retrieve the text that is being displayed.
TextAlignmentUITextAlignment. How the UITextField.Text should be aligned.
TextBackgroundColorKeyNSString. The constant that should be used as the key value when retrieving the text background color from a NSDictionary.
TextColorUIColor. The color to use for all the text in the UITextView.
TextColorKeyNSString. The constant that should be used as the key value when retrieving the text color from a NSDictionary.
TextContainerNSTextContainer. Defines the area in which text should be displayed.
TextContainerInsetUIEdgeInsets. Defines the UITextView.TextContainer's layout area as insets from the UITextView's content area.
TextContentTypeNSString. A hint of the type of data the field should contain (must be value from UITextContentType).
TextDidBeginEditingNotificationNSString. Notification constant for TextDidBeginEditing
TextDidChangeNotificationNSString. Notification constant for TextDidChange
TextDidEndEditingNotificationNSString. Notification constant for TextDidEndEditing
TextFontKeyNSString. The property holds the key that should be used to retrieve the value of the font for the text from a NSDictionary.
TextInputViewUIView. This the view that provides the coordinate system. Read-only.
TextStorageNSTextStorage. Holds the attribute text being displayed.
TokenizerUITextInputTokenizer. This property provides information on the tokenizer that would be used to break up the text into units such as characters, words, lines, and paragraphs.
TypingAttributesNSDictionary. The attributes to be applied to newly-typed text.
ViewForZoomingInScrollViewUIScrollViewGetZoomView. Delegate invoked by the object to get a value.
WeakDelegateNSObject. An object that can respond to the delegate protocol for this type
WeakInputDelegateNSObject. An object that can respond to the delegate protocol for inserted text.
WeakLinkTextAttributesNSDictionary. Returns weak link text attributes.
WeakTokenizerNSObject. Returns weak tokenizer.

Public Methods

AppearanceWhenContainedIn(params Type[]) : UITextView+UITextViewAppearance
Returns a strongly typed UIAppearance for instances of this class when the view is hosted in the specified hierarchy.
Begins displaying the floating cursor at the specified point.
ComparePosition(UITextPosition, UITextPosition) : NSComparisonResult
Returns a value that shows how one location in a document that compares to another location: before, identical, or after.
Deletes one character backwards from the cursor.
This method is called once dictation has unsuccessfully completed.
This method is called when the there is a pending dictation.
Ends display of the floating cursor.
GetAppearance(UITraitCollection) : UITextView+UITextViewAppearance
Returns an appearance proxy for the specified traits.
GetAppearance(UITraitCollection, params Type[]) : UITextView+UITextViewAppearance
Returns an appearance proxy for the specified traits.
GetAppearance<T>() : UITextView+UITextViewAppearance
Obtains the appearance proxy UITextViewAppearance for the subclass of UITextView.
GetAppearance<T>(UITraitCollection) : UITextView+UITextViewAppearance
Obtains the appearance proxy UITextViewAppearance for the subclass of UITextView.
GetAppearance<T>(UITraitCollection, params Type[]) : UITextView+UITextViewAppearance
Obtains the appearance proxy UITextViewAppearance for the subclass of UITextView that has the specified trait collection when the view is hosted in the specified hierarchy.
GetBaseWritingDirection(UITextPosition, UITextStorageDirection) : UITextWritingDirection
Determines the writing direction from a position within the text.
GetCaretRectForPosition(UITextPosition) : RectangleF
This method will return a rectangle that can be used for drawing the insertion caret.
GetCharacterOffsetOfPosition(UITextPosition, UITextRange) : Int32
Returns the number of characters in some text between position and the start of the range.
GetCharacterRange(UITextPosition, UITextLayoutDirection) : UITextRange
Returns a range that represents the distance from byExtendingPosition to as far as is possible in direction.
GetCharacterRangeAtPoint(PointF) : UITextRange
Returns the character or characters at the point specified.
GetClosestPositionToPoint(PointF) : UITextPosition
Returns the position that is closest to the point specified within the control.
GetClosestPositionToPoint(PointF, UITextRange) : UITextPosition
Returns the position in withinRange that is closest to withinRange.
GetFirstRectForRange(UITextRange) : RectangleF
Returns a rectangle that will enclose the first line of text in the range (if the range specifies multiple lines of text).
GetFrameForDictationResultPlaceholder(NSObject) : RectangleF
When overridden, customizes the placement of the dictation placeholder.
GetOffsetFromPosition(UITextPosition, UITextPosition) : Int32
Returns the number of characters between two visible locations in a document.
GetPosition(UITextPosition, Int32) : UITextPosition
Returns a second position that is offset from the specified position.
GetPosition(UITextRange, Int32) : UITextPosition
Returns a position within a document relative to the start of withinRange.
GetPosition(UITextPosition, UITextLayoutDirection, Int32) : UITextPosition
Returns a second position in a document the direction and offset relative to the first position.
GetPositionWithinRange(UITextRange, UITextLayoutDirection) : UITextPosition
Returns the furthest position possible in the visible text.
GetSelectionRects(UITextRange) : UITextSelectionRect[]
The selection rectangles for the range.
GetTextRange(UITextPosition, UITextPosition) : UITextRange
Returns the text range between two positions in a document.
GetTextStyling(UITextPosition, UITextStorageDirection) : NSDictionary
Returns a NSDictionary of styling properties at the position and in the direction specified.
Used to select a UIDictationPhrase from a collection of possible phrases.
InsertDictationResultPlaceholder() : NSObject
The dictation-results placeholder that will be used during dictation processing.
This method will insert text into the view at the position indicated by the cursor.
RemoveDictationResultPlaceholder(NSObject, Boolean)
Informs the UITextView that the placeholder is no longer needed.
ReplaceText(UITextRange, String)
Replaces text in the specified range with the text provided.
Scrolls the text view until the range is visible.
SetBaseWritingDirectionforRange(UITextWritingDirection, UITextRange)
Specifies the base writing direction.
SetMarkedText(String, NSRange)
This method will replace any existing marked text the specified text and then selects it.
ShouldChangeTextInRange(UITextRange, String) : Boolean
Called prior to an edit. Can be overridden to selectively filter edits.
TextInRange(UITextRange) : String
A substring of text in the range.
Any currently marked text is unmarked.
Moves the floating curor to the specified point.

Protected Methods

Releases the resources used by the UITextView object.

Public Events

ChangedEvent raised by the object.
DecelerationEndedEvent raised by the object.
DecelerationStartedEvent raised by the object.
DidZoomEvent raised by the object.
DraggingEndedEvent raised by the object.
DraggingStartedEvent raised by the object.
EndedRaised when editing has finished in this UITextView.
ScrollAnimationEndedEvent raised by the object.
ScrolledEvent raised by the object.
ScrolledToTopEvent raised by the object.
SelectionChangedEvent raised by the object.
StartedRaised when editing has started on this UITextView.
WillEndDraggingEvent raised by the object.
ZoomingEndedEvent raised by the object.
ZoomingStartedEvent raised by the object.