Want to change UITextview height while typing - swift

I want that if user add text and it add new line it should increase height and if user delete soometext and some line than it should decrease its height
For this I'have tried below solution
func textViewDidChange(_ textView: UITextView) {
self.updateCharacterCount()
DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(4),
execute: {
let pos = textView.endOfDocument
self.referenceOfClass?.textOfTextViewForTitle =
self.title_Name.text
let currentRect = textView.caretRect(for: pos ??
UITextPosition.init())
if (currentRect.origin.y == -1 || currentRect.origin.y ==
CGFloat.infinity){
if let val = self.indexPath{
self.referenceOfClass?.previousRectforTitle = currentRect
self.referenceOfClass?.inq_Tableview.reloadRows(at:
[val], with: .automatic)
}
}
else if currentRect.origin.y <
self.referenceOfClass?.previousRectforTitle.origin.y ?? 0.0 {
if let val = self.indexPath{
self.referenceOfClass?.previousRectforTitle = currentRect
self.referenceOfClass?.inq_Tableview.reloadRows(at:
[val], with: .automatic)
}
}
})
}
but it is not working in this textView.caretRect(for: pos ??
UITextPosition.init()) always return same value (0,-1)
So how can I solve this?
I am using xcode10.1 and this textview is in tableviewcell

You can try this,
Take Outlet of hight constraint and code below
#IBOutlet weak var txtMessageHeightConstraint: NSLayoutConstraint!
extension GigMessageSocketViewController:UITextViewDelegate
{
func textViewDidChange(_ textView: UITextView)
{
isClickOnSendMessage = true
let contentSize = txtMessage.sizeThatFits(CGSize(width: txtMessage.bounds.size.width, height: CGFloat(Float.greatestFiniteMagnitude)))
if textView.contentSize.height < 100.0
{
txtMessageHeightConstraint.constant = CGFloat(ceilf(Float(contentSize.height)))
textView.layoutIfNeeded()
textView.updateConstraints()
textView.scrollRangeToVisible(textView.selectedRange)
}
}
}

My first thought on how to solve this issue would be to create a custom UITextfield that it would be a UIView with content mode scaleToFill that inside a UILabel and you would make every component of the UITextfield clear
self.textColor = UIColor.clear
self.tintColor = UIColor.clear
self.borderStyle = UITextBorderStyle.none
self.placeholder=""
and on the function
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {}
You would update the UILabel text so the scaling would be automated because of the contraints.

Related

How can we get the whole text from selected text in UITextView?

How can I get the whole text in the blue rectangle, inside UITextView, if user select a portion like this:
My current code is to get only the selected text:
if let range = textView.selectedTextRange {
let selectedText = textView.text(in: range) {
//selectedText
}
}
You have to set your view controller as the text view delegate UITextViewDelegate
override func viewDidLoad() {
super.viewDidLoad()
textView.delegate = self
}
Implement textViewDidChangeSelection method
func textViewDidChangeSelection(_ textView: UITextView)
In this function you can get the UITextPosition of the start and end of the lines of the selection, then create a new range and retrieve the selected text:
func textViewDidChangeSelection(_ textView: UITextView) {
guard let selectedTextRange = textView.selectedTextRange,
let start = textView.tokenizer
.position(from: selectedTextRange.start, toBoundary: .line, inDirection: .backward),
let end = textView.tokenizer
.position(from: selectedTextRange.end, toBoundary: .line, inDirection: .forward),
let range = textView.textRange(from: start, to: end),
let text = textView.text(in: range)
else { return }
print(text)
}
Add this extension helpers to your project:
extension UITextDirection {
static let forward: Self = .init(rawValue: 0)
static let backward: Self = .init(rawValue: 1)
}

Custom Attributed string action in swift

I want to make the attributed string action in swift. I want append my custom variable to table view cell data.
I added colour and line also for that string, now i want to add the link for that added attributed string.
Please find my code with the following.
I add this code into cell for row at Index path.
let value = NSMutableAttributedString(string: " tap here.", attributes:[NSAttributedStringKey.link: URL(string: "http://www.google.com")!]).withTextColor(UIColor.disSatifyclr).withUnderlineColor(UIColor.disSatifyclr).withUnderlineStyle(.styleSingle)
if (cell.desclbl.text?.contains("more about donating, "))! {
let description = descriptions[indexPath.section][indexPath.row]
let attributedString = NSMutableAttributedString(string: description)
attributedString.append(value)
cell.desclbl.attributedText = attributedString
}
Here you can handle action using UITextView instead of UILabel on tap of string
class ViewController: UIViewController, UITextViewDelegate {
#IBOutlet weak var textView: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
textView.attributedText = prepareLink()
textView.tintColor = UIColor.black
textView.isSelectable = true
textView.isEditable = false
textView.delegate = self
}
func prepareLink() -> NSMutableAttributedString {
let formattedString = NSMutableAttributedString(string: " tap here ")
let linkAttribute = [
NSAttributedString.Key.foregroundColor: UIColor.green,
NSAttributedString.Key.underlineColor: UIColor.green,
NSAttributedString.Key.underlineStyle: 1,
NSAttributedString.Key.link: URL(string: "http://www.google.com")!
] as [NSAttributedString.Key : Any]
formattedString.addAttributes(linkAttribute, range: NSMakeRange(0, formattedString.length))
return formattedString
}
func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
if (URL.absoluteString == "http://www.google.com") {
// Do Something
}
return true
}
}
Note: If you have to continue with UILabel then you have to implement UITapGesture to handle action.

Switching to next UITextField when character limit is reached

I have three text fields to enter in a phone number. I am trying to set the character limit for each textfield to three characters and once this is reached switch to a new textfield.
I saw online to use this code to limit the characters:
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let currentCharacterCount = textField.text?.characters.count ?? 0
if (range.length + range.location > currentCharacterCount){
return false
}
let newLength = currentCharacterCount + string.characters.count - range.length
return newLength <= 25
}
and to use this to switch to typing on a new textfield:
.didbecomefirstresponder()
but I am not sure how to limit a text field to 3 characters and then switch to the next field.
This is my code, how I solve this:
The three textfields are:
#IBOutlet var txtField1: UITextField!
#IBOutlet var txtField2: UITextField!
#IBOutlet var txtField3: UITextField!
import UITextFieldDelegate in to your class and set the delegate to self for all the textfields.
And use this method to change focus.
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let currentCharacterCount = ((textField.text?.characters.count)! + string.characters.count) - 1
switch (textField, currentCharacterCount) {
case (self.txtField1, 3):
self.txtField2.becomeFirstResponder()
case (self.txtField2, 7):
self.txtField3.becomeFirstResponder()
default:
break
}
return true
}
Here I set character count 3 for the first textfield, and 7 for second textfield.
You could use the delegate method for UITextField shouldChangeCharactersInRange. You'd have to do a little setup work though. Here's an example that creates 3 textFields, conforms to the UITextFieldDelegate, and does something similar to what you're describing.
class ViewController: UIViewController, UITextFieldDelegate {
var max = 3
var fields:[UITextField] = []
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
// This is just an example to layout 3 text fields.
for i in 0 ..< 3 {
let field = UITextField()
view.addSubview(field)
field.delegate = self
field.borderStyle = .roundedRect
field.font = UIFont(name: "AvenirNext-Regular", size: 15.0)
field.textColor = UIColor.black
field.frame = CGRect(x: view.bounds.width/2 - 100, y: 100 + (150*CGFloat(i)), width: 200, height: 50)
fields.append(field)
}
}
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
// Make sure the field has non-nil text
if let text = textField.text {
// Check if the text length has reached max limit
if text.characters.count > (max-1) {
// Get index of field from fields array
if let index = fields.index(of: textField) {
// Make sure it's not the last field in the array else return false
if index < fields.count-1 {
// Make the next field in the array become first responder if it's text is non-nil and it's text's character count is less than desired max
if fields[index+1].text != nil && fields[index+1].text!.characters.count < (max-1) {
fields[index+1].becomeFirstResponder()
} else {
return false
}
} else {
return false
}
}
}
}
return true
}
}
I find Pierce's answers too complex and anas.p's answer doesn't work for me like it's supposed to. My Swift 5 solution:
override func viewDidLoad() {
textField1.addTarget(self, action: #selector(textFieldDidChange1), for:.editingChanged)
textField2.addTarget(self, action: #selector(textFieldDidChange2), for:.editingChanged)
}
#objc func textFieldDidChange1() {
// After 2 characters are typed, immediately jump to textField2
if (textField1.text?.count == 2) {
self.textField2.becomeFirstResponder()
}
}
#objc func textFieldDidChange2() {
if (textField2.text?.count == 2) {
self.textField3.becomeFirstResponder()
}
}

Avoid copying code for a lot of buttons

Edit on my question:
Grimxn, I made a subclass and can see it works, because of the borderWidth and color. But I still have a couple of questions on how to add my function:
Should I code "func textField(textField: UITextField" or "func textField(textField: MyCustomTextField" ?
What should I do with "if textField == numberField01 {" ?
How do I 'call this' from the ViewController code ?
class ViewController: UIViewController, UITextFieldDelegate {
#IBOutlet weak var numberField01: UITextField!
#IBOutlet weak var numberField02: MyCustomTextField!
override func viewDidLoad() {
super.viewDidLoad()
numberField01.delegate = self
numberField01.keyboardType = UIKeyboardType.NumberPad
numberField02.delegate = self
numberField02.keyboardType = UIKeyboardType.NumberPad
}
class MyCustomTextField: UITextField {
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.layer.borderColor = UIColor.redColor().CGColor
self.layer.borderWidth = 1.5
func textField(textField: UITextField,
shouldChangeCharactersInRange range: NSRange,
replacementString string: String)
-> Bool {
var result = true
var prospectiveText = (textField.text as NSString).stringByReplacingCharactersInRange(range, withString: string)
prospectiveText = prospectiveText.stringByReplacingOccurrencesOfString(".", withString: "", options: NSStringCompareOptions.LiteralSearch, range: nil)
if textField == numberField01 {
if count(string)>0 {
let disallowedCharacterSet = NSCharacterSet(charactersInString: "0123456789").invertedSet
let replacementStringIsLegal = string.rangeOfCharacterFromSet(disallowedCharacterSet) == nil
let resultingStringLengthIsLegal = count(prospectiveText) <= 4
let scanner = NSScanner(string: prospectiveText)
let resultingTextIsNumeric = scanner.scanDecimal(nil) && scanner.atEnd
result = replacementStringIsLegal && resultingStringLengthIsLegal && resultingTextIsNumeric
}
}
return result
}
}
}
Original question:
The following code is working fine for one textfield (numberField01). It makes sure the input is decimal only, places a decimal point, and prevents a user to paste in a non decimal string. But I have a lot more buttons... (numberField02 and up). How can I handle more buttons, without just copying my code for each button?
class ViewController: UIViewController, UITextFieldDelegate {
#IBOutlet weak var numberField01: UITextField!
#IBOutlet weak var numberField02: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
numberField01.delegate = self
numberField01.keyboardType = UIKeyboardType.NumberPad
numberField02.delegate = self
numberField02.keyboardType = UIKeyboardType.NumberPad
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
// Tap background to add decimal point and defocus keyboard
#IBAction func userTappedBackground(sender: AnyObject) {
for view in self.view.subviews as! [UIView] {
if let textField = view as? UITextField {
if count(numberField01.text) > 0 {
var numberString = numberField01.text
numberString = numberString.stringByReplacingOccurrencesOfString(".", withString: "", options: NSStringCompareOptions.LiteralSearch, range: nil)
var numberFromString = Double(numberString.toInt()!) / 100
numberField01.text = String(format:"%.2f", numberFromString)
}
textField.resignFirstResponder()
}
}
}
func textField(textField: UITextField,
shouldChangeCharactersInRange range: NSRange,
replacementString string: String)
-> Bool {
var result = true
var prospectiveText = (textField.text as NSString).stringByReplacingCharactersInRange(range, withString: string)
prospectiveText = prospectiveText.stringByReplacingOccurrencesOfString(".", withString: "", options: NSStringCompareOptions.LiteralSearch, range: nil)
if textField == numberField01 {
if count(string)>0 {
let disallowedCharacterSet = NSCharacterSet(charactersInString: "0123456789").invertedSet
let replacementStringIsLegal = string.rangeOfCharacterFromSet(disallowedCharacterSet) == nil
let resultingStringLengthIsLegal = count(prospectiveText) <= 4
let scanner = NSScanner(string: prospectiveText)
let resultingTextIsNumeric = scanner.scanDecimal(nil) && scanner.atEnd
result = replacementStringIsLegal && resultingStringLengthIsLegal && resultingTextIsNumeric
}
}
return result
}
}
So something similar to the following:
Button GetButtonCommonFeatures(Button myButton)
{
Write common code here....
e.g. myButton.delegate = self;...
return myButton;
}
Then call your method for each button. Lets take numberField01 for example. You will include the code in the method that applies to every button.
numberField01 = GetButtonCommonFeatures(numberField01);
Hope this helps

Digit input in a text field

Additional question:
Still need some help with my code. The textfield is 'measuredValue' and I plan to have 30 different texfields (measuredValue1...30). When I type '923' the text will be set to '9.23' right away. Then I want to add '4'... for '92.34' but that doesn't work. Thanks for helping out.
func textField(textField: UITextField,
shouldChangeCharactersInRange range: NSRange,
replacementString string: String)
-> Bool {
if count(string) == 0 { return true }
var measuredValue = (textField.text as NSString).stringByReplacingCharactersInRange(range, withString: string)
switch textField {
case digitsOnlyTextField:
if count(measuredValue) == 3 || count(measuredValue) == 4 {
let stringNumeric = Double(measuredValue.toInt()!) / 100
measuredValue = String(format:"%.2f", stringNumeric)
digitsOnlyTextField.text = measuredValue
}
return measuredValue.containsOnlyCharactersIn("0123456789") && count(measuredValue) <= 4
default:
return true
}
}
Original question:
I would like to validate my text fields to get the right input for my app. Input needs to be formatted like '9.90' or '15.34'. So always 3 or 4 digits, and always 2 decimals.
I would like to use 'numberpad keyboard' (just 0...9, no point) and add the decimal point after the user exits the field. So the user input is 990 or 1534, and then in the text field it will become 9.90 or 15.34 automatically.
I tried searching for examples first, but didn't find what I was looking for.
Any help appreciated.
Jan
You have to implement the UITextField delegate method
func textFieldDidEndEditing(textField: UITextField) {
//Logic goes here....
var textString = textField.text as! String
let textLength = countElements(textString)
if textLength >= 3 && textLength <= 4 {
let stringNumeric = Double(textString.toInt()!) / 100
let texts = String(format:"%.2f", stringNumeric)
textField.text = texts
}
}
Your class should confirm to the protocol UITextFieldDelegate
this is the 'final' code I used (but I'm open for improvements!). A few remarks:
when a user clicks the background (and leaves the textfield) the
decimal point gets set.
when a user copies & pastes text from another app, this code handles that.
when a user goes back in the textfield for editing, the decimal point gets set again.
Deleting a value (to an empty textfield) is handled correctly.
So I'm pleased. Thanks for the help.
import UIKit
class ViewController: UIViewController, UITextFieldDelegate {
#IBOutlet weak var numberField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
numberField.delegate = self
numberField.keyboardType = UIKeyboardType.NumberPad
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
// Tap background to add decimal point and defocus keyboard
#IBAction func userTappedBackground(sender: AnyObject) {
for view in self.view.subviews as! [UIView] {
if let textField = view as? UITextField {
if count(numberField.text) > 0 {
var numberString = numberField.text
numberString = numberString.stringByReplacingOccurrencesOfString(".", withString: "", options: NSStringCompareOptions.LiteralSearch, range: nil)
var numberFromString = Double(numberString.toInt()!) / 100
numberField.text = String(format:"%.2f", numberFromString)
}
textField.resignFirstResponder()
}
}
}
func textField(textField: UITextField,
shouldChangeCharactersInRange range: NSRange,
replacementString string: String)
-> Bool {
var result = true
var prospectiveText = (textField.text as NSString).stringByReplacingCharactersInRange(range, withString: string)
prospectiveText = prospectiveText.stringByReplacingOccurrencesOfString(".", withString: "", options: NSStringCompareOptions.LiteralSearch, range: nil)
if textField == numberField {
if count(string)>0 {
let disallowedCharacterSet = NSCharacterSet(charactersInString: "0123456789").invertedSet
let replacementStringIsLegal = string.rangeOfCharacterFromSet(disallowedCharacterSet) == nil
let resultingStringLengthIsLegal = count(prospectiveText) <= 4
let scanner = NSScanner(string: prospectiveText)
let resultingTextIsNumeric = scanner.scanDecimal(nil) && scanner.atEnd
result = replacementStringIsLegal && resultingStringLengthIsLegal && resultingTextIsNumeric
}
}
return result
}