Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift : align UILabel text in the top rather in the middle

I want the UILabel to start from the top even if the text is short it seems that NSTextAlignment doesn't work

enter image description here

cell.textContent.text = comments[indexPath.row]
cell.textContent.textAlignment = 




func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    //post's section == 0

    if indexPath.section == 0 {
        let cell = tableView.dequeueReusableCellWithIdentifier("postCID", forIndexPath: indexPath) as! postCell
        cell.usernameLabel.text = "Steve Paul Jobs"
         cell.time.text = "9:42 PM"
         cell.commentsLabelCount.text = "12 Comments"
         cell.textContent.text = "Return the number of rows in the sectioReturn the number of rows in the sectioReturn the number of rows in the sectioReturn the number of rows in the sectioReturn the number of rows in the sectioReturn the number of rows in the sectio"

        cell.layoutSubviews()

    }

        let cell = tableView.dequeueReusableCellWithIdentifier("commentCID", forIndexPath: indexPath) as! commentCell
        // Configure the cell...
      cell.layoutSubviews()

    cell.usernameLabel.text = "Steve Paul Jobs"
    cell.time.text = "9:42 PM"
    cell.textContent.text = comments[indexPath.row]
     cell.textContent.textAlignment = NSTextAlignment.Left


        return cell



}


import UIKit

class commentCell: UITableViewCell {
    @IBOutlet weak var textContent: UILabel!
    @IBOutlet weak var time: UILabel!
    @IBOutlet weak var userImage: UIImageView!
    @IBOutlet weak var usernameLabel: UILabel!

override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
    userImage.layer.cornerRadius = 2
    userImage.clipsToBounds = true

}
override func layoutSubviews() {
    super.layoutSubviews()
    textContent.sizeToFit()
}
like image 613
James Moriarty Avatar asked Aug 03 '15 01:08

James Moriarty


People also ask

How do I align text to leading Swiftui?

To align a text view along the horizontal axis, you need to use . frame() modifier with maxWidth set to . infinity and alignment to the alignment you want.

How do I change my UILabel?

To change the font or the size of a UILabel in a Storyboard or . XIB file, open it in the interface builder. Select the label and then open up the Attribute Inspector (CMD + Option + 5). Select the button on the font box and then you can change your text size or font.

How do you add padding to UILabel?

If you have created an UILabel programmatically, replace the UILabel class with the PaddingLabel and add the padding: // Init Label let label = PaddingLabel() label. backgroundColor = . black label.


1 Answers

Using Auto Layout in Storyboard will be very simple:

enter image description here

and

label.numberOfLines = 0;
like image 136
isaced Avatar answered Nov 16 '22 00:11

isaced