Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9182757
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T18:38:10+00:00 2026-06-17T18:38:10+00:00

For an iOS application I am writing I am using an UITextView, where the

  • 0

For an iOS application I am writing I am using an UITextView, where the user can insert a limited text.

To the textview there are 2 restrictions:

  1. Lines can be no longer than 30 characters
  2. There can be only 20 lines of text in the UITextView.

So in short, the maximum is 20 lines of 30 characters.

When a user is typing some text in the UITextView and the current sentence is 30 characters, I want it to automatically insert a new line \n (before the last word on that line) and force the last word and cursor to the line below.

When a user has 20 lines with 30 characters (or even simpler said: 20 lines, with 30 characters on the last line) I want the input to be blocked.

Now, most of this is fairly ‘simple’ but the code I have does not account for border cases, like inserting text in earlier lines.

I looked around the documentation from Apple, but I can not find a way to actually force this kind of Word-wrapping on a UITextView.

My try is to handle all this in the shouldChangeTextInRange delegate method (made the code a little more verbose, so it’s a bit easier to read).

#define MAX_LENGTH_LINE 30
#define MAX_LENGTH_ROWS 20

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    // Check for backspaces, they should always be allowed?
    if ([text length] == 0 && ![text isEqualToString:@"\n"])
        return YES;

    NSArray* lines = [textView.text componentsSeparatedByString:@"\n"];

    // Check if there are a maximum of lines and the last line is already maxed out
    NSString* lastLine = [lines objectAtIndex:[lines count] - 1];

    if (([lines count] == MAX_LENGTH_ROWS) &&
        (lastLine != nil) &&
        ([lastLine length] > MAX_LENGTH_LINE) &&
        ([text length] > 0))
        return NO;


    if ((lastLine != nil) &&
        ([lastLine length] > MAX_LENGTH_LINE))
    {
        NSRange range = [textView.text rangeOfString:@" " options:NSBackwardsSearch];
        NSRange breakRange = [textView.text rangeOfString:@"\n" options:NSBackwardsSearch];

        if (breakRange.location == NSNotFound)
            breakRange = NSMakeRange(0, 1);

        if (range.location == NSNotFound) {
            range = NSMakeRange(0, 1);
        }

        if (range.location > breakRange.location)
        {
            textView.text = [textView.text stringByReplacingCharactersInRange:NSMakeRange(range.location, 1) withString:@"\n"];
        }
        else
        {
            textView.text = [textView.text stringByAppendingString:@"\n"];
        }
    }

    if ([text isEqualToString:@"\n"])
    {
        if ([lines count] == MAX_LENGTH_ROWS)
            return NO;
        else {
            return YES;
        }
        NSRange range = NSMakeRange(textView.text.length - 1, 1);
        [textView scrollRangeToVisible:range];
    }

    return YES;
}

In the meanwhile I have been at this for a while and I lost it at the moment. Anyone who can give some pointers to just limit the UITextView to the 20 lines / 30 characters limitation I want?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T18:38:12+00:00Added an answer on June 17, 2026 at 6:38 pm

    After a bit of fiddling around, created a control which contains a UITextView as a subview.

    I let this control handle the text wrapping and forward the delegate methods to the view registered as a delegate.

    It might help others, so I am posting a link to BitBucket here.

    PS. It’s still very verbose, but that is to show how I solved this for my case.

    https://bitbucket.org/depl0y/sbframework/src/master/SBFramework/Views/SBTextView?at=master

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my iOS application, I'm showing date picker through which user can select at
I am writing an application using phonegap for iOS. I am trying to show
I am writing an AR application on iOS and am using CATransform3D transforms on
I am writing a iphone application using phonegap and ios. I have a weird
I'm writing an iOS application which needs to print using a Boca thermal printer.
We are using Coreplot to draw Charts in our IOS application, however we can't
I'm writing an iOS application using PhoneGap (aka Cordova), I have a simple html
I am writing a location based application for iOS 5. The application is using
I'm writing iOS application. There's a server related to some real estate. I've to
I'm nearly finished with writing my first iOS application, but I ran into a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.