Using UITextViews as UITextFields

Matt

This is my first question.

I'm currently working on an iOS app in Swift. I was previously using three UITextFields for users to enter their data (it's a journaling app), but I hadn't realized that UITextField was limited to one line.

I've worked on getting three UITextViews to behave like UITextFields, except for one thing. I was using the code below to detect if the user pressed the return key so that they could begin typing in the next TextView (like the way that the return key behaves with multiple TextFields).

My one problem is that when the cursor moves down to the next TextView, it brings the new line ("\n") with it! I've tried many different ways to fix this and nothing seems to be working. Any ideas?

Thanks, Matt

func textView(textView: UITextView!, shouldChangeTextInRange: NSRange, replacementText: NSString!) {
    if(replacementText == "\n") {
        if(textView == firstTextView) 
        {
            firstTextView.resignFirstResponder()
            secondTextView.becomeFirstResponder()
        }

        if(textView == secondTextView)
        {
            secondTextView.resignFirstResponder()
            thirdTextView.becomeFirstResponder()
        }

        if(textView == thirdTextView)
        {
            thirdTextView.resignFirstResponder()
        }

    }
}
rintaro

As far as I know, textView(_:shouldChangeTextInRange:replacementText:) must return Bool value:

optional func textView(_ textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool

And when you return false, it cancels the change of the text. So I think you should:

func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {
    if(replacementText == "\n") {

        // ...

        return false
    }
    return true
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Trouble Getting UITextViews to Adapt to content Using Auto-Layout while Embedded in UIScrollView

How to manage firstResponder with two UITextFields using UIDatePicker input

Naming or Tagging UITextViews in Swift?

UIScrollView vs TableView with UITextFields

Creating uitableview forms with uitextfields

Multiple UITextFields and textDidChangeNotification notification

Check UITextFields for text

Create a dynamic row of UITextfields

How to set validation in UITextFields

Adding uitextfields dynamically

Clearing UITextfields with UIButton

UITextFields keyboard not appearing in UIScrollview

Automatic placing of UITextviews under each other

UITextViews in a UITableView link detection bug in iOS 7

Iteratively creating UITextViews based on number of items in an array

Swift AutoLayout: UITextViews inside UIScrollView headache

Loop through all UITextViews with certain tag number

UITextViews hidden under keyboard in iOS 7

Shortcuts or autofill for contact info in UITextFields

How to check two optional UITextFields

How to distinguish UITextFields in delegate methods?

Change border width for multiple UITextFields

SKView integrated into UIViewController with UITextFields lagging

UITextFields not displaying in struct, how to fix?

How to navigate between UITextFields in UITableViewCell?

Use inputAccessoryView with multiple UITextFields and ViewControllers

Adding new UITextFields by users in Swift

How to duplicate and retrieve data from an UIView with many UITextviews in it

How can you search a UITextFields in UIScrollView