iOS 计算label的高度

+ (CGFloat)labelHeight:(UILabel *)label lines:(NSInteger)lines

{

NSRange range = NSMakeRange(0, label.attributedText.length);

NSDictionary *dic = [label.attributedText attributesAtIndex:0 effectiveRange:&range];

CGSize size = CGSizeMake(label.bounds.size.width, 10000);

CGFloat height = [label.text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:dic context:nil].size.height;

return height + 10.0f * lines;

}

时间: 2024-08-28 10:27:42

iOS 计算label的高度的相关文章

iOS textFiledView,label自适应高度

CGSize constraintSize; constraintSize.width = 320; constraintSize.height = MAXFLOAT; CGSize sizeFrame =[infoTextView.text sizeWithFont:infoTextView.font = [UIFont systemFontOfSize:12.0] constrainedToSize:constraintSize lineBreakMode:NSLineBreakByWord

ios 计算label宽度(高度)出错在iPhone5s、iphone6上出错

今天使用Label的时候,需要计算UILabel的高度,发现在4,4s上显示正常,在iPhone5s.iphone6上显示出错 使用的方法为: NSString *teacherComment = @"评语是写给学生看的,所以一方面评语要使用学生能看得懂的英语来写,所使用的词汇和语法不能过高或过低于学生的现有水平,要切合学生的实际水平,符合学生的个性心理..."; float commentHeight = 0.0; if ([teacherComment isEqual:@"

ios 设置label的高度随着内容的变化而变化

好吧 步骤1:创建label _GeRenJianJie = [[UILabel alloc]init]; 步骤2:设置label _GeRenJianJie.textColor = RGBAColor(95, 104, 115, 1); _GeRenJianJie.numberOfLines = 0; // 需要把显示行数设置成无限制 _GeRenJianJie.font = [UIFont systemFontOfSize:16]; _GeRenJianJie.textAlignment =

IOS 计算UILable字符串高度

IOS7 之前 截取了部分 @interface ZGViewCell () @property (nonatomic, strong) UILabel *nameLable; @end - (UILabel *)nameLable{ if (_nameLable == nil) { _nameLable = [[UILabel alloc] init]; [self.contentView addSubview:_nameLable]; // _nameLable.backgroundColo

ios计算内容的高度 (含7.0前及以后的版本的用法)

+ (CGFloat)heightForContent:(MyMsgTextModel *)content withWidth:(CGFloat)width { CGSize contentSize; if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0")) { NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init]; paragrap

ios--动态计算label的高度

#import "ViewController.h" @interface ViewController () { UILabel *dynamicLabel; } @end @implementation ViewController - (void)viewDidLoad { if (!dynamicLabel) { dynamicLabel=[[UILabel alloc]init]; } dynamicLabel.backgroundColor=[UIColor redColo

iOS动态计算Label的宽高

1.根据文字计算Label的宽度 1 +(CGFloat)widthOfString:(NSString *)string font:(UIFont *)font height:(CGFloat)height 2 { 3 NSDictionary * dict=[NSDictionary dictionaryWithObject: font forKey:NSFontAttributeName]; 4 CGRect rect=[string boundingRectWithSize:CGSize

Label自适应高度的用法及设置倒角

UILabel *label = [[UILabel alloc] init]; //根据内容动态计算label的高度 label.text = @"Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions"; //1.获取label当前使用的字体 UIFont *labelFo

IOS7中动态计算UILable的高度

.h文件 #import <UIKit/UIKit.h> @interface UILabel (ContentSize) - (CGSize)contentSize; @end .m文件 #import "UILabel+ContentSize.h" @implementation UILabel (ContentSize) - (CGSize)contentSize { NSMutableParagraphStyle * paragraphStyle = [[NSMut