I'm having some problems trying to set the numbers of the characters of 2 different UITextView
s to 12 (for example).
It works OK with only one UITextView
but it becomes hard with both. There are actually some errors while writing text in them. It doesn't type anything now. I have been trying different ways to get it done but everything seems wrong in the shouldChangeTextInRange
method.
How do I set the number of characters of both textViews properly?
class viewControllerCuatro: UIViewController, UITextViewDelegate {
@IBOutlet weak var textViewInteriorUno: UITextView!
@IBOutlet weak var textViewInteriorDos: UITextView!
@IBOutlet weak var textViewExteriorUno: UITextView!
@IBOutlet weak var textViewExteriorDos: UITextView!
@IBOutlet weak var foto1: UIImageView!
@IBOutlet weak var foto2: UIImageView!
@IBOutlet weak var imagenFondo: UIImageView!
@IBOutlet weak var scrollView: UIScrollView!
override func viewDidLoad() {
textViewExteriorUno.delegate = self
textViewExteriorDos.delegate = self
textViewInteriorUno.delegate = self
textViewInteriorDos.delegate = self
textViewExteriorUno.layer.masksToBounds = true
textViewExteriorUno.layer.borderWidth = 6
textViewExteriorUno.layer.cornerRadius = textViewExteriorUno.layer.frame.height/6
textViewExteriorUno.layer.borderColor = UIColor.purpleColor().colorWithAlphaComponent(0.2).CGColor
textViewExteriorDos.layer.masksToBounds = true
textViewExteriorDos.layer.borderWidth = 6
textViewExteriorDos.layer.cornerRadius = textViewExteriorDos.layer.frame.height/6
textViewExteriorDos.layer.borderColor = UIColor.purpleColor().colorWithAlphaComponent(0.2).CGColor
foto1.layer.masksToBounds = true
foto1.layer.borderWidth = 6
foto1.layer.cornerRadius = foto1.layer.frame.height/6
foto1.layer.borderColor = UIColor.purpleColor().colorWithAlphaComponent(0.2).CGColor
foto2.layer.masksToBounds = true
foto2.layer.borderWidth = 6
foto2.layer.cornerRadius = foto2.layer.frame.height/6
foto2.layer.borderColor = UIColor.purpleColor().colorWithAlphaComponent(0.2).CGColor
foto1.image = fotoUnoEscogida
foto2.image = fotoDosEscogida
imagenFondo.image = UIImage (named: tipoDeHojaElegida)
}
override func viewDidAppear(animated: Bool) {
let alertNotSuccessRegister = UIAlertController(title: "¡Casi hemos terminado!", message: "Modifica los textos a tu gusto para que sea lo más personal posible.", preferredStyle: .ActionSheet)
alertNotSuccessRegister.addAction(UIAlertAction(title: "Ok", style: .Default, handler: nil))
self.presentViewController(alertNotSuccessRegister, animated: true, completion: nil)
}
@IBAction func botonSiguiente(sender: AnyObject) {
if textViewInteriorUno.text != " " || textViewInteriorDos.text != " " {
textoPersonalUno = textViewInteriorUno.text!
textoPersonalDos = textViewInteriorDos.text!
performSegueWithIdentifier("hey3", sender: self)
print("Se acaba de guardar el texto \(textoPersonalDos)")
} else {
let alertaError = UIAlertController(title: "Por favor, rellena los campos de texto.", message: "Dedica unas bonitas palabras :D", preferredStyle: .ActionSheet)
alertaError.addAction(UIAlertAction(title: "Ok", style: .Default, handler: nil))
self.presentViewController(alertaError, animated: true, completion: nil)
}
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
if textViewInteriorUno.text == "" || textViewInteriorUno.text.isEmpty == true || textViewInteriorDos.text == "" || textViewInteriorDos.text.isEmpty == true {
textViewInteriorUno.text = " "
textViewInteriorUno.resignFirstResponder()
textViewInteriorDos.text = " "
textViewInteriorDos.resignFirstResponder()
}
else{
self.textViewInteriorUno.resignFirstResponder()
self.textViewInteriorDos.resignFirstResponder()
}
}
func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool{
let newLength = textViewInteriorUno.text.utf16.count + text.utf16.count - range.length
let otherLength = textViewInteriorDos.text.utf16.count + text.utf16.count - range.length
if (newLength <= 12) && (otherLength > 6) {
return true
} else {
return false
}
}
The delegate method is told which text view's delegate is being called. So, look at the textView argument in the method and then return based on that. For example:
func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool{
var shouldReplace = true // For most cases, return true, only modify if we have a special case
let newLength = textView.text.utf16.count + text.utf16.count - range.length
// If the textView is 'textViewInteriorUno'
if textView.isEqual(textViewInteriorUno
{
shouldReplace = newLength <= 12 // will be true if the length is less than or equal to 12
}
// If the textView is 'textViewInteriorDos'
else if textView.isEqual(textViewInteriorDos)
{
shouldReplace = newLength < 6 // Will be true if the length is less then 6
}
return shouldReplace
}
This way, you base your delegate's response on what the textView is, and you don't modify the behavior of other views.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.