0

I have a long text to display in TableCell. When it is word-wrapped to display in the 2nd line, is there any way to add the beginning margin in the 2nd line to be the same as the 1st line? See the screenshot below:

screenshot

the layout is a little bit messy when it's wrapped to the 2nd line.

I know the beginning margin of each long text to display, I use UITableView custom cell, I have set the label number of Lines to 0, and set Line Breaks to `Word Wrap.

byaruhaf
  • 4,128
  • 2
  • 32
  • 50
Rock Snow
  • 11
  • 3

3 Answers3

0

You should specify the placement of the label through a constraint and add the spacing there. You can attach it to the leading and the trailing edge of your parent container and add/subtract the margin there. This will scale nicely for all the sizes your app may encounter.

Patru
  • 4,481
  • 2
  • 32
  • 42
  • Thanks! I have add the cell margin there, but it seems not work. – Rock Snow May 21 '17 at 02:12
  • The accepted way to say "Thank you" for a correct answer on StackOverflow is to accept the answer :-) (It will gain you some points too) – Patru May 21 '17 at 02:14
  • Here is code: func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { if let cell = tableView.dequeueReusableCell(withIdentifier: "ResultsCell", for: indexPath) as? AcronymTableViewCell{ let result = totalResultList[indexPath.row] cell.layoutMargins = UIEdgeInsetsMake(0, -15, 0, 0) cell.resultLine.text = result cell.resultLine.numberOfLines = 0 cell.resultLine.lineBreakMode = NSLineBreakMode.byWordWrapping return cell } } – Rock Snow May 21 '17 at 02:19
  • Sorry, missed the "not" in your first comment. `cell.layoutMargins = UIEdgeInsetsMake(0, -15, 0, 0)` looks promising. However you should try to update your question with the code, more than a single line looks terrible in comments. Are you sure about the negative inset on the left? Have you tried a positive one? – Patru May 21 '17 at 15:29
  • There seem to be rather obscure interplay between `separatorInset` and `layoutMargins`, you might check out [this question](http://stackoverflow.com/questions/31537196/ios-9-uitableview-separators-insets-significant-left-margin) for some more hints on what to try. – Patru May 21 '17 at 15:33
  • Since you already subclass your `UITableViewCell` you should be able to override your `setFrame` method accordingly. – Patru May 21 '17 at 15:40
0

I would recommend this:

cell.resultLine.lineBreakMode = .byClipping
cell.resultLine.textAlignment = .right

I would also recommend that if the AcronymTableViewCell doesn't do much, normal cells have to ability to display text (through cell.textlabel?.text), and images (through cell.imageView?.image)

0

I've used

func tableView(_ tableView: UITableView, indentationLevelForRowAt indexPath: IndexPath) -> Int and layoutSubview on the TableViewCell

This two methods work well with tabelviewcontroller but not well under viewController + tableView.

Rock Snow
  • 11
  • 3