13

Is there any way to know the maximum size a font allowed.

I am doing

textInputTextView.font = [UIFont fontWithName:currentFontName size:doubleFontSize];
frame.size = textInputTextView.contentSize;

For different fonts I am getting wrong contentSize when doubleFontSize is more than 70. For some fonts I get wrong contentSize when doubleFontSize variable is only 40.

I am guessing textView.font = doubleSizeFont is too big for those particular fonts. All are ok when font size is small between 10 to 30.

Is there any way to know the maximum size allowed by a particular font?

Details:

I need an image from UITextView. I am using UIGraphicsGetImageFromCurrentImageContext image is producing ok. But I realized image quality is low. So I tried to make the TextView frame and font double.

When I Write in big font "I am a good boy" if good is the right most word. I get "I am a boy" the right most word good disappears I get some blank space on right side of the image. How tragic :)

Warif Akhand Rishi
  • 23,920
  • 8
  • 80
  • 107
  • 8
    Fonts are just a set of vector shapes, so there is no such thing as a maximum size. You could have an entire window made up of just one letter. Could you be more specific? What do you mean by "wrong" content size? What are you expecting? – borrrden May 22 '12 at 06:12
  • question edited Plz if you could help. – Warif Akhand Rishi May 22 '12 at 06:23
  • This edit doesn't give the info I asked for. What is happening that should not happen? Is the image cut off or something? – borrrden May 22 '12 at 06:42
  • How do you set up you image context? Have you looked at `UIGraphicsBeginImageContextWithOptions(CGSize size, BOOL opaque, CGFloat scale);`? Maybe your image context is just @1x on a retina screen? – David Rönnqvist May 22 '12 at 06:48
  • @borrrden Image is Ok. Text is cut off from right and bottom. I see white blank extra space on right and bottom. So image size is ok I guess. – Warif Akhand Rishi May 22 '12 at 06:48
  • What happens if you set it that way while your program is running? I don't think the text view will resize automatically...instead it will scroll. So if you take a picture, you will see only the top portion. – borrrden May 22 '12 at 06:53
  • @DavidRönnqvist I am setting the **textView Frame * 2** and **textView.text.size * 2** I don't want to use **scale = 2** for image quality resons. – Warif Akhand Rishi May 22 '12 at 06:55
  • Text size and frame size are *not* exactly equivalent, that is most likely your problem. Also are you sure other views aren't overlapping it? – borrrden May 22 '12 at 06:56
  • I wrote 10 lines in **ArialMT** font size **20** reached end of the **textView** textView did start Scrolling. top Lines I dont see. then I made the **textView.frame.size = contentSize** and took **BeginImageContext** with textView.frame. No problem. but more than 70 font size not all text are shown. – Warif Akhand Rishi May 22 '12 at 07:07
  • possible duplicate of [Measuring the pixel width of a string in Objective C](http://stackoverflow.com/questions/1435544/measuring-the-pixel-width-of-a-string-in-objective-c) – Mahmoud Al-Qudsi May 22 '12 at 07:14
  • a vote to close this question? how tragic? – Krishnabhadra May 22 '12 at 07:22
  • @Krishnabhadra tragic? It's a duplicate. There is no "maximum allowed size" but there is "how can I find out the maximum that will fit in a certain pixel width" which is an exact duplicate of the post I linked to. – Mahmoud Al-Qudsi May 22 '12 at 07:24
  • i have done same thing i m not getting any problem – Paresh Navadiya May 23 '12 at 13:06
  • After writing on text view in big fonts, you have doubled both font size and textView frame and save it as an image? Image Size is as the content size of the textView text? If so kindly give answer with code. I couldn't find any clue still now, working on other parts of my project. Thanks for your trying. – Warif Akhand Rishi May 24 '12 at 02:58
  • The answer is [here][1]. This answers your question in 2 lines of code. [1]: http://stackoverflow.com/questions/7241116/how-can-i-calculate-font-size-for-my-uilabel – virindh May 29 '12 at 00:31
  • Thank you for your comment. I was making both font size and textView frame size doubled. If this answer works I'm thinking of only double the textView frame and get the font size from your method. I'll keep you updated... – Warif Akhand Rishi May 29 '12 at 06:28

2 Answers2

2

You Can Calculate height/Width of the content by using the following code.Then resize your frame. Here my Width is fixed (290) and Calculating the height.

CGSize strSize= [DataToBeDisplayed sizeWithFont:[UIFont systemFontOfSize:17.0] constrainedToSize:CGSizeMake(290.0,99999.0)];
frame.size.height = strSize.height
Alladinian
  • 34,483
  • 6
  • 89
  • 91
muthukumar
  • 651
  • 1
  • 6
  • 19
1

I believe the issue that you are having is because you are using an UITextView. Text views inherits from the UIScrollView so it would not decrease the size of your font, it would only adapt its contents size and make your text scrollable.

Use an UITextField instead which also inherits from the UIView (thus can be used as an image) but allows for greater control. Fix its size to the maximum you would like it to be on screen and then working with both the [mylabel adjustsFontSizeToFitWidth:Y/N] and the [mylabel setminimumfontsize:somefontsize] methods, constrain it if too much text is entered. This will yield you a crisp big font and dynamically fit it upon input

~/End of Line

Im_Lp
  • 310
  • 3
  • 5