SwiftUI - Is there a way to create a read-only TextEditor?

Ronnie

I am using a TextEditor in SwiftUI to display the contents of a long text.

I want this to be read-only but any time I tap on it, the keyboard pops up.

Is there a way to make the TextEditor read-only and do not see the keyboard when the user taps on it?

I have tried .disabled() but this is not good because it prevents the controller from scrolling and like I said, the controller contains a long text.

Text is not good because it does not scroll.

DonMag

Very basic example of scrolling text:

struct ContentView: View {
    @State var content = "Long text here..."

    var body: some View {
        ScrollView {
            VStack {
                Text(content)
                    .lineLimit(nil)
            }.frame(maxWidth: .infinity)
        }
    }
}

Here's an example with some long text:

import SwiftUI

struct ContentView: View {
    @State var content =
        """
    Label

    A label can contain an arbitrary amount of text, but UILabel may shrink, wrap, or truncate the text, depending on the size of the bounding rectangle and properties you set. You can control the font, text color, alignment, highlighting, and shadowing of the text in the label.

    Button

    You can set the title, image, and other appearance properties of a button. In addition, you can specify a different appearance for each button state.

    Segmented Control
    
    The segments can represent single or multiple selection, or a list of commands.
    
    Each segment can display text or an image, but not both.
    
    Text Field
    
    Displays a rounded rectangle that can contain editable text. When a user taps a text field, a keyboard appears; when a user taps Return in the keyboard, the keyboard disappears and the text field can handle the input in an application-specific way. UITextField supports overlay views to display additional information, such as a bookmarks icon. UITextField also provides a clear text control a user taps to erase the contents of the text field.

    Slider
    
    UISlider displays a horizontal bar, called a track, that represents a range of values. The current value is shown by the position of an indicator, or thumb. A user selects a value by sliding the thumb along the track. You can customize the appearance of both the track and the thumb.
    
    Switch
    
    Displays an element that shows the user the boolean state of a given value.  By tapping the control, the state can be toggled.

    Activity Indicator View
    
    Used to indicate processing for a task with unknown completion percentage.
    
    Progress View
    
    Shows that a lengthy task is underway, and indicates the percentage of the task that has been completed.

    Page Control
    
    UIPageControl indicates the number of open pages in an application by displaying a dot for each open page. The dot that corresponds to the currently viewed page is highlighted. UIPageControl supports navigation by sending the delegate an event when a user taps to the right or to the left of the currently highlighted dot.
    
    Stepper
    
    Often combined with a label or text field to show the value being incremented.

    Horizontal Stack View
    
    An UIStackView creates and manages the constraints necessary to create horizontal or vertical stacks of views. It will dynamically add and remove its constraints to react to views being removed or added to its stack. With customization it can also react and influence the layout around it.
    
    Vertical Stack View
    
    An UIStackView creates and manages the constraints necessary to create horizontal or vertical stacks of views. It will dynamically add and remove its constraints to react to views being removed or added to its stack. With customization it can also react and influence the layout around it.

    Table View
    
    Coordinates with a data source and delegate to display a scrollable list of rows. Each row in a table view is a UITableViewCell object.
    
    The rows can be grouped into sections, and the sections can optionally have headers and footers.
    
    The user can edit a table by inserting, deleting, and reordering table cells.
    
    Table View Cell
    
    Defines the attributes and behavior of cells in a table view. You can set a table cell's selected-state appearance, support editing functionality, display accessory views (such as a switch control), and specify background appearance and content indentation.

    Image View
    
    Shows an image, or series of images as an animation.
    
    Collection View
    
    Coordinates with a data source and delegate to display a scrollable collection of cells. Each cell in a collection view is a UICollectionViewCell object.
    
    Collection views support flow layout as well a custom layouts, and cells can be grouped into sections, and the sections and cells can optionally have supplementary views.
    """
    
    var body: some View {
        ScrollView {
            VStack {
                Text(content)
                    .lineLimit(nil)
            }.frame(maxWidth: .infinity)
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

SwiftUI - How to create TextField that only accepts numbers

How to add placeholder text to TextEditor in SwiftUI?

Head First C#: strange way to create a read-only property

Is this a correct way to create a read-only view of a numpy array?

Change background color of TextEditor in SwiftUI

How to prevent TextEditor from scrolling in SwiftUI?

Is there a way to create a new session of WKExtendedRuntimeSession in SwiftUI?

TextEditor positioning in SwiftUI

TextEditor added / SwiftUi

SwiftUI TextEditor: scrolling to top at text change

Transparent Background for TextEditor in SwiftUI

The best way to create a root filesystem, split between read only and writable

Is there a better way to create read-only attributes in rails?

Is there any way to create BottomBar using SwiftUI

Binding to a read-only property in SwiftUI

Changing TextEditor background color in SwiftUI for macOS

Is there a way to create a read-only date box in blazor without using two-way binding?

Show line numbers in macOS SwiftUI TextEditor

is there any way in mongodb to create "user" that have "read" access only to specific views of the db?

Whats is the best way to create a model of a user in swiftUI

Disable selection cursor for TextEditor in macOS-SwiftUI

SwiftUI TextEditor disable editing but keep tapable

Controlling size of TextEditor in SwiftUI

SwiftUI replace and copy text TextEditor

Updating a SwiftUI TextEditor on font change

Detecting keyboard "submit button" press for TextEditor SwiftUI

vscode - Is there a way to create an instance of `vscode.TextEditor`?

Connect TextEditor text count to ProgressView in SwiftUI

SwiftUI Expanding TextEditor