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 166667
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T12:08:17+00:00 2026-05-11T12:08:17+00:00

I am trying to limit the text input into a UITextView in cocoa-touch. I

  • 0

I am trying to limit the text input into a UITextView in cocoa-touch. I really want to limit the amount of rows rather than the number of characters. So far I have this to count the amount of rows:

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {     if([text isEqualToString:@'\n']) {         rows++;     }     NSLog(@'Rows: %i', rows);     return YES; } 

However this doesn’t work if the line is automatically wrapped rather than the user pressing the return key. Is there a way to check if the text was wrapped similar to checking for ‘\n’?

Thanks.

  • 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. 2026-05-11T12:08:17+00:00Added an answer on May 11, 2026 at 12:08 pm

    Unfortunately, using NSString -stringWithFont:forWidth:lineBreakMode: doesn’t work – which ever wrap mode you choose, the text wraps with a width that is less than the current width, and the height becomes 0 on any overflow lines. To get a real figure, fit the string into a frame that is taller than the one you need – then you’ll get a height that is greater than your actual height.

    Note my fudge in this (subtracting 15 from the width). This might be something to do with my views (I have one within another), so you might not need it.

    - (BOOL)textView:(UITextView *)aTextView shouldChangeTextInRange:(NSRange)aRange replacementText:(NSString*)aText {         NSString* newText = [aTextView.text stringByReplacingCharactersInRange:aRange withString:aText];          // TODO - find out why the size of the string is smaller than the actual width, so that you get extra, wrapped characters unless you take something off         CGSize tallerSize = CGSizeMake(aTextView.frame.size.width-15,aTextView.frame.size.height*2); // pretend there's more vertical space to get that extra line to check on         CGSize newSize = [newText sizeWithFont:aTextView.font constrainedToSize:tallerSize lineBreakMode:UILineBreakModeWordWrap];          if (newSize.height > aTextView.frame.size.height)             {             [myAppDelegate beep];             return NO;             }         else             return YES; } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to limit the amount of characters that a user can input. I
I'm trying to limit the punctuation that a user can enter into a text
I am trying to limit the amount of text/data is being shown from MySQL
I'm trying to limit the number of connections my server will accept using semaphores,
I'm trying to limit the number of records from an XML feed that are
I'm trying to limit inserting elements to the page: <script type=text/javascript> $(function() { var
I am trying to limit the number of lines each of the Mappers gets.
I'm adding text fields with a onclick. What I'm trying to do is limit
I'm currently trying to limit the memory resources of the process. Before to do
I am trying to limit connections and requests per IP then add a whitelist

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.