0

I'm working with a UITextView and I want to make it so that once the user has filled the UITextView (you make it in storyboard, and these dimensions the user is not allowed to type outside of) the user cannot type anymore text. Basically, whats happening now is even if it looks like it's filled and I keep typing its like a never-ending text box which you can't see. What I assume is the dimensions you make it in storyboard is the only space you see text in.

Can someone help me?

http://www.prntscr.com/671n1u

Lyndsey Scott
  • 37,080
  • 10
  • 92
  • 128
wogwog
  • 99
  • 1
  • 11
  • Well , i am not sure , what are you upto excastly , i guess , you want to write in textfield upto certain character (the portan thats visible ). What you can actually do is , Refer to this link , http://stackoverflow.com/questions/433337/set-the-maximum-character-length-of-a-uitextfield – Saket Kumar Feb 19 '15 at 10:20

1 Answers1

3

You can use the UITextViewDelegate shouldChangeTextInRange: method to limit the text entry to the height of the text view:

func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {
    // Combine the new text with the old
    let combinedText = (textView.text as NSString).stringByReplacingCharactersInRange(range, withString: text)

    // Create attributed version of the text
    let attributedText = NSMutableAttributedString(string: combinedText)
    attributedText.addAttribute(NSFontAttributeName, value: textView.font, range: NSMakeRange(0, attributedText.length))

    // Get the padding of the text container
    let padding = textView.textContainer.lineFragmentPadding

    // Create a bounding rect size by subtracting the padding
    // from both sides and allowing for unlimited length 
    let boundingSize = CGSizeMake(textView.frame.size.width - padding * 2, CGFloat.max)

    // Get the bounding rect of the attributed text in the
    // given frame
    let boundingRect = attributedText.boundingRectWithSize(boundingSize, options: NSStringDrawingOptions.UsesLineFragmentOrigin, context: nil)

    // Compare the boundingRect plus the top and bottom padding
    // to the text view height; if the new bounding height would be
    // less than or equal to the text view height, append the text
    if (boundingRect.size.height + padding * 2 <= textView.frame.size.height){
        return true
    }
    else {
        return false
    }
}
Lyndsey Scott
  • 37,080
  • 10
  • 92
  • 128
  • I've changed my code and implemented that function and it doesn't seem like anything has changed. I then added the UITextViewDelegate to my class and when I run it I crash with this error: http://pastebin.com/3s8v648C - it just seems to not work. This crashes when I go to that specific VC by the way. This is my code though for that VC: http://pastebin.com/7FjYzRnW – wogwog Feb 20 '15 at 07:18
  • @wogwog I promise you this code will work... I tested it myself... You need to add the UITextViewDelegate and set the UITextView's delegate to self. Could you post the code showing how you tried to add the delegate? As your current code stands, since there's nothing in your viewDidLoad, nothing in this view controller can be causing a crash as soon as you transition to the view... – Lyndsey Scott Feb 20 '15 at 07:26
  • Ok I added in viewDidLoad : myTextView.delegate = self - ran it and it's still crashing. At the moment my class code is: class ThirdViewController: UIViewController, UITextViewDelegate { – wogwog Feb 20 '15 at 07:34
  • @wogwog Yeah, I suspect you've made a mistake with your interface's outlets: http://stackoverflow.com/a/13793334/2274694 – Lyndsey Scott Feb 20 '15 at 07:35
  • So how about I just delete my textView and place another one, wouldn't that just create a whole new one and resolve the errors? – wogwog Feb 20 '15 at 07:41
  • I got it to work! Thanks heaps! Would you happen to know whether I can make it an option that allows the user to choose whether they want bolld/italic etc or even change font? – wogwog Feb 20 '15 at 07:57
  • @wogwog You'd have to code that sort of functionality separately. – Lyndsey Scott Feb 20 '15 at 07:59