Skip to content

Instantly share code, notes, and snippets.

@tkc
Last active August 7, 2016 06:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tkc/836c08f6aa189b495af3f8fa45ae1f54 to your computer and use it in GitHub Desktop.
Save tkc/836c08f6aa189b495af3f8fa45ae1f54 to your computer and use it in GitHub Desktop.
import UIKit
class CustomTableViewCell: UITableViewCell
{
var contentText:String = "";
var contentLabel = UILabel();
override func awakeFromNib() {
super.awakeFromNib()
}
override init(style: UITableViewCellStyle, reuseIdentifier: String!)
{
super.init(style: style, reuseIdentifier: reuseIdentifier)
self.contentLabel.translatesAutoresizingMaskIntoConstraints = false
contentLabel.backgroundColor=UIColor.cyanColor()
contentLabel.font = UIFont.systemFontOfSize(22)
contentLabel.sizeToFit()
contentLabel.numberOfLines = 0
self.addSubview(contentLabel);
self.ajustAutoLayout()
}
func ajustAutoLayout() {
let top = NSLayoutConstraint(item: self.contentLabel,
attribute: NSLayoutAttribute.Top,
relatedBy: NSLayoutRelation.Equal,
toItem: self,
attribute: NSLayoutAttribute.Top,
multiplier: 1.0,
constant: 30)
let bottom = NSLayoutConstraint(item: self.contentLabel,
attribute: NSLayoutAttribute.Bottom,
relatedBy: NSLayoutRelation.Equal,
toItem: self,
attribute: NSLayoutAttribute.Bottom,
multiplier: 1.0,
constant: -30)
let left = NSLayoutConstraint(item: self.contentLabel,
attribute: NSLayoutAttribute.Leading,
relatedBy: NSLayoutRelation.Equal,
toItem: self,
attribute: NSLayoutAttribute.Leading,
multiplier: 1.0,
constant: 50)
let right = NSLayoutConstraint(item: self.contentLabel,
attribute: NSLayoutAttribute.Trailing,
relatedBy: NSLayoutRelation.Equal,
toItem: self,
attribute: NSLayoutAttribute.Trailing,
multiplier: 1.0,
constant: -50)
self.addConstraint(top)
self.addConstraint(bottom)
self.addConstraint(left)
self.addConstraint(right)
}
func setContentLabelText(text:String){
let textFontStyle: String = "Helvetica"
let textFontSize: CGFloat = 20.5
let textFont = UIFont(name: textFontStyle, size: textFontSize)!
let textStyle = NSMutableParagraphStyle()
textStyle.lineSpacing = 18.0
textStyle.paragraphSpacing = 18.0
textStyle.alignment = NSTextAlignment.Justified
let attributes: Dictionary = [NSParagraphStyleAttributeName: textStyle, NSFontAttributeName: textFont]
let attibute = NSAttributedString(string:text, attributes: attributes)
self.contentLabel.attributedText = attibute
}
required init?(coder aDecoder: NSCoder) {
fatalError("error")
}
}
class CustomTableSandBox: UIViewController, UITableViewDelegate, UITableViewDataSource {
private var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
tableView = UITableView(frame: CGRect(x: 0, y:0, width: self.view.frame.width, height:self.view.frame.height))
tableView.dataSource = self
tableView.delegate = self
tableView.estimatedRowHeight = 500
tableView.rowHeight = UITableViewAutomaticDimension
tableView.registerClass(CustomTableViewCell.self, forCellReuseIdentifier: "customCell")
self.view.addSubview(tableView)
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("customCell", forIndexPath: indexPath) as! CustomTableViewCell
let text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";
cell.setContentLabelText(text);
cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator
return cell
}
override func viewWillAppear(animated: Bool) {
tableView.reloadData()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment