我使用以下代码在IOS中添加文本
//Set up label frame
UILabel *tempLabel = [[UILabel alloc]initWithFrame:CGRectMake(100, 10, 210, 80)];
self.answer_text_label = tempLabel;
[tempLabel release];
[self.preview_answer_container addSubview:self.answer_text_label];
//Populate label with text
self.answer_text_label.text = self.answer.text;
self.answer_text_label.numberOfLines = 4;
self.answer_text_label.lineBreakMode = UILineBreakModeWordWrap;
[self.answer_text_label sizeToFit];
但是,我得到的结果是这样的,文本似乎向右溢出,而不是按照我的标签设置“CGRectMake(100、10、210、80)”中规定的那样停留在框架内
如果我更改为self.answer_text_label.numberOfLines = 0
. 但这对我不起作用,因为我需要将文本限制在我规定的标签框架内。
有什么办法可以包装文本并只保留 4 行?
编辑:
尝试建议的代码
self.answer_text_label.text = self.answer.text;
[self.answer_text_label sizeToFit];
CGRect labelRect = self.answer_text_label.frame;
labelRect.origin.y = CGRectGetMaxY(labelRect);
labelRect.size = self.answer_text_label.frame.size;
self.answer_text_label.frame = labelRect;
结果如下。似乎没有解决我的问题