0

I want to explain my question with codes. First you can see the custom label codes in the below;

let resultLabel : UILabel = {
    let lbl = UILabel()
    lbl.textColor = .white
    lbl.textAlignment = .right
    lbl.text = "0"
    lbl.font = UIFont(name: "Helvetica Neue", size: 35)
    lbl.minimumScaleFactor = 0.75
    lbl.lineBreakMode = .byClipping
    lbl.adjustsFontSizeToFitWidth = true
    return lbl           
}()

I just created label with these lines of codes. I also give them specific height according to view's frame height size and padding left and the right you'll see the constraint codes in the below.

resultLabel.anchor(top: view.topAnchor, left: view.leftAnchor, bottom: nil, right: view.rightAnchor, paddingTop: view.frame.size.height / 7, paddingLeft: 2, paddingBottom: 0, paddingRight: 5, width: 0, height: view.frame.size.height / 7)

I'll assign value into UILabel according to clicking button so i want to set chracter limit for the UILabel. According to code that i written it cropped data as i wanted but it didn't do that same action in the value. How can achieve of that could you help me out? Thanks.

Esat kemal Ekren
  • 516
  • 2
  • 8
  • 28
  • `it cropped data as i wanted but it didn't do that same action in the value.` - can you explain this more? – Losiowaty Mar 22 '17 at 06:54
  • Of course, let me explain. I see the 12345 in my screen because of the limit but when i inspect to value i could see 1234567 ( last 2 digit i clicked more the the test it will not shown on the screen but i will added in the value i need to cropped like in the screen value ) – Esat kemal Ekren Mar 22 '17 at 06:57

1 Answers1

0

What you want is not possible out of the box, and would be really strange in my opinion. Now the data and its presentation are separated, as they should be. Imagine, if your view controller size changed (the device got rotated, or your app is run on iPad in split mode, and the user resized the apps) - then you would be able to see more of your data. Because the label keeps the original data, everything would happen automatically. If it cropped the data to match its rendered representation, you would need to react to all such changes and reassign the original data to the label.

To provide a real world example - imagine you are looking at a huge (building sized) billboard, through a small (lets size, letter paper sized) window. You can see only a portion of the billboard, but everything is still there. In the same way, a UILabel is just a "window" onto the data provided.

Now, implementing this won't be trivial. Such a UIQuantumLabel* would need to take into consideration the width of each letter/digit/emoji/character in the data for current font at its current size (unless you were to limit it to monospaced fonts), take into consideration whether the automatic font scaling has happened and at what magnitude, whether the user has decreased/increased the font size in devices accessibility settings, and probably some more factors.

What is your use case for this? Why would you want to crop your data, and not keep the original?


*UIQuantumLabel because it reminded me of quantum mechanics (at least what I remember of it), where observing an object changes it state :)

Losiowaty
  • 7,911
  • 2
  • 32
  • 47
  • Ok let me explain something like these, If you inspect to ios calculator in iphone 7 it takes 9 digits, i couldn't write more in the screen also value too because when i wanted to multiply something it'll ignore out of the screen data. Could you approach this look? – Esat kemal Ekren Mar 22 '17 at 07:30
  • Well, its easy to limit the number of characters you can **input** into a `UILabel/UITextField/UITextView` using some kind of keyboard, and working one by one, and there is a lot on tutorials on how to do it - [like this one](http://stackoverflow.com/questions/433337/set-the-maximum-character-length-of-a-uitextfield). Now if you wanted to limit your `UILabel` to, for example, 5 characters, you should first prepare your data accordingly. The `UILabel` (or any other view) should be as dumb as possible. All your logic, should happen elsewhere and the result be simply presented on screen. – Losiowaty Mar 22 '17 at 07:34
  • actually my uilabel gets value according to buttons tag like in the calculator i'm not using keyboard or something like that. So according to device screen of course label width will be extend but according to that i need to keep data in the frame value and visual.. – Esat kemal Ekren Mar 22 '17 at 07:46
  • Well, to point you in the right direction - you should check your string `length` before appending next digit. If it is shorter then your max possible length, you can append. – Losiowaty Mar 22 '17 at 07:55