Joel Derfner
Joel Derfner

Reputation: 2207

Why doesn't my NSString fit into a frame created with the width of my NSString?

Here's how it seems to me the code I'm dealing with ought to be written:

    if (!instructions)
{
    instructions = [[UITextView alloc] init];
    instructions.backgroundColor=[UIColor clearColor];
    instructions.font = [UIFont fontWithName:@"Helvetica Neue" size:12];
    instructions.editable=NO;
    instructions.text = @"\nFor millennia, the Japanese haiku has allowed great\nthinkers to express their ideas about the world in three\nlines of five, seven, and five syllables respectively.";

//THIS IS THE RELEVANT LINE:
    NSString *t = @"thinkers to express their ideas about the world in three"; //Using this to set width since it's the longest line of the string.

    CGSize thisSize = [t sizeWithFont:[UIFont fontWithName:@"Helvetica Neue" size:12]];
    float textWidth = thisSize.width;
    const int INSTRUCTIONS_HEIGHT=10; //I know this is an ugly hack.
    float textHeight = thisSize.height*INSTRUCTIONS_HEIGHT;
    instructions.frame = CGRectMake(screenWidth/2-textWidth/2, screenHeight/2-textHeight/2, textWidth, textHeight);
}

But the output looks like this, with the line having to wrap around:

enter image description here

When I adjust the code to add a few characters to the line setting the width:

    {
    instructions = [[UITextView alloc] init];
    instructions.backgroundColor=[UIColor clearColor];
    instructions.font = [UIFont fontWithName:@"Helvetica Neue" size:12];
    instructions.editable=NO;
    instructions.text = @"\nFor millennia, the Japanese haiku has allowed great\nthinkers to express their ideas about the world in three\nlines of five, seven, and five syllables respectively.";

//THIS IS THE LINE I CHANGED:
    NSString *t = @"thinkers to express their ideas about the world in three lin"; //

    CGSize thisSize = [t sizeWithFont:[UIFont fontWithName:@"Helvetica Neue" size:12]];
    float textWidth = thisSize.width;
    const int INSTRUCTIONS_HEIGHT=6; //Since there's no wraparound here I could reduce this number.
    float textHeight = thisSize.height*INSTRUCTIONS_HEIGHT;
    instructions.frame = CGRectMake(screenWidth/2-textWidth/2, screenHeight/2-textHeight/2, textWidth, textHeight);
}

the output looks like what I want:

enter image description here

Why does the line "thinkers to express their ideas about the world in three" not fit into a text view set at the width of the line "thinkers to express their ideas about the world in three"?

(Obviously I have some learning to do about ways to orient things on screen. I haven't figured out "center" yet, for example, which I suspect would solve a lot of my problems. But this is what I'm working with for now, and I'm curious.)

Upvotes: 1

Views: 302

Answers (3)

jsd
jsd

Reputation: 7703

You need to constrain the sizeWithFont to the width of the text view.

Something like:

drawnSize = [t sizeWithFont:[UIFont fontWithName:@"Helvetica Neue" size:12]
                 constrainedToSize:CGSizeMake(instructions.frame.size.width, 3000)
                 lineBreakMode:NSLineBreakByWordWrapping ] ;

You may also need to obtain the insets from the text view and subtract them from the width:

UIEdgeInsets edgeInsets = instructions.contentInset;
CGFloat totalwidth = instructions.frame.size.width - edgeInsets.left - edgeInsets.right;

Upvotes: 3

Hermann Klecker
Hermann Klecker

Reputation: 14068

One reason is that you are using text views. What you do would work fine for UILabel. And Labels may have multiple lines. So if you donnot really need the text view for scrolling the text then consider using lables instead.

I don't remember the exact value. But for a text view consider some fuzzy insect value by which the actual width/space for the text itself is smaller than the text view with.

Upvotes: 3

Patrick Tescher
Patrick Tescher

Reputation: 3447

UITextView by default has some padding. You can get rid of it if you want: How to lose margin/padding in UITextView?

Upvotes: 3

Related Questions