-1

I'm trying to automatically layout text on a UILabel view. The text (such as "abcdefghij") contains ten characters. I want to display it in one single line. I turned off the Size Class and Auto Layout for convenience, and added following codes to layout the text on the UILabel. It should be ten characters in one line, and the width of the UILabel is equal to the width of the device.

    let screenWidth = UIScreen.mainScreen().bounds.width
    labelView.frame = CGRect(x: labelView.frame.origin.x, y: labelView.frame.origin.y, width: screenWidth, height: labelView.frame.height)
    let string = "abcdefghij"
    let stringLength = CGFloat(string.characters.count)
    let characterSize = keyboardView.font.pointSize
    let characterSpacing = (screenWidth - characterSize * stringLength) / stringLength 
    let content = NSAttributedString(string: string, attributes: [NSKernAttributeName: characterSpacing])
    keyboardView.attributedText = content

But it turns out like this. The width of string is not equal to the screen

I think, the only could be wrong here is the pointSize. It equals to 13.8 while I set the font size to 17. I don't understand it. Give me some hints, please. Thanks for your attention.

By using sizeWithAttributes and boundingRectWithSize(_:options:context:), I finally figured out how it works. But my origin purpose is fitting the 10 characters in one line. The code should calculate the space between the characters, and all the space is same size. Could you give me some advices? This is what I want to make

JsW
  • 1,682
  • 3
  • 22
  • 34
  • So you want to calculate the width the characters take up? – ilovecomputer Jun 14 '16 at 12:14
  • 3
    `I turned off the Size Class and Auto Layout for convenience` -- turn them back on. If for no other reason than the convenience of not having to have a massive look-up table pairing font face + size to heights (and then still having to guess at the width). – nhgrif Jun 14 '16 at 12:23
  • The answer is "it depends on the characters". iOS uses proportional fonts, so a letter like 'I' takes much less space than a letter like 'W'. You can use `sizeWithAttributes` - http://stackoverflow.com/questions/24141610/cgsize-sizewithattributes-in-swift – Paulw11 Jun 14 '16 at 12:24

1 Answers1

1

Each character occupies different amount of space depending on the character, font and size of the font.

Hence, you can use boundingRectWithSize(_:options:context:) to predict size of the string at runtime, and then take action according to your requirements.

Alexander
  • 59,041
  • 12
  • 98
  • 151
7vikram7
  • 2,764
  • 1
  • 25
  • 43
  • Thank you! I know how it works now. But my origin purpose is fitting the 10 characters in one line. The code should calculate the space between the characters, and all the space is same size. Could you give me some advices about it? – JsW Jun 14 '16 at 15:24
  • How many such lines do you intend to show? Seems you need to use a collectionview instead of a label. With each character inside a cell of a collectionveiw – 7vikram7 Jun 15 '16 at 06:58