【问题标题】:Add text under table sections in UITableView在 UITableView 的表格部分下添加文本
【发布时间】:2014-02-01 23:07:22
【问题描述】:

我试图模仿 iOS 7“快捷方式”之类的视图。

我使用了带有静态单元格、1 节和 2 行的 UITableViewController。对于文本“创建...的快捷方式”,我尝试了 UILabel 以及节页脚。页脚的问题是我无法找到将长文本包装成多行的方法。所以我很好奇在部分下实现文本的“标准”或首选方式是什么?谢谢!

【问题讨论】:

    标签: ios objective-c uitableview uilabel footer


    【解决方案1】:

    如果您在情节提要中选择表格视图部分并在“页脚”中输入您的文本,它将自动换行。

    如果您想更好地控制字体,可以使用 -[UITableViewDelegate tableView:viewForFooterInSection:] 设置自定义页脚视图

    【讨论】:

    • 谢谢,这成功了!我缺少的是表格视图样式必须是“分组”,否则页脚不会自动换行
    • @Jesse:如何更改页脚文本的对齐方式?有什么想法吗?
    • @Nit 在 Interface Builder 中,通过将 UIView 拖到表格视图底部来创建自定义页脚视图。在页脚视图中添加一个 UILabel 并设置 UILabel 的对齐方式。
    • 谢谢@marsant,我也错过了这个,我被困了将近一个小时,哈哈
    【解决方案2】:

    以下代码复制了您在上面显示的内容。我没有创建标签并返回它,而是创建了一个简单的 UIView,它是一些任意硬编码的高度(您可能希望更改它以满足您的需要),然后创建一个标签并将标签添加到视图中,返回视图,这样您就可以像创建框架时那样在视图中将标签居中。通过将行数设置为 0,您实际上是在告诉标签会有多行,并且我在标签上设置了一个自动调整大小的蒙版,这样如果视图旋转,文本仍将在视图中保持居中,其中任一填充有 10 个点边。您还需要确保返回适当的 heightForFooterInSection,以防您以后决定在其下方添加一些内容,它不会与您的页脚重叠。

    - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
        UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 80)];
    
        UILabel *explanationLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, self.view.frame.size.width - 20, 80)];
        explanationLabel.textColor = [UIColor darkGrayColor];
        explanationLabel.numberOfLines = 0;
        explanationLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
        explanationLabel.text = @"Create a shortcut that will automatically expand into the word or phrase as you type.";
        [footerView addSubview:explanationLabel];
    
        return footerView;
    }
    
    - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
        return 80;
    }
    

    给我这个:

    【讨论】:

    • 请注意,标签居中应该是UILabel *explanationLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, self.view.frame.size.width - 20, 80)];
    【解决方案3】:

    SWIFT 4 版本

    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
    
        let footerView = UIView(frame: CGRect(x: 0, y: 0, width: view.frame.size.width, height: 80))
    
        let explanationLabel = UILabel(frame: CGRect(x: 10, y: 0, width: view.frame.size.width - 20, height: 80))
        explanationLabel.textColor = UIColor.darkGray
        explanationLabel.numberOfLines = 0
        explanationLabel.text = tableDataSectionFooters[section]
        footerView.addSubview(explanationLabel as? UIView ?? UIView())
        return footerView
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 80
    }
    

    【讨论】:

      【解决方案4】:

      在页脚中使用 UILabel 并将其设置为换行(行数 = 0)

      【讨论】:

        【解决方案5】:

        所有其他答案在使用 UILabel 时都是正确的,但要达到 100% 相同的效果,您必须使用标签的属性文本属性。您还可以添加原始快捷方式屏幕截图中显示的缩进。

        【讨论】:

          猜你喜欢
          • 2016-05-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多