2

I want to split my dynamic UILabel text like given below e.g

UILabel text -

Aptitude tests English skills relevant to your requirements. It enables an organisation / institution to assess all four English skills – reading, writing, listening and speaking together with the core mandatory component (grammar and vocabulary) or test just one skill, e.g. reading.

to split into array of string with every line break even with every dimension of screen size whether iphone or ipad .

The result i want is array of strings -

["Aptitude tests English skills relevant to your requirements. It enables an organisation / institution","to assess all four English skills – reading, writing, listening and speaking together with the core ","mandatory component (grammar and vocabulary) or test just one skill, e.g. reading."]

With every line break in UILabel i need to split the string regardless to the dynamic screen size

Ajay Vyas
  • 81
  • 7

1 Answers1

1

Your approach can be hard, instead i recommend you to use other methods like using of sizeWithAttributes

extension String {

func widthOfString(usingFont font: UIFont) -> CGFloat {
    let fontAttributes = [NSAttributedString.Key.font: font]
    let size = self.size(withAttributes: fontAttributes)
    return size.width
}

func heightOfString(usingFont font: UIFont) -> CGFloat {
    let fontAttributes = [NSAttributedString.Key.font: font]
    let size = self.size(withAttributes: fontAttributes)
    return size.height
}

func sizeOfString(usingFont font: UIFont) -> CGSize {
    let fontAttributes = [NSAttributedString.Key.font: font]
    return self.size(withAttributes: fontAttributes)
    }
}

Assuming you know the width and your font-size in your label, you could use some logics like below:

    let inputText = "Aptitude tests English skills relevant to your requirements. It enables an organisation / institution to assess all four English skills – reading, writing, listening and speaking together with the core mandatory component (grammar and vocabulary) or test just one skill, e.g. reading."
    let labelWidth = UIScreen.main.bounds.width
    var resultArray:[String] = []
    var readerString = ""
    for i in 0 ..< inputText.count
    {

        readerString += inputText[i]
        //Check if overflowing boundries and wrapping for new line
        if readerString.widthOfString(usingFont: UIFont.systemFont(ofSize: 14)) >= labelWidth {
            resultArray.append(readerString)
            readerString = ""
        }
 }
Amir.n3t
  • 2,859
  • 3
  • 21
  • 28
  • Do you really believe that `inputText` is really an array who can assign its element to `readerString` according to loop. Have you ever tested this on playground? – Ajay Vyas Feb 22 '19 at 05:51
  • There are couple of String extensions that you can use to get this behaviour, I didn't paste them here since they are not related to your question. Have a look at this link that might help you: [link] (https://stackoverflow.com/questions/24092884/get-nth-character-of-a-string-in-swift-programming-language) – Amir.n3t Feb 22 '19 at 05:55