Showing posts with label multiline. Show all posts
Showing posts with label multiline. Show all posts

Sunday, October 8, 2017

Swift 4 NEW Tutorial-2 (Strings are Collections Again)

Prior to Swift 3, Strings are collection types, Now again in Swift 4 they are collections.

Swift 3 

-------------------------------------------------------------------------

var str = “Hello World!”
str.characters.count
str.characters.append(“appending….”)

-------------------------------------------------------------------------

Swift 4 

-------------------------------------------------------------------------

var str = “iOS Solves”
str.count        // 10
str.append(“ Blog”)      // iOS Solves Blog

-------------------------------------------------------------------------

Multiline strings :-

Swift 3

let name = “name1 \n name2 \n name3”

Output:

name1
name2
name3

Swift 4

Use 3 double quotes to specify it as a multiline string.

-------------------------------------------------------------------------

let name = “””
name1
name2
name3
“””

Output:

name1
name2
name3

-------------------------------------------------------------------------

Use spaces for indentation

-------------------------------------------------------------------------

let name = “””
  name1
  name2
  name3
“””

Output:

  name1
  name2
  name3

-------------------------------------------------------------------------

Hope this post is useful. Feel free to comment incase of any queries.


Saturday, November 19, 2016

Swift 3 UITableView Dynamic cell height based on content

Let’s take an example of chat application, where chat bubbles dimensions vary dynamically based on the chatted text.



We can achieve this dynamic size of cell by calculating size of cell based on displaying text font, size and length.This is very tedious. We can achieve this simply using autolayouts and UITableView automatic dimension setting for row height.

First, Take your label and give below constraints. Constraint constants can be of your choice.

  1. top
  2. leading
  3. trailing &
  4. bottom




After that make label as multiline by giving number of Lines as 0 and Line Break as 'Word Wrap'.

Lines - 0
Line Break - Word Wrap






In the code, give the below settings to UITableView.


  
        self.tableViewRank.estimatedRowHeight = 500
      self.tableViewRank.rowHeight = UITableViewAutomaticDimension


That’s it. Now, Table view’s cells will be of dynamic height based on the label’s content.





Hope this post is useful. Feel free to comment incase of any queries.