0
나는 텍스트의 길이에 따라 라벨의 높이를 얻으려고합니다. 그러나 단어가 한 줄에 들어 가지 않으면 단어의 일부가 다음 줄로 넘어갑니다. 이 메서드는 한 행의 높이 만 반환합니다. 누구든지 나를 도울 수 있습니까?잘못된 라벨 높이
- (CGFloat)heightFromText:(NSString *)text{
if (!text) {
return 0.0;
}
UIFont *font = [UIFont systemFontOfSize:16.0];
UIColor *color = [UIColor colorWithColorCode:@"438fbe"];
NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle alloc];
paragraphStyle.lineBreakMode = NSLineBreakByCharWrapping;
NSDictionary *attributes = @{NSForegroundColorAttributeName : color, NSFontAttributeName : font, NSParagraphStyleAttributeName : paragraphStyle};
NSAttributedString *attributed = [[NSAttributedString alloc] initWithString:text attributes:attributes];
CGRect needed = [attributed boundingRectWithSize:CGSizeMake(self.width, CGFLOAT_MAX) options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading) context:nil];
CGFloat textHeight = ceilf(needed.size.height);
return textHeight;
}
을 그래서 내가 레이블을 만들 때 지정된 것을 높이? 0.0? – MacUser
은 중요하지 않습니다. 0은 괜찮습니다. – dijipiji