iOS 7 - UITextView size font to fit all text into view (no scroll)

I am trying to find a non-deprecated method to size the font of a textview down so that all text fits in the textview without requiring scrolling.

The method 'sizeWithFont' is deprecated and I want to ensure best practices, and XCode says to use 'boundingRectWithSize' but not sure how to use this to size a font down so that all text fits.

Any suggestions? And NO I can not use a UILabel instead. I need to have the text vertically aligned at the top and UILabel does not do this.

This worked Pre-iOS 7:

CGFloat fontSize;
CGFloat minSize;
if([deviceType isEqualToString:@"iPad"] || [deviceType isEqualToString:@"iPad Simulator"]){
    fontSize = 40;
    minSize = 15;
}
else{
    fontSize = 18;
    minSize = 8;
}
while (fontSize > minSize)
{
    CGSize size = [quote sizeWithFont:[UIFont fontWithName:@"Interstate" size:fontSize] constrainedToSize:CGSizeMake(newView.frame.size.width, 10000)];

    if (size.height <= newView.frame.size.height) break;

    fontSize -= 1.0;
}
Solution 1

Your problem can be solved by simply replacing sizeWithFont: constrainedToSize: with :

boundingRectWithSize:CGSizeMake(newView.frame.size.width, FLT_MAX)
                options:NSStringDrawingUsesLineFragmentOrigin
             attributes:@{NSFontAttributeName:[UIFont fontWithName:@"Interstate" size:fontSize]}
                context:nil];
Solution 2

The sizeThatFits method can be used to address this problem like this:

while (fontSize > minSize &&  [newView sizeThatFits:(CGSizeMake(newView.frame.size.width, FLT_MAX))].height >= newView.frame.size.height ) {
    fontSize -= 1.0;
    newView.font = [tv.font fontWithSize:fontSize];
}

I hope one of these solutions solve your problem. Cheers!

iOS 7 - UITextView size font to fit all text into view (no scroll), I am trying to find a non-deprecated method to size the font of a textview down so that all text fits in the textview without requiring scrolling. The method� Hello all, I have a fixed size UITextView say width and height of 150,150. The purpose is to display the thought of the day. Please note the size need to remain constant and I cant change it. Now The length of the thought string varies with respect to thought. What I want to do is change the

I had to do the same but then with programmatically added AutoLayout constraints (NSLayoutConstraint). Because of the constraints the contentSize wasn't correct for the most time causing the UITextView to scroll :/ To still get the correct font size I ended up just creating a new UITextView for the sake of doing some good ol' trial and error testing and getting it through there.

Pretty simple but like with everything you just gotta come up with it :)

NSInteger fontSize = 200;

UITextView *testTextView = [[UITextView alloc] init];
testTextView.text = self.myRealTextView.text;
testTextView.font = [UIFont fontWithName:@"AldotheApache" size:fontSize];
while ([testTextView sizeThatFits:(CGSizeMake(self.myRealTextView.frame.size.width, FLT_MAX))].height >= self.myRealTextView.frame.size.height ) {
    fontSize -= 0.5;
    testTextView.font = [UIFont fontWithName:@"AldotheApache" size:fontSize];
}

NSLog(@"Correct font size is: %ld",(long)fontSize);

self.myRealTextView.font = [UIFont fontWithName:@"AldotheApache" size:fontSize];

iOS 9, iOS 9 - UITextView shrink font size to fill text view without scrolling? - ios. Still curious what the proper way to adjust font size to fit text to UITextView would be though. title and subtitle text in a Subtitle-style table cell) all show up at the same size. java-7 � subdomain � windows-xp � akeneo � smartcontracts � android -gps Here we will learn iOS UI textview in swift with example and how to use ios UI textview control for multiline of text with example in Xcode.. iOS UI TextView. In iOS UI textview control is used to handle multiple lines of text or a large amount of text. iOS textview will enable scroll automatically when the content exceeds its default height or width and it allows users to style the content

Try UITextView's sizeThatFits. You can probably use it the following way:

//Set  text content of UITextView
//...

while (fontSize > minSize) {

   // Set font size of UITextView

   CGSize size = [textView sizeThatFits:(CGSizeMake(textView.frame.size.width, FLT_MAX)];

   if (size.height <= newView.frame.size.height) break;

   fontSize -= 1.0;

}

ios7, ios - How to Insert the UITextView into UIAlertview in iOS7 - Stack Ov ios objective-c ios7 ios7 - iOS 7.1 UITextView still not scrolling to cursor/caret after ne ios ios7 ios7 - iOS 7 - UITextView size font to fit all text into view (no scro. This example demonstrates how do I change the font size of TextView in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.

UITextView is subclass of UIScrollView -> so you can check the contentSize of the scrollable area after you set your text or font into textView.

textView.text = quote;
do
{
    textView.font = [UIFont fontWithName:@"Interstate" size:fontSize];
    [textView layoutIfNeeded];
    if (textView.contentSize.height <= newView.frame.size.height) {
        break;
    }
    fontSize -= 1.0;
} while (fontSize >= minSize);

That should work... Probably it would work even without [textView layoutIfNeeded].

Pass on the part which doesn't fit…, The UIlabel has multiply lines and a fixed font size. Now my question is if not all of the text fits into the UILabel, how can I pass this Asked 7 months ago by wouyld probably to replace the label by a TextView, so that user can scroll content. where the label is truncated and select the substring to display in next view. First of all, I wish to thank you for the tremendous help you have offered me, a novice iOS developer. OK, I now have a new problem to solve. My app has a bunch of text views and the font sizes are too small. But there are so many text views that it'd take me forever to set the font size on all of them. So I want to set the size programmatically.

Swift 5+

    var fontSize = Font.Size.section.rawValue
    let minSize = Font.Size.large.rawValue

    while fontSize > minSize {
        let textViewSize = CGSize(width: textView.frame.size.width, height: textView.frame.size.height)
        let size = textView.sizeThatFits(textViewSize)

        if size.height <= textViewSize.height {
            break
        }

        fontSize -= 1.0
    }

    textView.font = Font.primary.of(size: fontSize)

Text Kit Tutorial: Getting Started, In this tutorial, you'll learn how to use Text Kit in your iOS app to layout If you select a really small font size, your table view ends up looking a little sparse. and the table rows now change dynamically to fit the text size as shown in the Next, open NoteEditorViewController.swift, remove the UITextView� Thus, if you want a given view to size itself to its parent view, you should add it to the parent view before calling this method. You should not override this method. If you want to change the default sizing information for your view, override the size That Fits(_:) instead. That method performs any needed calculations and returns them to this

Getting to Know TextKit � objc.io, iOS 7's new interface replaces a lot of icons and bezels with plain-text buttons. All UILabel s, UITextField s, and UITextView s were using web views in the and a simple, scrollable, editable component for larger amounts of text: UITextView . and allow almost no customization beyond setting a font and a text color. iOS� Next, we’ll look into Auto-Sizing UITextViews. Auto-Size UITextView. UITextViews size can be changed depending on the content size of it. Let’s create another ViewController in our storyboard and connect it via a Button segue. Add the Button to the current ViewController and set the constraints.

Uitextview adjust font size to fit, If you do not implement this method, the collection view uses the values in its itemSize With scrolling enabled, the frame of the UITextView is independent of its This is all I got for today. uitextview shrink text to fit (5) I've been looking for that it can fit text on more lines // so that we do not end iOS7 - Adjusting font size of� Provide an instance of the TextView widget and one of the text types, such as TextViewCompat.AUTO_SIZE_TEXT_TYPE_NONE or TextViewCompat.AUTO_SIZE_TEXT_TYPE_UNIFORM. To define the default setting in XML through the support library, use the app namespace and set the autoSizeTextType attribute to none or uniform.

Swift label wrap text, TRUE makes it break lines if text exceeds the widget's size. Xcode 7 iOS 9 Swift 2 Tutorial 2: Changing a label's text and using the textfield In this With a width of 100 points but no constraint on the height, a text view might wrap a wrapping x axis labels in a chart I have tried all sorts of things to achieve this, to no avail. I am having a hard time getting the text color of my UITextView to change without it changing the color of attributed text. It works fine if I set TextColor after setting AttributedText, but it loses the color attributed I added to some of the words. It works fine on iOS 6 with this code: Editable = true; TextColor = UIColor.White;

Comments
  • Awesome! Solution 1 didn't work for me, it actually made the font extremely large. But solution 2 did the trick! Thanks!!
  • what is newView here
  • Those, who cant got what is newView here - check bicycle answer below
  • I love you, nice Solution :)
  • This somewhat works. Doesn't work well for typing individual characters for me...