これが私が扱っているコードが書かれるべきであると私にどのように思われるかです:
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);
}
出力は私が望むもののように見えます:
「世界についての考えを3つで表現する思想家」という行が、「世界についての考えを3つで表現する考え者」という行の幅に設定されたテキストビューに収まらないのはなぜですか。
(明らかに、画面上で物事を方向付ける方法について学ぶことがあります。たとえば、「中心」はまだわかりません。これにより、多くの問題が解決されると思います。しかし、これが私が取り組んでいることです。今のところ、私は興味があります。)