在过去的几个小时里,我一直在试图弄清楚这一点,但无济于事。我有一个自定义tableViewCell类我将它用于我的tableViewCells。我在自定义类中有3个标签和2个textFields。这是我的SearchResultTableViewCell.h
文件:
#import <UIKit/UIKit.h>
@interface SearchResultTableViewCell : UITableViewCell
@property (strong, nonatomic) IBOutlet UILabel *price;
@property (strong, nonatomic) IBOutlet UILabel *itemName;
@property (strong, nonatomic) IBOutlet UILabel *discount;
@property (strong, nonatomic) IBOutlet UITextField *quantity;
@property (strong, nonatomic) IBOutlet UITextField *discountInput;
@end
文本标签是从数据库条目中填充的。我现在遇到的问题是,当itemName
太长时,它会在显示中被切断。我试过在故事板中将标签的Lines属性设置为3行,但它不起作用。有没有人对如何在标签中实现多行有一个想法?谢谢你!
在你的CustomTableViewCell.m
self.itemName.numberOfLines = 0; // change to 0 to support multiple lines
self.itemName.lineBreakMode = NSLineBreakByWordWrapping;
CGRect frame = self.itemName.frame; // cache the frame of your item name label
CGFloat itemWidth = frame.size.width; // keep the width to a variable
CGSize constraint = CGSizeMake(itemWidth, 2000); // define your constraint, unlimited height, and the width will be the original width
CGSize size = [self.itemName.text sizeWithFont:self.itemName.font constrainedToSize:constraint lineBreakMode:self.itemName.lineBreakMode]; // get the size that fit your content
size.width = itemWidth; // use back the original width
frame.size = size; // update the size to the frame cached just now
self.itemName.frame = frame; // update the item name label frame
如果将numberolines设置为0(并将标签设置为文本换行),则标签将自动换行并使用所需的行数。
cell.textLabel。lineBreakMode = UILineBreakModeWordWrap;cell.textLabel.lineBreakMode = UILineBreakModeTextWrap;
如果您在IB中编辑UILabel,您可以通过按alt-return键来输入多行文本以获得换行符-return将完成编辑。