使用Swift在表格单元格中的特定单词周围居中文本

时间:2016-04-12 15:49:14

标签: swift uitableview nsattributedstring text-alignment

我有一个带有多个单元格的UITableView,每个单元格都包含一个属性文本的句子。我想知道如何围绕这些句子中包含的特定单词将这些单元格中的文本居中。

结果将是单词的中心列,但每个单元格包含不同的周围单词。

我猜测开始的地方是将文本居中在单元格数组中,但是在" cellForRowAtIndexPath"委托方法行:

     func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{

    self.cell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "cell")


    if(cell.isEqual(NSNull)) {
        self.cell = NSBundle.mainBundle().loadNibNamed("cell", owner: self,        options: nil)[0] as! UITableViewCell;
    }
        self.cell.detailTextLabel?.text = self.synonymsCategories[indexPath.row]!
        self.cell.textLabel?.attributedText =     self.synonymsContext[indexPath.row]
        self.cell.detailTextLabel?.textAlignment = .Right

        self.cell.textLabel?.textAlignment = NSTextAlignment.Center
        self.cell.textLabel?.backgroundColor = UIColor.greenColor()
        return self.cell as UITableViewCell

}

但我没有对" self.cell.textLabel"做出任何改变。在运行时产生任何视觉变化。

有什么想法吗?任何帮助非常感谢。

菲利克斯

0 个答案:

没有答案