0

UITextViewのテキストのサイズを調整して収まるようにしようとしています。私は次のコードを持っています:

NSString *storyTitle = [newsfeedToSubject.properties valueForKey:@"title"];

    int currentFontSize = 18;
    UIFont *currentFont = [UIFont fontWithName:kProximaNovaBold size:currentFontSize];
    CGSize storyTitleSize = [storyTitle sizeWithFont:currentFont constrainedToSize:self.newsfeedStoryTitle_.frameSize lineBreakMode:UILineBreakModeWordWrap];

    while (storyTitleSize.height >= self.newsfeedStoryTitle_.frameHeight){
        currentFontSize--;
        currentFont = [UIFont fontWithName:kProximaNovaBold size:currentFontSize];
        storyTitleSize = [storyTitle sizeWithFont:currentFont constrainedToSize:self.newsfeedStoryTitle_.frameSize lineBreakMode:UILineBreakModeWordWrap];
    }

    [self.newsfeedStoryTitle_ setFont:currentFont];
    [self.newsfeedStoryTitle_ setText:storyTitle];
    [self.newsfeedStoryTitle_ setBackgroundColor:[UIColor redColor]];

しかし、これが私が得ているものです:

ここに画像の説明を入力してください

別の改行モードを試し、自動サイズ変更をnoneに設定しましたが、役に立ちませんでした。何か案が?

4

2 に答える 2

1

フレームのサイズを決定するために使用しているため、 while ループは 目的の処理storyTitleSize.height を実行しません。これは、テキストの回り込み効果を考慮していません。1 つの提案は、文字列を切り捨てて、タイトルの一定量の文字のみを表示することです。それ以外の場合は、フレームの幅と文字列の長さに基づいて、改行の数を計算し、使用する必要があります。

while(storyTitleSize.height * numLineBreaks >= self.newsFeedStoryTitle_.frameHeight)
{
    ...
}

それが役立つことを願っています。

このスレッドのように、フレーム (UITextView) のサイズを動的に変更することもできます

于 2012-07-31T18:08:49.153 に答える
0

これらのコードを試してください

while (self.textView.contentSize.height >= frameHeight){
    currentFontSize--;
    currentFont = [UIFont fontWithName:kProximaNovaBold size:currentFontSize];
    [self.textView setFont:currentFont];
    self.textView.text = @"your string";
}
于 2012-07-31T18:08:55.043 に答える