NSString sizeWithFont: constrainedToSize: Rückkehr falsche Höhe auf Retina-Displays
-
10-10-2019 - |
Frage
Ich glaube, ich einen Rand Fall gefunden für sizeWithFont: constrainedToSize: wo, auf einem Retina-Display, wird es manchmal (es auf Zeilenumbruch basiert scheint) gibt eine Höhe 1 Zeile höher als tatsächlich benötigt wird, und was noch wichtiger ist, als zieht es tatsächlich.
Hinweis: Der eigentliche Code, den ich innerhalb performant centric Hand gezeichnet variabler Höhe Tabellenansicht Zelle Code begraben verwenden, so dass ich das Problem destilliert habe bis zu so einfach einem wenig Beispielcode wie möglich. (Bitte beachten Sie dies, wenn auf Antwort etwas anderes als meine Frage versuchen: -)
Dieses Beispiel UIView füllt es die Inhalte, Maßnahmen, den Text zu passen (verpackt), füllt diese rect, dann zieht den Text.
Auf einer Netzhauteinrichtung (oder Simulators) die Höhe zurückgeführt wird 1 Zeile zu hoch, aber auf einem vorge Retina-Gerät (oder Simulators) es gibt die richtige Höhe.
Ich würde sehr jede Einsicht schätzt jemand haben kann, wie es ein Fehler ist, ich zu beheben möchte!
Vielen Dank!
-Eric
- (void)drawRect:(CGRect)rect {
NSString * theString = @"Lorem ipsum dolor sit ameyyet, consectetur adipiscing elit. Etiam vel justo leo. Curabitur porta, elit vel.";
UIFont * theFont = [UIFont systemFontOfSize:12];
CGSize theConstraint = CGSizeMake(rect.size.width - 20, rect.size.height - 20);
CGSize theResultSize = [theString sizeWithFont:theFont constrainedToSize:theConstraint];
// dump the measurements
NSLog(@"returned a size h = %f, w = %f", theResultSize.height, theResultSize.width);
// fill the whole rect
CGContextRef context = UIGraphicsGetCurrentContext();
[[UIColor yellowColor] set];
CGContextFillRect(context, rect);
// fill the measured rect
CGRect theRect = CGRectMake(10, 10, theResultSize.width, theResultSize.height);
context = UIGraphicsGetCurrentContext();
[[UIColor cyanColor] set];
CGContextFillRect(context, theRect);
// draw the text
[[UIColor blackColor] set];
[theString drawInRect:theRect withFont:theFont];
}
Das ganze einfache Projekt ist verfügbar hier .
Simulator Bilder:
http: //files.droplr. com / files / 9979822 / aLDJ.Screen% 20shot% 202011-01-11% 20at% 2012% 3A34% 3A34.png
http: //files.droplr. com / files / 9979822 / YpCM.Screen% 20shot% 202011-01-11% 20at% 2012% 3A36% 3A47.png
Lösung
It seems to be an issue with your simulator. This is what I got when I ran it with a Retina simulator on OS 4.3.2
Andere Tipps
Following is the method I use to find the height of label for dynamic text content. This works fine in my app
- (float)getHeightFortheDynamicLabel:(NSString *)stringForTheLabel
{
UITextView *aSampleTextView;
// 30 is the minimum height
aSampleTextView = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, mywidth, 30)];
aSampleTextView.text = stringForTheLabel;
aSampleTextView.font = [UIFont systemFontOfSize:kMyFontSize];
aSampleTextView.alpha = 0;
[self.view addSubview:aSampleTextView];
float textViewHeight = aSampleTextView.contentSize.height;
[aSampleTextView removeFromSuperview];
[aSampleTextView release];
return textViewHeight;
}
NSString *strSubstring = @"asdfghjklasdfghjkl adsds";
CGFloat maxWidth = 205.0;
CGFloat maxHeight = 9999;
CGSize maximumLabelSize = CGSizeMake(maxWidth,maxHeight);
CGSize expectedLabelSize = [strSubstring sizeWithFont:[UIFont fontWithName:@"StagSans-Light" size:12] constrainedToSize:maximumLabelSize lineBreakMode:UILineBreakModeWordWrap];
NSLog(@"returned a size h = %f, w = %f", expectedLabelSize.height, expectedLabelSize.width);
return expectedLabelSize;