1

これが私が扱っているコードが書かれるべきであると私にどのように思われるかです:

    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つで表現する考え者」という行の幅に設定されたテキストビューに収まらないのはなぜですか。

(明らかに、画面上で物事を方向付ける方法について学ぶことがあります。たとえば、「中心」はまだわかりません。これにより、多くの問題が解決されると思います。しかし、これが私が取り組んでいることです。今のところ、私は興味があります。)

4

3 に答える 3

3

UITextViewには、デフォルトでいくつかのパディングがあります。必要に応じてそれを取り除くことができます:UITextViewでマージン/パディングを失う方法は?

于 2013-01-15T00:23:20.370 に答える
3

sizeWithFontをテキストビューの幅に制限する必要があります。

何かのようなもの:

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

また、テキストビューから挿入図を取得し、幅から差し引く必要がある場合もあります。

UIEdgeInsets edgeInsets = instructions.contentInset;
CGFloat totalwidth = instructions.frame.size.width - edgeInsets.left - edgeInsets.right;
于 2013-01-15T00:23:53.070 に答える
3

1つの理由は、テキストビューを使用していることです。あなたがすることはUILabelのためにうまくいくでしょう。また、ラベルには複数の行が含まれる場合があります。したがって、テキストをスクロールするためにテキストビューが本当に必要ない場合は、代わりにラベルの使用を検討してください。

正確な値は覚えていません。ただし、テキストビューの場合は、テキスト自体の実際の幅/スペースがテキストビューよりも小さくなるファジーな昆虫の値を考慮してください。

于 2013-01-15T00:23:56.470 に答える