Inserting (Appending) text on the top of a UITextView - Swift - ios

I have a UITextView where I insert text using:
MyUITextView.insertText(my_string_to_be_inserted)
it works fine but it appends text to the bottom of the UITextView.
Is there any way to add text on the top (like a stack) where the last added text will be always on the top?

You can try the snippet below.
if let position = textView.textRange(from: textView.beginningOfDocument, to: textView.beginningOfDocument) {
textView.replace(position, withText: "Your text goes here")
}
Refer the api here.

Related

Change letter color of tableview if found searchbar text

I have search bar and table view. I can filter table view items based on search bar text. But I have to change found letter's color. For example if I write HE to search bar, I want to show HELLO in table view but the color of HE should different than LLO.
Thanks for ideas.
use NSMutableAttributedString for your concept, Here I tell the logic customize yourself where you need (must call in inside the cellForRow)
lblcheck.text = "hello" // is the original text
lblcheck.textColor = UIColor.red // initally set the whole color for your text
//Create mutable string from original one
let attString = NSMutableAttributedString(string: lblcheck.text!)
//Fing range of the string you want to change colour
//If you need to change colour in more that one place just repeat it
let range: NSRange = (lblcheck.text! as NSString).range(of: "he", options: .caseInsensitive) //he in here use searchBar.text
attString.addAttribute(NSForegroundColorAttributeName, value: UIColor.white, range: range) // here set selection color what ever you typed
//Add it to the label - notice its not text property but it's attributeText
lblcheck.attributedText = attString
Text color change can be done by using NSAttributedString. When you show your search results, in your cellForRow delegate method, get search string and replace the word in your textView with NSAttributedString version. You can change color for the specific string in your NSAttributedString.
This is the general idea without seeing any code.

UITextField's width is never less than it's width with placeholder text

I have a UITextField and UILabel sitting together in a UIView as so:
and here it is in Xcode:
The label is hidden until the user enters some text into the text field, so it serves to provide a persistent "suffix" to the numeric entry. The problem is that when the user types a number into the text field, it doesn't shrink down to the size of the text, it remains at the size of the original placeholder, even though it isn't visible, as so:
Is there any way I can constrain the text field's width to be the minimum size to accommodate the user's text, and not pay attention to the invisible placeholder text's width?
Thank you
I managed to solve it myself:
Whenever the text is edited, the text field is checked to see if there is any text inside. If there is no text, the 'mg' suffix is hidden, and the placeholder is added. If there is text, the 'mg' suffix is shown and the placeholder is removed. Like so: (Swift)
func textFieldTextChanged(_ textField: UITextField) {
milligramTextField.invalidateIntrinsicContentSize()
view.layoutIfNeeded()
if textField.text == "" {
milligramSuffixLabel.text = ""
milligramTextField.placeholder = "0 mg"
} else {
milligramSuffixLabel.text = " mg"
milligramTextField.placeholder = ""
}
}
This answer was of great help.

Position cursor at desired location iOS Swift 3

I have custom UITextField which holds data (eg:) for username . I'm trying to place cursor at desired location if I enter wrong data and want to modify it. For eg: If I enter my username as "stcak" instead of "stack" , I need to place cursor at "a" in textField and delete "ca" and modify that with "ac" so i get desired text "stack".
Im not able to place my cursor at desired location. I had to delete entire text
Kindly help me how I can achieve this
Thanks
Use UITextPosion for set cursor in UItextField.
Below Sample code for that:
let newposition = textField.position(from: textField.beginningOfDocument, offset: index)
textField.selectedTextRange = textField.textRange(from: newposition!, to: newposition!)
Here index is Position of cursor in text Field.

How to programmatically enter text in UITextView at the current cursor position

I would like to add (some predefined) text at the current cursor position in a UITextView using Swift. So if I have a UITextField named txtField that has some text in it already such as "this is a beautiful valley" and I tap in the area between "a" and "beautiful" so that the cursor is now in the space between "a" and "beautiful" and then click a Button on my user interface a string of text such as "very" will get typed at the cursor position, so that the text in the UITextView will now become "this is a very beautiful valley". At the end of this operation (after button click event) I would the cursor to be just after the word "very". Many thanks for your help. I can see some question on the forum with similar themes, but the answers are in Objective C. I suicidal like help using Swift.
Try this... Inside your button's IBAction use this (please do not use forced optional unwrapping) or else if the textView has no selection or cursor, the app might crash:
let txt = "whatever you want"
if let range = handleToYourTextView.selectedTextRange {
// From your question I assume that you do not want to replace a selection, only insert some text where the cursor is.
if range.start == range.end {
handleToYourTextView.replaceRange(range, withText: txt)
}
}
try this
textView.replaceRange(textView.selectedTextRange!, withText: "your text")
update:
Swift 5.2
let txt = "whatever you want"
if let range = myTextView.selectedTextRange {
if range.start == range.end {
myTextView.replace(range, withText: txt)
}

UITextView top of the text is shown half when pressed on new line, rest is clipped

Check out the gif below, as you see the textview is normal. When I press the return button the top text gets clipped, when I enter a letter or press return again its fixed. After that the next return also clips the text. There is also unnecessary space created at the bottom.
Here is the code I use under UITextViewDelegate:
func textViewDidChange(textView: UITextView) {
textView.h = textView.contentHeight
contentTextContainer.h = textView.contentHeight + 20 //Unnecessary but..
}

Resources