Search
 
SCRIPT & CODE EXAMPLE
 

SWIFT

uitextview placeholder uikit ios

class ViewController: UIViewController {

    @IBOutlet weak var textView: UITextView!
    let placeholder = "Example: I started my career as a street wear model based in Maryland. After 3 years of working with some of the top companies there, I moved to LA, where I currently reside. I’ve been featured in shows, 12 magazines, commercials, and a number of music videos. Now, Im currently looking to continue working with clothing companies and campaigns."
    let start = NSRange(location: 0, length: 0)
    override func viewDidLoad() {
        super.viewDidLoad()
        textView.delegate = self
        textView.text = placeholder
    }
}

extension ViewController: UITextViewDelegate {
    func textViewDidChangeSelection(_ textView: UITextView) {
        // Moves cursor to start when tapped on textView with placeholder
        if textView.text == placeholder {
            textView.selectedRange = start
        }
    }
    func textViewDidChange(_ textView: UITextView) {
        // Manages state of text when changed
        if textView.text.isEmpty {
            textView.text = placeholder
            textView.textColor = .lightGray
        } else if textView.text != placeholder {
            textView.textColor = .black
        }
    }
    func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
        // Called when you're trying to enter a character (to replace the placeholder)
        if textView.text == placeholder {
            textView.text = ""
        }
        return true
    }
}
Comment

PREVIOUS NEXT
Code Example
Swift :: activity indicator swiftui 
Swift :: PDF Preview in Swift iOS 
Swift :: how to remove item from list swift 
Swift :: swift make condition that you are in sumulator 
Swift :: swift 5 get app version 
Swift :: swift close view 
Swift :: remove all add TapGestureRecognizer swift 
Swift :: simple alert swifti 
Swift :: swift change navigation bar title 
Swift :: create button with icon swift 
Swift :: Swift Switch Statement with Range 
Swift :: swift fit label to text 
Swift :: swift split string into array of 2 characters 
Swift :: swift replace newlines with space 
Swift :: get last element of array swift 
Swift :: convert uiimage to data swift 
Swift :: Swift How to declare an optional in Swift? 
Swift :: Swift Static Properties 
Swift :: xcode how to get aspect ratio of device 
Swift :: how to multiply numbers in array swift 
Swift :: command compileswift failed with a nonzero exit code 
Swift :: Swift Optional Binding (if-let 
Swift :: Swift Operator Associativity 
Swift :: swiftui profile picture 
Swift :: view rounded corners swift 
Swift :: AndroidManifest.xml:5: Error: Class referenced in the manifest flutter build 
Swift :: Swift print() with terminator 
Swift :: UICollectionviewcontroller reload data 
Ruby :: rails api only with postgress and rspec 
Ruby :: remove ruby 
ADD CONTENT
Topic
Content
Source link
Name
4+3 =