考虑我在UILabel
(动态文本的长行)中有以下文本:
由于外星军队的数量远远超过了团队,因此玩家必须利用后世界末日的世界,例如在垃圾箱,支柱,汽车,瓦砾和其他物体后面寻找掩护.
我想调整UILabel's
高度,以便文本可以适应.我正在使用以下属性UILabel
来使文本包装.
myUILabel.lineBreakMode = UILineBreakModeWordWrap; myUILabel.numberOfLines = 0;
如果我没有朝着正确的方向前进,请告诉我.谢谢.
sizeWithFont constrainedToSize:lineBreakMode:
是使用的方法.以下是如何使用它的示例:
//Calculate the expected size based on the font and linebreak mode of your label // FLT_MAX here simply means no constraint in height CGSize maximumLabelSize = CGSizeMake(296, FLT_MAX); CGSize expectedLabelSize = [yourString sizeWithFont:yourLabel.font constrainedToSize:maximumLabelSize lineBreakMode:yourLabel.lineBreakMode]; //adjust the label the the new height. CGRect newFrame = yourLabel.frame; newFrame.size.height = expectedLabelSize.height; yourLabel.frame = newFrame;
你正朝着正确的方向前进.你需要做的就是:
myUILabel.numberOfLines = 0; myUILabel.text = @"Enter large amount of text here"; [myUILabel sizeToFit];
在iOS 6中,Apple为UILabel添加了一个属性,极大地简化了标签的动态垂直大小调整:preferredMaxLayoutWidth.
将此属性与lineBreakMode = NSLineBreakByWordWrapping和sizeToFit方法结合使用,可以轻松地将UILabel实例的大小调整为适应整个文本的高度.
来自iOS文档的引用:
preferredMaxLayoutWidth 多行标签的首选最大宽度(以磅为单位).
讨论 当应用布局约束时,此属性会影响标签的大小.在布局期间,如果文本超出此属性指定的宽度,则附加文本将流向一个或多个新行,从而增加标签的高度.
一个样品:
... UILabel *status = [[UILabel alloc] init]; status.lineBreakMode = NSLineBreakByWordWrapping; status.numberOfLines = 5; // limits to 5 lines; use 0 for unlimited. [self addSubview:status]; // self here is the parent view status.preferredMaxLayoutWidth = self.frame.size.width; // assumes the parent view has its frame already set. status.text = @"Some quite lengthy message may go here…"; [status sizeToFit]; [status setNeedsDisplay]; ...
而是以编程方式执行此操作,您可以在设计时在Storyboard/XIB中执行此操作.
在属性检查器中将UIlabel的行数属性设置为0.
然后根据要求设置宽度约束/(或)前导和尾随约束.
然后用最小值设置高度约束.最后选择您添加的高度约束,并在尺寸检查器中选择属性检查器旁边的高度约束,将高度约束的关系从等于 - 大于.更改.
无需添加单行代码即可完美检查此工作.(使用Autolayout)
我根据你的要求为你做了一个演示.从以下链接下载,
自动化UIView和UILabel
分步指南: -
第1步: -将约束设置为UIView
1)领先2)Top 3)尾随(来自主视图)
第2步: -将约束设置为标签1
1)领先2)前3)尾随(从它的超级视图)
第3步: -将约束设置为标签2
1)领先2)尾随(从它的超级视图)
第4步: - 最棘手的是从UIView向UILabel提供帮助.
步骤5: -(可选)将约束设置为UIButton
1)领先2)底部3)尾随4)固定高度(来自主视图)
输出: -
注意: -确保在Label属性中设置了Number of lines = 0.
我希望这个信息足以理解Autoresize UIView根据UILabel的高度和Autoresize UILabel根据文字.
谢谢大家的帮助,这是我尝试的代码,这对我有用
UILabel *instructions = [[UILabel alloc]initWithFrame:CGRectMake(10, 225, 300, 180)]; NSString *text = @"First take clear picture and then try to zoom in to fit the "; instructions.text = text; instructions.textAlignment = UITextAlignmentCenter; instructions.lineBreakMode = NSLineBreakByWordWrapping; [instructions setTextColor:[UIColor grayColor]]; CGSize expectedLabelSize = [text sizeWithFont:instructions.font constrainedToSize:instructions.frame.size lineBreakMode:UILineBreakModeWordWrap]; CGRect newFrame = instructions.frame; newFrame.size.height = expectedLabelSize.height; instructions.frame = newFrame; instructions.numberOfLines = 0; [instructions sizeToFit]; [self addSubview:instructions];
解决iOS7之前和iOS7以上的问题
// // UILabel+DynamicHeight.m // For StackOverFlow // // Created by Vijay on 24/02/14. // Copyright (c) 2014 http://Vijay-Apple-Dev.blogspot.com. All rights reserved. // #import#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending) #define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending) #define iOS7_0 @"7.0" @interface UILabel (DynamicHeight) /*====================================================================*/ /* Calculate the size,bounds,frame of the Multi line Label */ /*====================================================================*/ /** * Returns the size of the Label * * @param aLabel To be used to calculte the height * * @return size of the Label */ -(CGSize)sizeOfMultiLineLabel; @end // // UILabel+DynamicHeight.m // For StackOverFlow // // Created by Vijay on 24/02/14. // Copyright (c) 2014 http://Vijay-Apple-Dev.blogspot.com. All rights reserved. // #import "UILabel+DynamicHeight.h" @implementation UILabel (DynamicHeight) /*====================================================================*/ /* Calculate the size,bounds,frame of the Multi line Label */ /*====================================================================*/ /** * Returns the size of the Label * * @param aLabel To be used to calculte the height * * @return size of the Label */ -(CGSize)sizeOfMultiLineLabel{ NSAssert(self, @"UILabel was nil"); //Label text NSString *aLabelTextString = [self text]; //Label font UIFont *aLabelFont = [self font]; //Width of the Label CGFloat aLabelSizeWidth = self.frame.size.width; if (SYSTEM_VERSION_LESS_THAN(iOS7_0)) { //version < 7.0 return [aLabelTextString sizeWithFont:aLabelFont constrainedToSize:CGSizeMake(aLabelSizeWidth, MAXFLOAT) lineBreakMode:NSLineBreakByWordWrapping]; } else if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(iOS7_0)) { //version >= 7.0 //Return the calculated size of the Label return [aLabelTextString boundingRectWithSize:CGSizeMake(aLabelSizeWidth, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{ NSFontAttributeName : aLabelFont } context:nil].size; } return [self bounds].size; } @end
由于不推荐使用sizeWithFont,我使用这个.
这个获得标签特定的属性.
-(CGFloat)heightForLabel:(UILabel *)label withText:(NSString *)text{ NSAttributedString *attributedText = [[NSAttributedString alloc] initWithString:text attributes:@{NSFontAttributeName:label.font}]; CGRect rect = [attributedText boundingRectWithSize:(CGSize){label.frame.size.width, CGFLOAT_MAX} options:NSStringDrawingUsesLineFragmentOrigin context:nil]; return ceil(rect.size.height); }
您可以TableViewController's
(UITableViewCell *)tableView:cellForRowAtIndexPath
通过以下方式实现方法(例如):
#define CELL_LABEL_TAG 1 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSString *text = @"my long text"; static NSString *MyIdentifier = @"MyIdentifier"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:identifier] autorelease]; } CGFloat width = [UIScreen mainScreen].bounds.size.width - 50; CGFloat height = [self textHeight:text] + 10; CGRect frame = CGRectMake(10.0f, 10.0f, width, height); UILabel *cellLabel = [[UILabel alloc] initWithFrame:frame]; cellLabel.tag = CELL_LABEL_TAG; cellLabel.textColor = [UIColor blackColor]; cellLabel.backgroundColor = [UIColor clearColor]; cellLabel.textAlignment = UITextAlignmentLeft; cellLabel.font = [UIFont systemFontOfSize:12.0f]; [cell.contentView addSubview:cellLabel]; [cellLabel release]; return cell; } UILabel *label = (UILabel *)[cell viewWithTag:CELL_LABEL_TAG]; label.text = text; label.numberOfLines = 0; [label sizeToFit]; return cell;
还可以使用NSString
的sizeWithFont:constrainedToSize:lineBreakMode:
方法来计算文本的高度.
这是一个类别版本:
UILabel + AutoSize.h #import
@interface UILabel (AutoSize) - (void) autosizeForWidth: (int) width; @end
的UILabel + AutoSize.m
#import "UILabel+AutoSize.h" @implementation UILabel (AutoSize) - (void) autosizeForWidth: (int) width { self.lineBreakMode = UILineBreakModeWordWrap; self.numberOfLines = 0; CGSize maximumLabelSize = CGSizeMake(width, FLT_MAX); CGSize expectedLabelSize = [self.text sizeWithFont:self.font constrainedToSize:maximumLabelSize lineBreakMode:self.lineBreakMode]; CGRect newFrame = self.frame; newFrame.size.height = expectedLabelSize.height; self.frame = newFrame; } @end