【发布时间】:2010-09-23 00:15:04
【问题描述】:
我有一个有点长的字符串,在 tablView 单元格中显示时会被截断。我这样做是为了使表格视图更宽:
tableView.rowHeight = 100;
如何使字体更小以及将文本换行在表格视图单元格中?
【问题讨论】:
标签: objective-c cocoa-touch uitableview
我有一个有点长的字符串,在 tablView 单元格中显示时会被截断。我这样做是为了使表格视图更宽:
tableView.rowHeight = 100;
如何使字体更小以及将文本换行在表格视图单元格中?
【问题讨论】:
标签: objective-c cocoa-touch uitableview
在tableView:cellForRowAtIndexPath: 中,您可以在 textLabel(或 descriptionLabel,取决于您使用的单元格样式)上设置一些属性来执行此操作。设置font 更改字体,设置linkBreakMode 使其自动换行,设置numberOfLines 设置最大行数(在此之后截断。您可以将其设置为0 表示没有最大值。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell* aCell = [tableView dequeueReusableCellWithIdentifier:kMyCellID];
if( aCell == nil ) {
aCell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kMyCellID] autorelease];
aCell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:10.0];
aCell.textLabel.lineBreakMode = NSLineBreakByWordWrapping; // Pre-iOS6 use UILineBreakModeWordWrap
aCell.textLabel.numberOfLines = 2; // 0 means no max.
}
// ... Your other cell setup stuff here
return aCell;
}
【讨论】:
UILineBreakModeWordWrap。 NSLineBreakByWordWrapping 相当于 iOS6。更新了我的答案。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell* Cell = [tableView dequeueReusableCellWithIdentifier:kMyCellID];
if( Cell == nil ) {
Cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kMyCellID] autorelease];
Cell.textLabel.font = [UIFont fontWithName:@"TimesNewRoman" size:10.0];
Cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
Cell.textLabel.numberOfLines = 2; // 0 means no max.
}
// your code here
return Cell;
}
【讨论】:
您应该继承 UITableViewCell 并使其具有 UITextView 而不是 UITextField。然后将您的子类分配给您的 UITableView。
【讨论】: