我有一个从数据库填充的UILabel..UILabel中的文本可以很长,也可以短..问题是文本在最后被剪切..例如,它如下所示:"www.sample.com/h1/h2/h3..."它应该在哪里:"www.sample.com/h1/h2/h3/h4/h5/h6.html"
这是我正在使用的代码:
CGFloat constraintWidth = 180.0f;
CGSize labelsize = [details.web sizeWithFont:self.webTextLabel.font constrainedToSize:CGSizeMake(constraintWidth, CGFLOAT_MAX) lineBreakMode:NSLineBreakByWordWrapping];
CGRect webFrame = CGRectMake(20, self.webTitleLabel.frame.origin.y + self.WebTitleLabel.frame.size.height + 10, constraintWidth, labelsize.height);
[self.webLabel setFrame:webFrame];
[self.webLabel setText:details.web]; // details.web is where I get it from the database
self.webLabel .numberOfLines=0;
我正在使用 Xcode 5 和 iOS 7
解决:我设法使用这种方法解决了它:
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 = UILineBreakModeWordWrap;
[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];
使用以下内容获取文本或标签的动态大小
labelHeight = [details.web boundingRectWithSize:CGSizeMake(constraintWidth, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin | NSLineBreakByWordWrapping attributes:@{NSFontAttributeName:self.webTextLabel.font} context:nil];