stworzyłem kategorię wysokość dla UILabels MANIPULACJA:
UILabel + TCFlexibleHeight.h:
#import <UIKit/UIKit.h>
@interface UILabel (TCFlexibleHeight)
- (CGFloat)heightForText:(NSString*)text;
- (CGFloat)heightForCurrentText;
- (CGFloat)adjustHeightForCurrentText;
@end
U ILabel + TCFlexibleHeight.m:
#import "UILabel+TCFlexibleHeight.h"
static const NSInteger kMaxLines = 1000;
@implementation UILabel (TCFlexibleHeight)
- (CGFloat)heightForText:(NSString*)text {
if (text == nil) {
return 0;
}
NSInteger numberOfLines = self.numberOfLines > 0 ? self.numberOfLines : kMaxLines;
CGSize size = CGSizeMake(self.frame.size.width, self.font.lineHeight * numberOfLines);
return [text sizeWithFont:self.font constrainedToSize:size lineBreakMode:self.lineBreakMode].height;
}
- (CGFloat)heightForCurrentText {
return [self heightForText:self.text];
}
- (CGFloat)adjustHeightForCurrentText {
CGFloat height = [self heightForCurrentText];
CGRect frame = self.frame;
frame.size.height = height;
return height;
}
@end
z tą kategorią Twój kod będzie coś takiego:
[themeLabel setFrame:CGRectMake(leftMargin, mainScrollView.frame.origin.y + topPadding, 274, [themeLabel heightForCurrentText])];
Zauważ, że ta kategoria nie obsługuje przypisywane ciągi i wymagają zawijania linii ustawiony clip do postaci.
Jaka jest szerokość, która zostanie zwrócona? – Fogmeister
Wysokość wyrażana jest jako 'CGFloat', więc' nil' nie ma sensu. – trojanfoe
@Zmień, że to 0.0 po użyciu "sizeThatFits" i 274 po ustawieniu go ręcznie –