【问题标题】:How to set different fonts in table view detail text label如何在表格视图详细信息文本标签中设置不同的字体
【发布时间】:2012-03-14 10:44:14
【问题描述】:

在单元格样式 value1 的表格视图中,我在文本标签中显示日期和时间。详细我显示的文本标签**Title:-Comments**

所有标题和 cmets 长度不同。

![在此处输入图片描述][1] 现在我需要显示标题用一种颜色、字体和 cmets 用另一种颜色和字体我需要做什么

【问题讨论】:

  • [cell.textLabel setTextColor:[UIColor redColor]]; [cell.textLabel setFont:[UIFont font....]];
  • this question的可能重复
  • @sunajledif 我认为 OP 希望将两种不同的字体合二为一 UILabel
  • @alanduncan 哎呀。是的。抱歉,我误解了这个问题。

标签: iphone objective-c uitableview uilabel


【解决方案1】:

创建单元格时,您需要在文本标签中设置所需的所有属性:

-(UITableViewCell *)tableView:(UITableView *)_tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath {
  static NSString *kCellId = @"CellId";
  UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:kCellId];
  if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCellId];
    cell.accessoryType = UITableViewCellAccessoryNone;
    cell.textLabel.font = [UIFont fontWithName:@"CenturyGothic-Bold" size:17];
    cell.textLabel.textColor = [UIColor colorWithRed:53/255.0 green:55/255.0 blue:53/255.0 alpha:1];
    cell.selectedBackgroundView = [[UIView alloc] initWithFrame:cell.frame];
    cell.selectedBackgroundView.backgroundColor = [UIColor colorWithRed:(73/255.0) green:(14/255.0) blue:(111/255.0) alpha:1];
  }

  id obj = [filteredList objectAtIndex:indexPath.row];

  cell.textLabel.text = [obj valueForKey:@"title"];

  return cell;
}

【讨论】:

  • 我想在一个 UILabel 中使用两种不同的字体
  • 你不能用常规的 UILabel 做到这一点。您要么使用两个标签,要么必须使用 CoreText。
  • 你需要继承 UILabel。而且您还需要使用 CoreText 和 NSAttributedString。
【解决方案2】:

有两种方法可以解决这个问题。

您可以创建一个自定义的UITableViewCell 并布局三个单独的UILabel 实例,您可以根据自己的喜好设置样式;或者,您可以创建自定义UITableViewCell 并使用UILabel 的直接替换,从而允许您使用NSAttributedString。第一个解决方案会更直接。

【讨论】:

    猜你喜欢
    • 2015-06-27
    • 1970-01-01
    • 2014-08-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-08
    • 1970-01-01
    • 1970-01-01
    • 2011-09-12
    相关资源
    最近更新 更多