So berechnen Sie die Höhe eines NSAttributedString mit der angegebenen Breite in iOS 6 [duplizieren]
Mögliche Duplikate:
So erhalten Sie die Höhe für NSAttributedString mit einer festen Breite
Jetzt ist NSAttributedString in iOS 6 verfügbar. Für Layoutzwecke möchte ich wissen, wie die erforderliche Höhe eines NSAttributedString bei fester Breite berechnet wird. Ich suche etwas, das NSStrings entspricht- (CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size
aber für NSAttributedString.
Zur Berechnung der Zeichnungsgröße von NSAttributedStrings stehen zwei Methoden zur Verfügung:
- (CGSize)size
Kann nicht verwendet werden, da keine Breite berücksichtigt wird.Ich habe es versucht- (CGRect)boundingRectWithSize:(CGSize)size options:(NSStringDrawingOptions)options context:(NSStringDrawingContext *)context
, aber irgendwie gibt es mir nicht die richtige Höhe. Ich denke, die Methode ist fehlerhaft. Wenn ich den folgenden Code ausführe, gibt es mirbounding size: 572.324951, 19.000000
die angegebene Breite von 200 zu ignorieren. Es sollte mir so etwas wie 100 der Höhe geben.NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] init]; NSDictionary *attributes = @{NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue" size:15], NSForegroundColorAttributeName : [UIColor blueColor]}; [attributedString appendAttributedString:[[NSAttributedString alloc] initWithString:@"Attributed String\n" attributes:attributes]]; [attributedString appendAttributedString:[[NSAttributedString alloc] initWithString:@"Attributed String\n" attributes:attributes]]; [attributedString appendAttributedString:[[NSAttributedString alloc] initWithString:@"Attributed String\n" attributes:attributes]]; [attributedString appendAttributedString:[[NSAttributedString alloc] initWithString:@"Attributed String\n" attributes:attributes]]; [attributedString appendAttributedString:[[NSAttributedString alloc] initWithString:@"Attributed String\n" attributes:attributes]]; CGRect frame = [attributedString boundingRectWithSize:CGSizeMake(200, 1000) options:0 context:nil]; NSLog(@"bounding size: %f, %f", frame.size.width, frame.size.height);
Für Mac OS X stehen andere Methoden zur Verfügung, jedoch nicht für iOS.