Jak obliczyć wysokość NSAttributesString o podanej szerokości w iOS 6 [duplikat]
Możliwy duplikat:
Jak uzyskać wysokość dla NSAttributesString o stałej szerokości
Teraz NSAttributedString jest dostępny w iOS 6. Dla celów układu chcę wiedzieć, jak obliczyć wymaganą wysokość NSAttributesString pod stałą szerokością. Szukam czegoś, co jest równoważne z NSString- (CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size
ale dla NSAttributedString.
Aby obliczyć rozmiar rysunku NSAttributesStrings, dostępne są dwie metody:
- (CGSize)size
nie można użyć, ponieważ nie uwzględnia żadnej szerokości.próbowałem- (CGRect)boundingRectWithSize:(CGSize)size options:(NSStringDrawingOptions)options context:(NSStringDrawingContext *)context
, ale jakoś nie daje mi odpowiedniej wysokości. Myślę, że metoda jest błędna. Jeśli uruchomię następujący kod, da mi tobounding size: 572.324951, 19.000000
ignorując podaną szerokość 200. Powinien dać mi około 100 wysokości.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);
Istnieją inne metody dostępne dla systemu Mac OS X, ale nie dla systemu iOS.