在我看来,我正在处理的代码应该是这样写的:
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);
}
但输出看起来像这样,行必须环绕:
当我调整代码以在设置宽度的行中添加一些字符时:
{
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);
}
输出看起来像我想要的:
为什么“思想家在三个方面表达他们对世界的想法”这一行不适合设置在“思想家在三个方面表达他们对世界的想法”这一行宽度处的文本视图?
(显然,我需要学习如何在屏幕上定位事物。例如,我还没有弄清楚“中心”,我怀疑这会解决我的很多问题。但这就是我正在使用的现在,我很好奇。)