Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy doesn't my NSString fit into a frame created with the width of my NSString?
    primarykey
    data
    text
    <p>Here's how it seems to me the code I'm dealing with ought to be written:</p> <pre><code> 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); } </code></pre> <p>But the output looks like this, with the line having to wrap around:</p> <p><img src="https://i.stack.imgur.com/ipXSM.png" alt="enter image description here"></p> <p>When I adjust the code to add a few characters to the line setting the width:</p> <pre><code> { 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); } </code></pre> <p>the output looks like what I want:</p> <p><img src="https://i.stack.imgur.com/RkynI.png" alt="enter image description here"></p> <p>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"?</p> <p>(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.)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload