ios--动态计算label的高度

#import "ViewController.h"

@interface ViewController ()

{

UILabel *dynamicLabel;

}

@end

@implementation ViewController

- (void)viewDidLoad {

if (!dynamicLabel) {

dynamicLabel=[[UILabel alloc]init];

}

dynamicLabel.backgroundColor=[UIColor redColor];

dynamicLabel.font=[UIFont fontWithName:@"HelveticaNeue" size:24.0f];

dynamicLabel.text=@"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus eu urna quis lacus imperdiet scelerisque a nec neque. Mauris eget feugiat augue, vitae porttitor mi. Curabitur vitae sollicitudin augue. Donec id sapien eros. Proin consequat tellus in vehicula sagittis. Morbi sed felis a nibh hendrerit hendrerit. Lorem ipsum dolor sit.";

dynamicLabel.lineBreakMode=NSLineBreakByWordWrapping;

dynamicLabel.numberOfLines = 0;

CGRect tempRect=[dynamicLabel.text boundingRectWithSize:CGSizeMake(300,1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObjectsAndKeys:dynamicLabel.font,NSFontAttributeName, nil] context:nil];

dynamicLabel.frame=CGRectMake(10, 64, tempRect.size.width,  tempRect.size.height);

[self.view addSubview:dynamicLabel];

}

时间: 2024-07-29 14:19:16

ios--动态计算label的高度的相关文章

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

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

iOS 动态计算文本内容的高度

关于ios 下动态计算文本内容的高度,经过查阅和网上搜素,现在看到的有以下几种方法: 1. //  获取字符串的大小  ios6 - (CGSize)getStringRect_:(NSString*)aString { CGSize size; UIFont *nameFont=[UIFont fontWithName:@"Helvetica" size:13]; size=[aString sizeWithFont:nameFont constrainedToSize:CGSize

iOS开发动态计算cell的高度

在iOS开发过程中,我们经常会用到UITableView, 谈到UITableView当然少不了UITableViewCell.那么有时候我们就会有疑惑,怎么样才能让cell的高度根据文字的大小多少,以及照片的高度来动态设计呢? 下面我们来看一下,到底怎么做才能让cell的高度动态变化,让界面看起来更美观协调一些呢? //动态设置cell的高度 + (CGFloat)heightForRowWithModel:(PhotoInfo *)photoInfo { //1.图片的高度 //让图片等比例

动态计算Label高度

//1.设置该label的numberOfLines为0 self.titleLabel.numberOfLines = 0; //2.字体的设置要与之前相同 NSDictionary * attribute = @{NSFontAttributeName:[UIFont systemFontOfSize:17]}; //3.将size的高度设为最大值 CGSize size = CGSizeMake(355, CGFLOAT_MAX); //4.将label的高度设为动态计算出的size的高度

iOS开发总结-UITableView 自定义cell和动态计算cell的高度

UITableView cell自定义头文件: shopCell.h #import <UIKit/UIKit.h> @interface shopCell : UITableViewCell @property (strong, nonatomic)  UIImageView *image;@property (strong, nonatomic)  UILabel *name;@property (strong, nonatomic)  UILabel *itemshop;@propert

根据文本内容动态计算文本框高度的步骤

在视图加载的过程中,是先计算出frame,再根据frame加载视图的,所以在设计计算高度的方法的时候,设计成加号方法; //首先给外界提供计算cell高度的方法 + (CGFloat)heightForRowWithDic:(NSDictionary *)dic { //cell高度 = nameLabel高度 + contentLabel高度 + 间距; return [self heightForText:dic[@"content"]] + 30 + kHeight_NameLa

iOS 动态计算行高,宽等

UILabel有两个计算文字大小的方法: 1.针对对富文本计算NSAttributedString - (CGRect)boundingRectWithSize:(CGSize)size options:(NSStringDrawingOptions)options attributes:(nullable NSDictionary*)attributes context:(nullable NSStringDrawingContext *)context NS_AVAILABLE(10_11,

iOS 动态计算cell高度实用API

// Use the estimatedHeight methods to quickly calcuate guessed values which will allow for fast load times of the table. // If these methods are implemented, the above -tableView:heightForXXX calls will be deferred until views are ready to be display

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.