Reputation: 150
I'm trying to do a custom table cell. I added a textfield in a view inside my cell and put some text in. But sometimes text inside get a line break casually. I think it was a problem of textfields so I replace with textView but the problem persist...Any ideas?
Here there is an image :
As u can see they get different line break and instead of fit the entire line casually they get a break and start a new line
Here's my code for putting text in textview:
var dataArray = [1 : "Università di Messina Università di Messina Università Università di Messina Università Università di Messina Università di Messina Università Università di Messina Università niversità di Messina orem ipsum dolo Lorem ipsum dolo Lorem ipsum dolo Lorem ipsum dolo Lorem ipsum dolo " ,2 : "Università degli studi di Giurisprundeza di Bologna di Roma",3 :"Università di Messina Università di Messina Università Università di Messina Università Università di Messina Università di Messina Università Università di Messina Università niversità ", 4 : "Università degli studi di Giurisprundeza di Bologna di Roma",5 :"Università di Messina Università di Messina Università Università di Messina Università Università di Messina Università di Messina Università Università di Messina Università niversità ",6 : "Università degli studi di Giurisprundeza di Bologna di Roma",7 :"Università di Messina Università di Messina Università Università di Messina Università Università di Messina Università di Messina Università Università di Messina Università niversità "]
var nomeArray = [1 : "asdarar awraw e awrawr awrawr hanno commentato questo elemento dsa asdasd" ,2 : "asdarar awraw e awrawr awrawr hanno commentato questo elemento dsa asdasd",3 :"asdarar awraw e awrawr awrawr hanno commentato questo elemento dsa asdasd",4 :"asdarar awraw e awrawr awrawr hanno commentato questo elemento dsa asdasd",5 : "Gianluca Materia e Davide Iuffrida hanno commentato questo elemento dsa asdasd " ,6 : "dauwdga awitawht awtiawt awitawhot awitahwot awiotha awaiaw awruhatio awitahoiwtahowtoaiwthoiawoitagiotiwagotagotawgiotaogitoasitvai",7 : "Gianluca Materia e Davide Iuffrida hanno commentato questo elemento"]
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! customCell
let yPositionHeader = cella.headerPost.frame.origin.y
let heightHeader = cella.headerPost.frame.height
let name = Array(nomeArray.values)[indexPath.row]
cell.proprietario?.text = name
cell.proprietario?.textAlignment = NSTextAlignment.Left;
cell.proprietario?.font = UIFont.systemFontOfSize(13.0);
cell.proprietario?.sizeToFit()
let text = Array(dataArray.values)[indexPath.row]
cell.miaLabel?.text = text
cell.miaLabel?.frame.origin.y = yPositionHeader + heightHeader + 30
cell.miaLabel?.numberOfLines = 0
cell.miaLabel?.textAlignment = NSTextAlignment.Left;
cell.miaLabel?.font = UIFont.systemFontOfSize(13.0);
cell.miaLabel?.sizeToFit()
let heightLabel = cell.miaLabel.frame.height
let yPositionLabel = cell.miaLabel.frame.origin.y
cell.azioniPost.frame.origin.y = yPositionLabel + heightLabel + 30
let heightActionP = cella.azioniPost.frame.height
let yPositionAzioniPost = cella.azioniPost.frame.origin.y
cella.separatorPost.frame.origin.y = yPositionAzioniPost + altezzaAzioniPost
let heightSeparatorPost = cella.separatorPost.frame.height
tabella.rowHeight = altezzaLabel + heightHeader + heightActionBar + heightSeparatorPost + 65
return cella
}
class customCell: UITableViewCell
{
@IBOutlet weak var headerPost: UIView!
@IBOutlet var miPiace: UIButton?
@IBOutlet var Condividi: UIButton?
@IBOutlet weak var miaLabel: UILabel!
@IBOutlet weak var azioniPost: UIView!
@IBOutlet weak var proprietario: UITextView!
@IBOutlet weak var nomeCondividente: UILabel!
@IBOutlet weak var separatorPost: UIView!
}
Upvotes: 0
Views: 1660
Reputation: 12053
This is what Auto Layout is for.
First of all, UILabel
is perfect for your use case, you don't need UITextView
or UITextView
. UILabel
can display multi-line text, you just have to set its numberOfLines
property to 0
and its Line Break Mode to Word Wrap
.
In your storyboard where you design your prototype cell (or in your cell's xib), I'm guessing you're not setting any constraints for the UILabel
's width. This means that Interface Builder will use the UILabel
's default width you have it in the storyboard, which does not always mean full screen width on all devices.
Upvotes: 2