2016-05-17 36 views
1

我的swift APP有问题。Swift:如何居中细胞文本

在我TableViewCell功能我写

let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath:indexPath) as MyCell 
cell.setMyCell(self.data[0]["content"]!) 

如果我写

cell.textAlignment red line appear and say "textAlignment in unavailable: API deprecated" 
+0

大概你在你的单元格上有一个标签,并且你想对齐标签中的文本,而不是整个单元格... – Wain

+0

您是否阅读过'UITableViewCell'的textAligment文档:“Deprecation Statement instead instead分配给textLabel和detailTextLabel属性的UILabel对象的文本对齐。“将这个对齐应用到想要的'UILabel',而不是单元格。 – Larme

+0

我没读过。感谢您的意见。我解决它。 –

回答

8

使用雨燕4人正确的答案应该是:

cell.textLabel?.textAlignment = .center 
+0

我该怎么做才是有一个图像和一个单元格中的标签,我该如何居中? –

+1

您必须先将图片置于居中位置,然后在您的图片居中放入标签后添加限制 – Thomas

0

试试这个

MyLabel1.textAlignment = NSTextAlignment.Center 
0

我假设您在您的手机上使用了标签? 确保标签在单元格中居中,并给它一个足够大的宽度以满足您的需求。 然后你可以使用两种方法,您可以使用代码,对齐标签:

label.textAlignment = .Center 

,或者你可以在此使用界面生成器改变:

Alignment example in interface builder

希望这有助于!