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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T00:27:25+00:00 2026-05-12T00:27:25+00:00

I need to move text that the user has entered into a large multi-line

  • 0

I need to move text that the user has entered into a large multi-line UITextView into a smaller (but still multi-line) UITextView*. If the user has entered more text than will display in the smaller view, I want to truncate the text so that it fits with all the (truncated) text visible. (Neither the large UITextView nor the smaller one should scroll.)

What’s the best way to do this?

I can use a loop, shortening the string by a character each time, and then use NSString‘s sizeWithFont: constrainedToSize: lineBreakMode: to find out the height this shorter string would need, and then compare that against the height I have available in my smaller UITextView, ending the loop when the string will fit – but that seems slow and awkward. There must be a better way.

I’d like to just tell the destination UITextView to truncate its displayText member as it displays it on screen, but I’ve not been able to find a way to do that.

*More context on this, from a comment I made below:

I’ve got a landscape app. I change the layout of the view depending on the photo the user chooses. If it’s a landscape photo, the caption is smaller – just a line at the bottom of the photo. If she chooses a portrait photo, then there’s plenty of space I can use for the caption at the side of the photo, so the caption is bigger.

If the user changes her photo orientation from portrait to landscape, then I want to truncate the text and then allow her to edit it so that it makes sense. I could just zap it, but I’d prefer to preserve it to minimize her typing.

  • 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-05-12T00:27:25+00:00Added an answer on May 12, 2026 at 12:27 am

    I wrote the following recursive method and public API to do this properly. The ugly fudge factor is the subject of this question.

    #define kFudgeFactor 15.0
    #define kMaxFieldHeight 9999.0
    
    // recursive method called by the main API
    -(NSString*) sizeStringToFit:(NSString*)aString min:(int)aMin max:(int)aMax
    {
    if ((aMax-aMin) <= 1)
        {
        NSString* subString = [aString substringToIndex:aMin];
        return subString;
        }
    
    int mean = (aMin + aMax)/2; 
    NSString* subString = [aString substringToIndex:mean];
    
    CGSize tallerSize = CGSizeMake(self.frame.size.width-kFudgeFactor,kMaxFieldHeight);
    CGSize stringSize = [subString sizeWithFont:self.font constrainedToSize:tallerSize lineBreakMode:UILineBreakModeWordWrap];
    
    if (stringSize.height <= self.frame.size.height)
        return [self sizeStringToFit:aString min:mean max:aMax]; // too small
    else    
            return [self sizeStringToFit:aString min:aMin max:mean];// too big
    }
    
    -(NSString*)sizeStringToFit:(NSString*)aString
    {
    
    CGSize tallerSize = CGSizeMake(self.frame.size.width-kFudgeFactor,kMaxFieldHeight);
    CGSize stringSize = [aString sizeWithFont:self.font constrainedToSize:tallerSize lineBreakMode:UILineBreakModeWordWrap];
    
    // if it fits, just return
    if (stringSize.height < self.frame.size.height)
        return aString; 
    
    // too big - call the recursive method to size it       
    NSString* smallerString = [self sizeStringToFit:aString min:0 max:[aString length]];
    return smallerString;   
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to move data from a datareader into a Farpoint Spreadsheet component in
I need to move .NET code to the Compact Framework. That code uses HttpUtility.UrlEncode
I have a need to move an item in an IEnumerable<> up, that is
Since I need to send email by code, I'm currently using plan text but
I have a web page that has a few text boxes, and a dual
I need some help with parsing a line of text. The line of text
I need this WPF ComboBox as a dropdown where user cannot enter text like
We need to move off traditional FTP for security purposes (it transmits it's passwords
I need to move backwards through an array, so I have code like this:
I need to move a sprite only vertically on mouse move. How do I

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.