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:
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:
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.)