TextView - Tai-Kimura/SwiftJsonUI GitHub Wiki

TextView

class: SJUITextView inherites: UITextView

Platform Support

  • UIKit: Full support (All attributes available)
  • SwiftUI: Full support (Maps to TextEditor with equivalent functionality)
  • Jetpack Compose: Full support (Maps to TextField with multiline support)

Attributes for TextView

attribute name UIKit SwiftUI Compose XML type in json details remarks
text string Text content for textView
font string Font name for textView
fontSize float Font size for textView
fontColor string textView.textColor
hint string NSLocalizedString(hint, comment:"") will be set to placeholder
hintFont string Hint font name for textView Dynamic mode only
hintColor string Hint font color for hint
hideOnFocused boolean Hint will hide when true and focused Dynamic mode only
flexible boolean Auto-height adjustment between minHeight and maxHeight Dynamic mode only
containerInset array/string Text container insets. Array [top, left, bottom, right] or string "top|left|bottom|right" Dynamic mode only
returnKeyType string Return key type: Done, Next, Search, Send, Go, Route, Yahoo, Google Not supported in SwiftUI TextEditor
editable boolean Enable/disable editing
scrollEnabled boolean Enable/disable scrolling
minHeight float Minimum height for flexible mode
maxHeight float Maximum height for flexible mode
textAlign string Text alignment: Left, Right, Center
lineBreakMode string Line break mode
dataDetectorTypes string Data detector types for links, phone numbers, etc. UIKit only
allowsEditingTextAttributes boolean Allow editing text attributes UIKit only
keyboardType string Keyboard type
onTextChange string Text change event selector

Protocols for TextView

SJUITextViewDelegate

@objc public protocol SJUITextViewDelegate

Delegate protocol for SJUITextView with additional functionality beyond UITextViewDelegate.

func textViewDidChangeFrame(textView: SJUITextView)

Called when the text view's frame changes, particularly useful for flexible height text views.

Properties for TextView

Static Properties

open class var viewClass: SJUITextView.Type

This property will be used to decide which class to inflate with createFromJSON method. You should define the view's class on this property when you create classes inheriting from SJUITextView.

static var textColor: UIColor

Default text color for text views (default: black).

Instance Properties

public var maxHeight: CGFloat

Maximum height constraint for flexible text views (default: 0, meaning no limit).

public var minHeight: CGFloat

Minimum height constraint for flexible text views (default: 21.0).

public var flexible: Bool

Whether the text view's height adjusts automatically to content (default: false). When true, height changes between minHeight and maxHeight based on content.

public var placeHolder: SJUILabel!

Placeholder label displayed when the text view is empty. Managed automatically.

public var hasContainer: Bool

Whether the text view has a container view (default: false).

public var fontSize: CGFloat

Font size for the text view (default: 12.0).

public var hideOnFocused: Bool

Whether to hide the placeholder when the text view gains focus (default: true).

public var hintColor: UIColor

Color for the hint/placeholder text.

public var hintFont: String

Font name for the hint/placeholder text.

weak var sjuiDelegate: SJUITextViewDelegate?

Custom delegate for handling frame change events and other SJUITextView-specific callbacks.

Override Properties

override var text: String!

Text content with automatic placeholder management. Setting text updates placeholder visibility.

override var attributedText: NSAttributedString!

Attributed text content with automatic placeholder management.

var hint: String!

Hint/placeholder text with custom setter that manages placeholder label updates.

Functions for TextView

Initializers

override init(frame: CGRect, textContainer: NSTextContainer?)
required init?(coder aDecoder: NSCoder)

Custom initializers that set up placeholder functionality and notification observers.

Static Methods

public class func createFromJSON(attr: JSON, target: Any, views: inout [String: UIView]) -> SJUITextView

Factory method called when the text view is created from JSON file. Override this method when you create classes inheriting from SJUITextView class.

Instance Methods

func setPlaceHolderIfNeeded()

Updates placeholder visibility based on current text content and focus state. Automatically called when text changes or focus changes.

Override Methods

override func draw(_ rect: CGRect)

Custom drawing implementation that handles placeholder rendering and layout.

override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?)

Key-value observation handler for content size changes in flexible text views. Triggers frame adjustments and delegate callbacks.

Notification Methods

@objc func textChanged()

Notification handler called when text content changes. Updates placeholder visibility and triggers binding updates.

@objc func textBeginEditing()

Notification handler called when text editing begins (becomes first responder). Manages placeholder visibility based on hideOnFocused setting.

@objc func textEndEditing()

Notification handler called when text editing ends (resigns first responder). Restores placeholder if text is empty.

Automatic Features

SJUITextView provides several automatic features:

  • Flexible Height: Automatically adjusts height based on content when flexible is true
  • Placeholder Management: Shows/hides placeholder based on text content and focus
  • Content Size Observation: Monitors content size changes for layout updates
  • Notification Integration: Automatically registers for text change notifications
  • Delegate Callbacks: Calls sjuiDelegate methods for frame changes and other events