現在、シミュレーターを使用して古いアプリをiPhone 4に調整していますが、UILabelの描画とsizeWithFont:constrainedToSize:で、現在iPhone4シミュレーターでのみ表示される非常に奇妙な動作に遭遇する可能性があります。
次のエラーテキストをユーザーに表示しようとしています:@ "ユーザー名またはパスワードが正しくありません"このテキストは、上部、中央、下部の3つの部分で構成される動的エラーボックス内にあるため、ラベルのサイズを計算しますそれに応じて中央の背景画像フレームを変更できます。
UILabelサイズ計算コードの例を次に示します。
CGRect errorFrame = CGRectMake(40, 0, 240.0, 22.0);
UILabel *errorlabel = [[UILabel alloc] initWithFrame:errorFrame];
errorlabel.adjustsFontSizeToFitWidth = NO;
errorlabel.font = [UIFont fontWithName:@"HelveticaNeue" size:16];
errorlabel.textAlignment = UITextAlignmentLeft;
errorlabel.numberOfLines = 0;
errorlabel.text = @"Incorrect user name or password";
// since only the width is fixed I will use a really large height value
CGSize errorLabelSize = [errorlabel.text sizeWithFont:errorlabel.font constrainedToSize:CGSizeMake(240.0, 4600.0)];
CGRect newFrame = errorlabel.frame;
newFrame.size.height = errorLabelSize.height;
errorlabel.frame = newFrame;
// added so I can easily see the new frame
errorlabel.backgroundColor = [UIColor redColor];
[self.errorView addSubview:errorlabel];
[errorlabel release];
iPhone 3シミュレーターでコードを実行すると、sizeWithFont:constrainedToSize:メソッドは1行の高さを返し、このエラーテキストを1行に描画します。iPhone 4シミュレーターで同じコードを実行すると、sizeWithFont:constrainedToSize:は2行に必要なサイズ(170.0、42.0)を返しますが、ラベル自体は1行に描画されます。これは、sizeWithFontコードがレンダリングコードと同じロジックを使用していないかのようです。
エラーテキストを変更することはできません:)この問題を回避または解決する方法はありますか?
前もって感謝します