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 2015-01-21 11:23:37
沒關係 - 0應該沒問題 – dijipiji 2015-01-21 12:16:52