0

I am using Xcode 4.2 iOS 5.0 and also using storyboard. I have simple button and I connected that button with outlet.Now I am changing the button text from code.Problem is that if my string of button title is that "hey how are you?" then it is showing in button title "hey....u?" .Can anyone suggest me where I am doing wrong or missing something else?

![enter image description here][1]
This is IB Settings.
I just wrote btw.textLabel.text=textstr; and frame is (0,0,290,42) and text str is time as for e.g [NSDate date]

NSCry
  • 1,662
  • 6
  • 23
  • 39

5 Answers5

6

Your button width is smaller than the title string's length.that's why it truncates the middle part of the string. You can set the content mode to characterWrap in IB to make the title in two lines or you can change the font size to a smaller one to fit in your button width or you can change the button width to occupy the whole string. Prefer what is fine for you.

Abizern
  • 146,289
  • 39
  • 203
  • 257
Sree
  • 907
  • 5
  • 14
2

Try [myButton sizeToFit]; //I need to type 9 more characters to answer this question due to 30 char minimum.

dbrajkovic
  • 3,693
  • 1
  • 17
  • 14
1

I'm not sure if this already worked in XCode 4.2, but now it does ;)

You need to use the UIButton method setTitle:forState:

[self.myButton setTitle:@"Correct New Title" forState:UIControlStateNormal];

as this will correctly update the size and position of the buttons label. Where as just setting the title with self.myButton.titleLabel.text = @"Wrong New Title"; will not.

Credits to James Beith (see his Answer in UIButton - text truncated)

ndreisg
  • 1,119
  • 13
  • 33
0
myButton.textLabel.text = @"some very long title";
[myButton.textLabel sizeToFit];
iutinvg
  • 3,479
  • 3
  • 21
  • 18
0

FOR SWIFT 4,5

You need to use "setTitle" instead of "textLabel.text"

Button.setTitle("this is maximium line text come", for: .normal)
Sandu
  • 436
  • 4
  • 8