Limit number of characters in uitextview
You should be looking for an empty string instead, as the apple reference says
If the user presses the Delete key, the length of the range is 1 and an empty string object replaces that single character.
I think the check you actually want to make is something like [[textView text] length] - range.length + text.length > 140
, to account for cut/paste operations.
Setting maximum number of characters of `UITextView ` and `UITextField `
Update Swift 4.X
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
let newText = (textView.text as NSString).replacingCharacters(in: range, with: text)
let numberOfChars = newText.count
return numberOfChars < 10 // 10 Limit Value
}
Try this out:
func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {
let newText = (textView.text as NSString).stringByReplacingCharactersInRange(range, withString: text)
let numberOfChars = newText.characters.count // for Swift use count(newText)
return numberOfChars < 10;
}
How to limit characters in UITextView iOS
1) add UITextViewDelegate in your viewcontroller like this
@interface ViewController : UIViewController <UITextViewDelegate>
Each time a user types a character on the keyboard, just before the character is displayed,following method get called. This is a handy location to test the characters that a user is typing and disallow specific characters you want to restrict.
textView:shouldChangeCharactersInRange:replacementString
2) then write following code.
instead of 140 you can put whatever number you want.
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
return textView.text.length + (text.length - range.length) <= 140;
}
For more help take a look at UItextView
Swift 2.0 solution
func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {
return textView.text.characters.count + (text.characters.count - range.length) <= textViewLimit
}
UITextView characters remaining
try this
import UIKit
class ViewController: UITextViewDelegate {
let maxLenghth = 200
func textViewDidChange(_ textView: UITextView) {
countLabel.text = "\(maxLength - textView.text.count)"
}
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
return textView.text.count + (text.count - range.length) <= maxLength
}
}
Swift - Limit the number of characters in lines for UITextview
Implement shouldChangeTextIn and return false if any line has more than the allowed number of characters:
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
let maxAllowedCharactersPerLine = 10
let lines = (textView.text as NSString).replacingCharacters(in: range, with: text).components(separatedBy: .newlines)
for line in lines {
if line.characters.count > maxAllowedCharactersPerLine {
return false
}
}
return true
}
Limit the number of Characters for an IBOutlet TextView, Swift
Please have a look of below question
Set the maximum character length of a UITextField (In my answer you can also see for UITextView)
Instead of writing duplicate code everywhere, You can create an extension of UITextView
or UITextField
to set the max length
Update:
Comment Explanation:
First thing, You need to remove the UITextView
delegate method from your current view controller. Because extension of UITextView
is internally used UITextViewDelegate
Note: Delegate is one to one communication. In your case, if you implement delegate in the current view controller then an extension of UITextView
is not worked.
UITextView limit characters and display count left
If you look closely at the docs for UITextViewDelegate
, you will see that textViewDidChange
is defined like this:
optional func textViewDidChange(_ textView: UITextView)
Look at the parameter! It requires a parameter with an external parameter name of _
!
Look what you've written:
func textViewDidChange(textView: TextViewX)
Your parameter's external name is textView
!
This is why it does not work.
So, just add _
and a space before textView
and change the type of the parameter to UITextView
.
Related Topics
Urlresponse Is Not Retrieved After Storing in Cache Using Storecachedresponse
Wkwebview Causes My View Controller to Leak
Swift: Failed to Assign Value to a Property of Protocol
Applicationwillterminate: Not Being Called
New Itunes Connect Interface -- Should It Immediately Be Seen on "Prerelease"
How to Effectively Process Uitouches in a Multitouch Sequence
How to Save Created PDF in Document Folder and Merge in iOS
How to Set Cornerradius for Only Bottom-Left,Bottom-Right and Top-Left Corner of a Uiview
How to Check If a Static Library Is Built for 64-Bit
Nsuserdefaults Losing Its Keys & Values When Phone Is Rebooted But Not Unlocked
Xctest/Xctest.H Not Found on Old Projects Built in Xcode 6
Cabasicanimation Rotate Returns to Original Position
Programmatically Fire Button Click Event
iOS Change Navigation Bar Title Font and Color
No Suitable Records Were Found Verify Your Bundle Identifier Is Correct
Add a Border Outside of a Uiview (Instead of Inside)