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

Ask A Question

Stats

  • Questions 261k
  • Answers 261k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The Info.plist file mustn't be checked as a member of… May 13, 2026 at 11:40 am
  • Editorial Team
    Editorial Team added an answer If you really want to do this with JavaScript, I… May 13, 2026 at 11:40 am
  • Editorial Team
    Editorial Team added an answer Right click on your project, go to properties and there's… May 13, 2026 at 11:40 am

Related Questions

I know how to do this with pure PHP but I need to do
I'm making a system called NCIV in PHP. In that system you can change
With reference to this programming game I am currently building. Important: Scroll down to
When presenting preformatted text on the web (e.g. code samples), line wrapping can be

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.