TextView
public class TextView : ScrollView
A scrollable, multiline text region. Wrapper of UITextView
UITextView supports the display of text using custom style information and also supports text editing. You typically use a text view to display multiple lines of text, such as when displaying the body of a large text document. This class supports multiple text styles through use of the attributedText property. (Styled text is not supported in versions of iOS earlier than iOS 6.) Setting a value for this property causes the text view to use the style information provided in the attributed string. You can still use the font, textColor, and textAlignment properties to set style attributes, but those properties apply to all of the text in the text view. It’s recommended that you use a text view—and not a UIWebView object—to display both plain and rich text in your app.
TextView(text: "Hello, World!")
.height(100)
.font(size: 20)
.font(style: .bold)
.margin(edge: .top, value: 96)
.backgroundColor(.yellow)
.cornerRadius(10)
.didEndEditing { text in
print("\(String(describing: text))")
}
.shouldEndEditing { text -> Bool in
print("\(String(describing: text))")
return true
}
.didChangeText { text in
print("\(String(describing: text))")
}.gesture(gesture: getstur)
.onTapGesture {
print("on tap")
}.onLongPressGesture(numberOfTaps: 1, numberOfTouches: 3) {
print("on long press")
}
-
Create a new TextView with specified text string.
Declaration
Swift
public init(text: @autoclosure @escaping () -> String?)
Parameters
text
a string value.
-
Create a new TextView with specified text container.
Declaration
Swift
public init(textContainer: () -> NSTextContainer?)
Parameters
textContainer
a new text container.
-
The text that the text view displays. In iOS6 and later, assigning a new value to this property also replaces the value of the attributedText property with the same text, albeit without any inherent style attributes. Instead the text view styles the new string using the font, textColor, and other style-related properties of the class.
Declaration
Swift
@discardableResult public func text(_ value: @autoclosure @escaping () -> String?) -> Self
Parameters
value
a new string value.
Return Value
self
-
The font of the text
This property applies to the entire text string. The default value of this property is the body style of the system font.
Declaration
Swift
@discardableResult public func font(_ value: @autoclosure @escaping () -> UIFont?) -> Self
Parameters
value
a new font value
Return Value
self
-
The color of the text.
This property applies to the entire text string. The default text color is black. In iOS 6 and later, assigning a new value to this property causes the new text color to be applied to the entire contents of the text view. If you want to apply the color to only a portion of the text, you must create a new attributed string with the desired style information and assign it to the attributedText property.
Declaration
Swift
@discardableResult public func textColor(_ value: @autoclosure @escaping () -> UIColor?) -> Self
Parameters
value
a new color
Return Value
self
-
Change the color the text, the color is created by the rgba value.
Declaration
Swift
@discardableResult public func textColor(red r: @autoclosure @escaping () -> Int, green g: @autoclosure @escaping () -> Int, blue b: @autoclosure @escaping () -> Int, alpha a: @autoclosure @escaping () -> CGFloat = 1) -> Self
Parameters
r
the red value of the color object, data range from 0 to 255.
g
the green value of the color object, data range from 0 to 255.
b
the blue value of the color object, data range from 0 to 255.
a
the opacity value of the color object, data range from 0 to 1.
Return Value
description
-
Change the color of the text, the color is created by the hex and alpha.
Declaration
Swift
@discardableResult public func textColor(hex: @autoclosure @escaping () -> Int, alpha a: @autoclosure @escaping () -> Float = 1) -> Self
Parameters
hex
hex number
a
alpha value
Return Value
self
-
The technique for aligning the text. See:UITextView.textAlignment
This property applies to the entire text string. The default value of this property is NSTextAlignment.natural. Assigning a new value to this property causes the new text alignment to be applied to the entire contents of the text view. If you want to apply the alignment to only a portion of the text, you must create a new attributed string with the desired style information and assign it to the attributedText property.
public enum NSTextAlignment : Int { case left = 0 // Visually left aligned case center = 1 // Visually centered case right = 2 // Visually right aligned /* !TARGET_ABI_USES_IOS_VALUES */ // Visually right aligned // Visually centered case justified = 3 // Fully-justified. The last line in a paragraph is natural-aligned. case natural = 4 // Indicates the default alignment for script }
Declaration
Swift
@discardableResult public func textAlign(_ value: @autoclosure @escaping () -> NSTextAlignment) -> Self
Parameters
value
a new test alignment
Return Value
self
-
Set the font of the text, the font is created by the specified name and style.
public enum AKFontStyle{ case `default` case bold case italic case bolditalic }
Declaration
Swift
@discardableResult public func font(name: @autoclosure @escaping () -> String?, style: @autoclosure @escaping () -> AKFontStyle, size: @autoclosure @escaping () -> CGFloat) -> Self
Parameters
name
font name
style
font style
size
font size
Return Value
self
-
Change the font name of the text.
Declaration
Swift
@discardableResult public func font(name value: @autoclosure @escaping () -> String?) -> Self
Parameters
value
a new font name.
Return Value
self
-
Change the font size of the text.
Declaration
Swift
@discardableResult public func font(size value: @autoclosure @escaping () -> CGFloat) -> Self
Parameters
value
a new font size.
Return Value
self
-
Change the font style of the text.
public enum AKFontStyle{ case `default` case bold case italic case bolditalic }
Declaration
Swift
@discardableResult public func font(style value: @autoclosure @escaping () -> AKFontStyle) -> Self
Parameters
value
a new style
Return Value
self
-
A Boolean value that indicates whether the text view is editable.
See: UITextView.isEditable The default value of this property is true.
Declaration
Swift
@discardableResult public func isEditable(_ value: @autoclosure @escaping () -> Bool) -> Self
Parameters
value
a new Boolean value.
Return Value
self
-
A Boolean value that indicates whether the text view is selectable.
See: UITextView.isSelectable This property controls the ability of the user to select content and interact with URLs and text attachments. The default value is true.
Declaration
Swift
@discardableResult public func isSelectable(_ value: @autoclosure @escaping () -> Bool) -> Self
Parameters
value
a new Boolean value.
Return Value
self
-
A Boolean value that indicates whether the text view allows the user to edit style information.
See: UITextView.allowsEditingTextAttributes When set to true, the text view allows the user to change the basic styling of the currently selected text. The available style options are listed in the edit menu and only apply to the selection. The default value of this property is false.
Declaration
Swift
@discardableResult public func allowsEditingTextAttributes(_ value: @autoclosure @escaping () -> Bool) -> Self
Parameters
value
a new Boolean value
Return Value
self
-
The styled text that the text view displays.
See: UITextView.attributedText. Assigning a new value to this property also replaces the value of the text property with the same string data, albeit without any formatting information. In addition, the font, textColor, and textAlignment properties are updated to reflect the typing attributes of the text view.
Declaration
Swift
@discardableResult public func attributedText(_ value: @autoclosure @escaping () -> NSAttributedString) -> Self
Parameters
value
a new NSAttributedString value.
Return Value
self
-
A Boolean value that indicates whether inserting text replaces the previous contents.
The default value of this property is false. When the value of this property is true and the text view is in editing mode, the selection UI is hidden and inserting new text clears the contents of the text view and sets the value of this property back to false.
Declaration
Swift
@discardableResult public func clearsOnInsertion(_ value: @autoclosure @escaping () -> Bool) -> Self
Parameters
value
a new Boolean value.
Return Value
self
-
A Boolean value that determines the rendering scale of the text.
See: UITextView.usesStandardTextScaling When the value of this property is true, UIKit automatically adjusts the rendering of the text in the text view to match the standard text scaling. When using the standard text scaling, font sizes in the text view appear visually similar to how they would render in macOS and non-Apple platforms, and copying the contents of the text view to the pasteboard preserves the original font point sizes. This effectively changes the display size of the text without changing the actual font point size. For example, text using a 13-point font in iOS looks like text using a 13-point font in macOS. If your app is built with Mac Catalyst, or if your text view’s contents save to a document that a user can view in macOS or other platforms, set this property to true. The default value of this property is false.
Declaration
Swift
@available(iOS 13.0, *) @discardableResult public func usesStandardTextScaling(_ value: @autoclosure @escaping () -> Bool) -> Self
Parameters
value
a new Boolean value.
Return Value
self
-
The current selection range of the text view. See: UITextView.selectedRange
In iOS 2.2 and earlier, the length of the selection range is always 0, indicating that the selection is actually an insertion point. In iOS 3.0 and later, the length of the selection range may be non-zero.
Declaration
Swift
@discardableResult public func selectedRange(_ value: NSRange) -> Self
Parameters
value
a range value
Return Value
self
-
The types of data that convert to tappable URLs in the text view.
See: UITextView.dataDetectorTypes You can use this property to specify the types of data (phone numbers, http links, and so on) that should be automatically converted to URLs in the text view. When tapped, the text view opens the application responsible for handling the URL type and passes it the URL. Note that data detection does not occur if the text view’s isEditable property is set to true.
Declaration
Swift
@discardableResult public func dataDetectorTypes(detectorType value: () -> UIDataDetectorTypes) -> Self
Parameters
value
a new UIDataDetectorTypes value.
Return Value
self
-
The attributes to apply to new text that the user enters.
See: UITextView.typingAttributes This dictionary contains the attribute keys (and corresponding values) to apply to newly typed text. When the text view’s selection changes, the contents of the dictionary are cleared automatically.
Declaration
Swift
@discardableResult public func typingAttributes(_ value: [NSAttributedString.Key : Any]) -> Self
Parameters
value
new attributes
Return Value
self
-
Scrolls the text view until the text in the specified range is visible.
See: UITextView.scrollRangeToVisible
Declaration
Swift
@discardableResult public func scrollRangeToVisible(_ value: NSRange) -> Self
Parameters
value
The range of text to scroll into view.
Return Value
self
-
The custom input view to display when the text view becomes the first responder.
See: UITextView.inputView If the value in this property is nil, the text view displays the standard system keyboard when it becomes first responder. Assigning a custom view to this property causes that view to be presented instead. The default value of this property is nil.
Declaration
Swift
@discardableResult public func inputView(_ content: () -> View) -> Self
Parameters
content
the content of input view.
Return Value
self
-
The custom accessory view to display when the text view becomes the first responder.
See: UITextView.inputAccessoryView The default value of this property is nil. Assigning a view to this property causes that view to be displayed above the standard system keyboard (or above the custom input view if one is provided) when the text view becomes the first responder. For example, you could use this property to attach a custom toolbar to the keyboard.
Declaration
Swift
@discardableResult public func inputAccessoryView(_ content: () -> View) -> Self
Parameters
content
the content of inputAccessoryView.
Return Value
self
-
The inset of the text container’s layout area within the text view’s content area.
See: UITextView.textContainerInset. This property provides text margins for text laid out in the text view. By default the value of this property is (8, 0, 8, 0).
Declaration
Swift
@discardableResult public func textContainerInset(_ value: UIEdgeInsets) -> Self
Parameters
value
new edge insets.
Return Value
self
-
The attributes to apply to links.
See: UITextView.linkTextAttributes. The default attributes specify blue text with a single underline and the pointing hand cursor.
Declaration
Swift
@discardableResult public func linkTextAttributes(_ value: [NSAttributedString.Key : Any]!) -> Self
Parameters
value
new attributes.
Return Value
self
-
set the callback of which return value determine whether to begin editing in the specified text view.
Declaration
Swift
@discardableResult public func shouldBeginEditing(_ action: @escaping (_ text: String?) -> Bool) -> Self
Parameters
action
callback
Return Value
Self
-
Asks the callback whether to stop editing in the specified text view.
Declaration
Swift
@discardableResult public func shouldEndEditing(_ action: @escaping (_ text: String?) -> Bool) -> Self
Parameters
action
callback
Return Value
self
-
Tells the callback when editing begins in the specified text view.
Declaration
Swift
@discardableResult public func didBeginEditing(_ action: @escaping (_ text: String?) -> Void) -> Self
Parameters
action
callback
Return Value
self
-
Tells the callback when editing stops for the specified text view, and the reason it stopped.
Declaration
Swift
@discardableResult public func didEndEditing(_ action: @escaping (_ text: String?) -> Void) -> Self
Parameters
action
callback
Return Value
self
-
Asks the callback whether to change the text at the specified range.
Declaration
Swift
@discardableResult public func shouldChangeText(_ action: @escaping (_ text: String?, _ range: NSRange, _ replacementText: String) -> Bool) -> Self
Parameters
action
callback
Return Value
self
-
Tells the callback the text at the specified range had been changed.
Declaration
Swift
@discardableResult public func didChangeText(_ action: @escaping (_ text: String?) -> Void) -> Self
Parameters
action
callback
Return Value
self
-
Tells the callback when the text selection changes in the specified text view.
Declaration
Swift
@discardableResult public func didChangeSelection(_ action: @escaping (_ text: String?, _ selectedRange: NSRange) -> Void) -> Self
Parameters
action
callback
Return Value
self
-
Asks the callback whether the specified text view allows user interaction with the specified URL in the specified range of text.
See: UITextViewDelegate
Declaration
Swift
@available(iOS 10.0, *) @discardableResult public func shouldInteractWithURL(_ action: @escaping (_ text: String?, _ URL: URL, _ characterRange: NSRange, _ interaction: UITextItemInteraction) -> Bool) -> Self
Parameters
action
callback
Return Value
self
-
Asks the callback whether the specified text view allows user interaction with the provided text attachment in the specified range of text.
See: UITextViewDelegate
Declaration
Swift
@available(iOS 10.0, *) @discardableResult public func shouldInteractWithTextAttachment(_ action: @escaping (_ text: String?, _ textAttachment: NSTextAttachment, _ characterRange: NSRange, _ interaction: UITextItemInteraction) -> Bool) -> Self
Parameters
action
callback
Return Value
self