【问题标题】:SearchTextField display list from left to rightSearchTextField 从左到右显示列表
【发布时间】:2017-07-31 07:59:12
【问题描述】:

我正在使用 SearchTextField 窗格来显示预测文本。

https://github.com/apasccon/SearchTextField

以下代码使用显示列表构建表格。

public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    var cell = tableView.dequeueReusableCell(withIdentifier: SearchTextField.cellIdentifier)

    if cell == nil {
        cell = UITableViewCell(style: .subtitle, reuseIdentifier: SearchTextField.cellIdentifier)
    }

    //cell!.backgroundColor = UIColor.clear
    cell!.backgroundColor = UIColor.red
    cell!.layoutMargins = UIEdgeInsets.zero
    cell!.preservesSuperviewLayoutMargins = false
    cell!.textLabel?.font = theme.font
    cell!.detailTextLabel?.font = UIFont(name: theme.font.fontName, size: theme.font.pointSize * fontConversionRate)




    cell!.textLabel?.textColor = theme.fontColor
    cell!.detailTextLabel?.textColor = theme.fontColor

    cell!.textLabel?.text = filteredResults[(indexPath as NSIndexPath).row].title
    cell!.detailTextLabel?.text = filteredResults[(indexPath as NSIndexPath).row].subtitle
    cell!.textLabel?.attributedText = filteredResults[(indexPath as NSIndexPath).row].attributedTitle
    cell!.detailTextLabel?.attributedText = filteredResults[(indexPath as NSIndexPath).row].attributedSubtitle

    cell!.imageView?.image = filteredResults[(indexPath as NSIndexPath).row].image

    cell!.selectionStyle = .none

    cell!.detailTextLabel?.textAlignment = .right
    cell!.textLabel?.textAlignment = .right



    return cell!
}

生成一个匹配标题列表作为标签,如图所示

所以我的问题是如何让我的标签从右到左显示数据?

感谢您提供的任何帮助

【问题讨论】:

  • 你想让它对齐吗?
  • 是的。这正是我想要实现的目标
  • (style: .subtitle 不支持正确的文本对齐,如果您需要使用自定义标签,请将您的样式更改为(style: .Default)
  • 能否请您解释一下如何更改样式以及在哪里更改?

标签: ios swift3 xcode8


【解决方案1】:

如果你看框架,你可以设置

cell!.detailTextLabel?.textAlignment = .right
cell!.textLabel?.textAlignment = .right
cell!.textLabel?.text = filteredResults[(indexPath as NSIndexPath).row].title
cell!.detailTextLabel?.text = filteredResults[(indexPath as NSIndexPath).row].subtitle
cell!.textLabel?.attributedText = filteredResults[(indexPath as NSIndexPath).row].attributedTitle
cell!.detailTextLabel?.attributedText = filteredResults[(indexPath as NSIndexPath).row].attributedSubtitle

cell!.imageView?.image = filteredResults[(indexPath as NSIndexPath).row].image

cell!.selectionStyle = .none

在 pod 中的 SearchTextField.swift 文件中。

【讨论】:

  • 试过这个。它在代码中。也不起作用,因为标签是动态文本的宽度而不是父单元格,我认为
  • 我认为你应该先设置 textalignment 和 text to label。
  • 不确定我是否理解您提出的解决方案。能否请您发布我需要使用的代码?
  • 没有用。文本可能向右对齐,但标签向左对齐,标签的宽度正是文本值所需的大小。如果标签是单元格的宽度,这可能会起作用,但不确定如何将标签定义为父单元格的宽度
  • 宽度与 textalignment 无关,如果您将 textalignment 设置为 right 以更正标签它应该满足您的要求。
猜你喜欢
  • 2013-06-21
  • 1970-01-01
  • 1970-01-01
  • 2015-07-11
  • 1970-01-01
  • 1970-01-01
  • 2015-02-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多