Multiple font sizes in a single UITableview cell (Swift) - swift

I have a UITableviewCell with multiple lines of text. I am trying to figure out how to make each line a different font size.
Here is the code I am using to populate the table cell:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier("eventCell")!
cell.textLabel!.textAlignment = NSTextAlignment.Center
cell.textLabel!.numberOfLines = 0
cell.textLabel!.lineBreakMode = NSLineBreakMode.ByWordWrapping
cell.textLabel!.text = self.eventsToday[indexPath.row].eventTitle + "\n" + self.eventsToday[indexPath.row].eventPlaceName
return cell
}
Is there a way to reduce the font size after the "\n" line break so that the string labeled "eventPlaceName" will show up as smaller than the one labeled "eventTitle?"

Use attributedText property of textLabel object.
Using attributedText you can format your text.
For more, see NSAttributedString

Is there a way to reduce the font size after the "\n" line break so
that the string labeled "eventPlaceName" will show up as smaller than
the one labeled "eventTitle?"
This sounds exactly like your table view cell needs TWO SEPARATE labels:
One for the "Event title"
One for the "Event place"
I mean, you could use an attributed string, but clearly these are two separate pieces of information and you have a repeating pattern here. Two labels sounds like the best practice / less headaches down the road, in my opinion.
How to add an extra label:
Subclass UITableViewCell (Let's call the custom subclass CustomTableCell in the code below).
Set the class of your prototype cells to your custom subclass, in the storyboard. Also set the reuse identifier (here I used "YourCellIdentifier").
Forget the original label (it doesn't show in the storyboard and therefore you couldn't position your second label relative to it); instead add TWO new labels to the cell, and setup outlets with the custom cell subclass. Name the properties (e.g.) eventTitleLabel and eventPlaceLabel. Setup any layout constraints you may need, and configure the font sizes, colors, etc.
At runtime, set the labels' text like so:
let cell = tableView.dequeueReusableCellWithIdentifier("YourCellIdentifier", forIndexPath: indexPath) as! CustomTableCell
// (forced cast shouldn't fail f identifier is registered properly)
cell.eventTitleLabel.text = "Event Title XXXX"
cell.eventPlaceLabel.text = "Event Place YYYY"
return cell

Related

Cannot Change Text of Label in TableView

I have a tableView which displays the address and phone number of a business. I want to use a label next to the address and phone number. For some reason, all of my labels say "Address" even though I have the phone label set as "Phone" in the storyboard and in the code. Why is it doing this?
How it looks in the storyboard
How it looks when I run it
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! ProfileCell
cell.addressLabel.text = "Address:"
cell.phoneLabel.text = "Phone:"
cell.addressInfoLabel.text = objectsArray[indexPath.section].sectionObjects[indexPath.row]
cell.phoneInfoLabel.text = objectsArray[indexPath.section].sectionObjects[indexPath.row]
return cell
}
Remove following lines
cell.addressLabel.text = "Address:"
cell.phoneLabel.text = "Phone:"
My guess is you have set outlet reference to addressLabel, what should have been referred to phoneLabel. Check that.
I checked your code. The problem is with height of the labels. Address label is overlapping phone label. Here are the things you can do
Change the background colors of the address label and phone label. You will realise that address label is completely hiding phone label.
Remove aspect ratios for the labels and give some constant height.e.g. 30
Implement heightForRow for tableView and give some height e.g. 150.

How to turn on editing in cell Swift 3

I have a question about cell in swift. Let's imagine that I have a cell with 20 rows. The cell is selectable right now, but the thing I want is that, when user selects 20 row it should be editable. And the value which enters the user should be saved in some variable. Is it somehow possible? Thank's in advance!
If you want 20 cells in tableView, then
You have to make nib of a cell, and make textfield in cell, and also create outlet in nib, (you can also create prototype cell)
Then in cellForRow AtIndexPath makes textField delegate as self (to class), in that class you have to implement textField delegate, and then you can access textFields as:
let indexPath = IndexPath(row: 0, section: 0)
let cell = tableView(tableView cellForRowAt: IndexPath) as! MYCELL
let text = cell.textField.text
Sorry for my bad english,
I think it will be helpful

Swift - read above cell value in tableview

Basically I am trying to get a value from a custom table view cell, as I have made 5 prototype cells - each with a different tableViewCell class. However, what I am trying to do is to read the data from the cell above. I have called my classes: TableViewCell1, TableViewCell2 etc... So on the cellForRowAtIndexPath, I have the code like this:
if indexPath.row == 1 {
let cell2 = tableView.dequeueReusableCellWithIdentifier("secondCell", forIndexPath: indexPath) as! TierCell2
return cell2
}
This works, but I have also tried to do it like this, to be able to access the value above (with no luck) - which only results in an optional value error.
let cell1:TableViewCell1 = TableViewCell1()
cell2.PriceText.text = cell1.PriceText.text
Any ideas on how I might accomplish this?
I suggest you then try creating one custom table view cell with the 5 price tier labels that you want to display. Then in the cellForRowAtIndexPath you just deque a cell as the custom class and fill in all the tier information that you already have in your dictionary and return that cell.

How to set the table view cell to adjust to label?

I am using Xcode to make an app. Using auto layout, I have a tableview contained in a container view which is part of a view controller embedded un a navigation controller.
This table view is my content page, so the individual tableview cells leads to another tableview which contains bulk of the information I would like to display.
I added UIlabel which contains a paragraph of words to each tableview cells and set the constraints to the respective tableview cells.
In the storyboard, it looks alright having to be able to see all the paragraphs. I have set the line to be 0, and word wrap.
When I run the programme with lets say 4s simulator, the table view cells do not display the whole paragraph but rather, just indicate "..." at the end of each paragraph.
How do I set the settings such that the tableview cells will adjust itself according to the UIlabel after being word wrapped giving a certain screen sizes of phone?
PS: I am new to SWIFT programming and thanks for taking your time to help me.
Have you done this?:
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
This is required for self-sizing table view cells. Also make sure of the following things:
a) The number of lines of UILabel is set to 0
b) The autolayout system in the table view cell is proper -- all the views are pinned on all sides and the autolayout can accurately determine the height of the cell.
c) Also do a tableView.estimatedRowHeight = 75 (or your estimated height) for efficiency.
add this two line in viewDidLoad Method and label number of lines 0 and dont give any height.
func viewDidLoad() {
super.viewDidLoad()
tableView.estimatedRowHeight = 44.0
tableView.rowHeight = UITableViewAutomaticDimension
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! CustomCell
//cell.customTexLabel Number 0f lines 0 and dont give height contrains just Leading,Trailing,TOP and Bottom.
cell.customTextLable?.text = "Here index starts from 0 which means first element can be accessed using index as 0, second element can be accessed using index as 1 and so on. Let's check following example to create, initialize and access arrays:Here index starts from 0 which means first element can be accessed using index as 0, second element can be accessed using index as 1 and so on. Let's check following example to create, initialize and access arrays:Here index starts from 0 which means first element can be accessed using index as 0, second element can be accessed using index as 1 and so on. Let's check following example to create, initialize and access arrays:"
return cell
}
Hi #Daniel you can learn how to create self sizing cell here is the demo tutorial for custom dynamic cell
http://developerclouds.com/2016/03/23/table-view-custom-cell-with-self-sizing/

Use tags for tableView inside UIViewController to update Labels

I added a table view to my UIViewController. then i added a label to my custom cell inside the table view. i tried to update labels inside the tableview by using tags. seems it does not working.
this is my code.
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell: UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("LineItemCell", forIndexPath: indexPath)
let object = lineItemsObject[indexPath.row]
//cell.textLabel?.text = object["quantity"]
print(object["description"]!)
if let descriptionLabel = cell.viewWithTag(500) as? UILabel {
descriptionLabel.text = object["description"]
}
else
{
print("fail")
}
return cell
}
When i call the function always it does not read the tag and prints "fail". i have assigned correct tag value to my label also.
here i have attached a image of my label details in attribute inspector
Please help me the fix the issue.
As #TheAppMentor said code looks fine. Make sure you entered correct cell identifier. Then make sure you connected your view controller to correct class (it's my common mistake). Also make sure that something else in this cell hasn't the same tag. You can also print all subviews to check what's inside.
Your cell is not knowing about your label inside (the one with 500 tag). You are not providing a custom cell, because of this line:
let cell: UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("LineItemCell", forIndexPath: indexPath)
This is a predefined cell. As of documentation you can have predefined cells or custom ones (obtained by subclassing UITableViewCell):
"When creating cells, you can customize them yourself or use one of several predefined styles. The predefined cell styles are the simplest option. With the predefined styles, the cell provides label and image subviews whose positions and styling are fixed. ... To set the text and images of the cell, use the textLabel, detailTextLabel, and imageView properties."
If you want to go predefined:
If you want to just put the text onto cell use (I see this one is commented on first place).
cell.textLabel?.text = object["quantity"]
Go custom
Extend UITableViewCell on a separate swift file. Do your bindings here and work with your storyboard in parallel. Assign the custom class to your cell. Also, change dequeue:
let cell: MyCustomTableViewCell = self.tableView.dequeueReusableCellWithIdentifier("LineItemCell", forIndexPath: indexPath) as! MyCustomTableViewCell