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

See Also: UITextView Members

Syntax

[Foundation.Register("UITextView", true)]
public class UITextView : UIScrollView, IUITextInput, IDisposable

Remarks

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

Editing

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 UIKit.UITextView resign first responder status (by calling UIResponder.ResignFirstResponder).

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

Since the UIKit.UITextView is multiline, unlike the UIKit.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 UIKit.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 UIKit.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
		};
		AddSubview(textView);
	}

	public override void TouchesBegan(NSSet touches, UIEvent evt)
	{
		EndEditing(true);
	}
}
          

Text Kit

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

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

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

UIKit.NSTextStorage is a subclass of Foundation.NSMutableAttributedString and is responsible for holding the string, with various text styles. UIKit.NSTextContainer objects are responsible for modeling the geometric layout of the page. The UIKit.NSLayoutManager translates characters in the UIKit.NSTextStorage into glyphs, lays those out in lines according to the constraints of the UIKit.NSTextContainers, and coordinates the display of one or more UIKit.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.BeginEditing();
	attributedString.AddAttribute(UIStringAttributeKey.ForegroundColor, UIColor.Red, new NSRange(0, 10));
	attributedString.AddAttribute(UIStringAttributeKey.Font, UIFont.PreferredFontForTextStyle(UIFontTextStyle.Headline), new NSRange(0, 10));
	attributedString.EndEditing();

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

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

	//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;
	AddSubview(TextView);

	//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;
	AddSubview(imgView);

	//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 UIKit.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 UIKit.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 UIKit.UITextViewDelegate class and assign it to the UIKit.Delegate property. Alternatively, for low-level control, by creating a class derived from Foundation.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

Requirements

Namespace: UIKit
Assembly: Xamarin.iOS (in Xamarin.iOS.dll)
Assembly Versions: 0.0.0.0