How do you limit UILabel characters in swift?
Asked Answered
V

4

7

I'm trying to set a limit in my label to 10 characters, I'm a complete newbie to programming in general so its my 3rd month so far... anyway thanks in advance :-)

Vlada answered 5/10, 2015 at 22:8 Comment(3)
Not even close to enough information and no code.Takamatsu
Since you've already set a limit on yourself how can you expect to set one on a UILabel?Jap
I think the question is pretty clear and don't need code just to limit characters on one label... It's not like I'm asking how to write a complicated function and then the code be necessaryVlada
S
9

If you want to limit the UILabel to just 10 characters then you just have to assign it with a text with length of 10. You can use NSString and NSRange to extract the text you need.

let str = "This is a Very Long Label"
let nsString = str as NSString
if nsString.length >= 10
{
  label.text = nsString.substringWithRange(NSRange(location: 0, length: nsString.length > 10 ? 10 : nsString.length))
}
Saveall answered 5/10, 2015 at 23:1 Comment(1)
Of course this code will crash if str has less than 10 characters in it to start with.Ill
H
15

You can also create a String extension and reuse it with any text string.. You can do it in the following way:

create the extension:

extension String {
   func maxLength(length: Int) -> String {
       var str = self
       let nsString = str as NSString
       if nsString.length >= length {
           str = nsString.substring(with:
               NSRange(
                location: 0,
                length: nsString.length > length ? length : nsString.length)
           )
       }
       return  str
   }
}

you can use the extension in the following way:

label.text = "This is a Very Long Label".maxLength(length: 10)

the previous code was tested in Swift 5.0

Hackman answered 12/4, 2019 at 18:7 Comment(0)
S
9

If you want to limit the UILabel to just 10 characters then you just have to assign it with a text with length of 10. You can use NSString and NSRange to extract the text you need.

let str = "This is a Very Long Label"
let nsString = str as NSString
if nsString.length >= 10
{
  label.text = nsString.substringWithRange(NSRange(location: 0, length: nsString.length > 10 ? 10 : nsString.length))
}
Saveall answered 5/10, 2015 at 23:1 Comment(1)
Of course this code will crash if str has less than 10 characters in it to start with.Ill
P
2

Check out this answer: https://mcmap.net/q/23464/-how-does-string-substring-work-in-swift

My version is (Swift 5+):

extension String {
    func shorted(to symbols: Int) -> String {
        guard self.count > symbols else {
            return self
        }
        return self.prefix(symbols) + " ..."
    }
}
Pawnbroker answered 28/9, 2021 at 10:15 Comment(0)
R
-2

SWIFT 3

        let str = GL_GetNews[indexPath.row]["n_body"].stringValue
        let nsString = str as NSString
        if nsString.length > 0
        {
             cell.newsDescription.text = nsString.substring(with: NSRange(location: 0, length: nsString.length > 200 ? 200 : nsString.length))
        }
Ricercare answered 6/8, 2017 at 10:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.