Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 11-16-2010, 05:13 PM   #1 (permalink)
Registered Member
 
craig t mackenzie's Avatar
 
Join Date: Aug 2008
Location: London, UK
Age: 25
Posts: 53
craig t mackenzie is on a distinguished road
Send a message via AIM to craig t mackenzie
Question Scaling text to fit view

Hi there,

I'm having a bit of trouble working out the "best" way to render text in my application.

My main view consists of a text view, and the design of the application dictates a few things:
  • The (font) size of the text should be dynamic
  • The text frame should be centred vertically in the view
  • Hyphenation should be automatic and only when needed (avoided if possible)

At the moment i'm using a UILabel and the following code to try and guess the best font size to use for the amount of text:


Code:
txt  = @"this is just some sample text";
    
mylabel.font = [self getFontForString:txt];
mylabel.adjustsFontSizeToFitWidth = YES;
mylabel.numberOfLines = 0;
[mylabel setText:txt];
And:

Code:
    - (UIFont *) getFontForString:(NSString *)txt {
         CGFloat                textLength = txt.length;
         CGFloat                maxFontSize = 71;
         CGFloat                minFontSize = 27;
         CGFloat                newFontSize = 0;
    
         NSArray                *chunks = [txt componentsSeparatedByString:@" "];
         NSSortDescriptor *sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"length" ascending:NO] autorelease];
         NSArray                *sortedChunks = [chunks sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];
         
         CGSize                labelSize = theThingLabel.bounds.size;
         CGSize                projectedSize = [[sortedChunks objectAtIndex:0] sizeWithFont:[UIFont boldSystemFontOfSize:maxFontSize]];
    
         if (projectedSize.width > labelSize.width) {
              CGFloat percentageDifference = ((projectedSize.width - labelSize.width)/labelSize.width)*100;
              if (percentageDifference > 50) {
                   newFontSize = ((minFontSize/percentageDifference)*100) - 10;
                   if (newFontSize < minFontSize) newFontSize = minFontSize;
              } else {
                   newFontSize = ((percentageDifference/maxFontSize)*100) - 10;
                   if(newFontSize < (maxFontSize/2)) newFontSize = maxFontSize - abs(newFontSize);
              }
         } else {
              if ( textLength > 11 && textLength < 255) {
                   newFontSize = (maxFontSize - ((maxFontSize - minFontSize) * ((textLength- 11) / 100)));
              } else if (textLength <= 11) {
                   newFontSize = maxFontSize;
              } else if (textLength >= 255) {
                   newFontSize = minFontSize;
              }
         }
    
         return [UIFont boldSystemFontOfSize:newFontSize];
    }
This works, to an extent, but often falls over when the text is a bit on the long side, these two example show it rendering the following strings:
  • "short amount of text"
  • "a substantially longer amount of text which i still want to render nicely."





As you can see in the second example (with far longer text) there are a number of issues:
  • The initial widow
  • The dropped y
  • The missing "nicely."

So with all this in mind, what are my options, I'm open to moving to using coretext if this is the right solution, but have no idea where to start, it's also possible i've made a mistake which i just can't see in my "font size guessing" code.

Any input you could offer would gratefully recieved.

Many Thanks!
craig t mackenzie is offline   Reply With Quote
Old 11-16-2010, 11:04 PM   #2 (permalink)
Registered Member
 
Join Date: May 2010
Posts: 107
JDave is on a distinguished road
Default

It looks like you are trying to reinvent the wheel, unless I misunderstand your issues. I think you need to assess where your data/text is coming from (the source of the text), and then look at the similarities and differences with the:

UILabel class
UITextView class
UIWebView class

To answer the vertical alignment issue: As a hack, consider aligning the text vertically may be easier to do if you align the .center of your UILabel to the center of the region you want it centered to...

To answer the text formatting issues and font size:
Most of the items you are looking to do are built into the UILabel class [link].

Specifically, have a look at:
textAlignment property
lineBreakMode property
adjustsFontSizeToFitWidth property


also, I recently came across:
sizeToFit, which is from UIView's class, which UILabel inherits from. I have had variable results with sizeToFit and other UILabel property combinations noted above. Here is the usage:
Code:
[myLabel sizeToFit]; // give this a try and see what happens
The hyphenation is something that you will need to manage separately, as finding where to break up a word and when to hyphenate it is a complex task that is language dependent, and may be too big of a task. Now if you already have hyphens in your text string, then you should be able to utilize the built in properties of UILabel.

If that fails, have a look at UITextView [link]. UITextView has more text formatting options than UILabel, mainly the ability to edit with the keyboard and display. BUT just remember to disable scrolling and the keyboard interactivity as needed.

Alas, as a third option, have a look at UIWebView [link], it can do a lot of formatting on the fly, and is good if your source is HTML or RTF (ie some file), and is not just for internet content, per se.
JDave is offline   Reply With Quote
Reply

Bookmarks

Tags
coretext, iphone, layout, text, uilabel

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 374
7 members and 367 guests
apatsufas, Kirkout, lzwasyc, MarkC, Sami Gh, SamorodovAlex, VinceYuan
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,664
Threads: 94,120
Posts: 402,898
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Leslie80
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 02:43 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0