1

I'm essentially just looking for a way to limit a UITextView to only a single line, and then have the width of the UITextView contentSize scale with the text in the textView without truncating anything. Any help would be greatly appreciated.

KHAAAAAAAAN
  • 161
  • 1
  • 7
  • Count the number of letters.Suppose in one line 50 letters can come. Make a check and do not add beyond 50 letters. Add an textchanging event ob textview where in that action it will keep track of number of letters inserted – Rajan Maheshwari Jun 29 '15 at 18:57
  • 3
    Why not use a UITextField instead? – David Skrundz Jun 29 '15 at 19:31

2 Answers2

2

I would use a UITextField and set "adjustsFontSizeToFitWidth = true". That would scale the text. If you still want to use a UITextView. You could try this:

 textView.textContainer.maximumNumberOfLines = 1
the_pantless_coder
  • 2,297
  • 1
  • 17
  • 30
  • I specifically don't want to scale the text size though. I want the size of the text view to expand horizontally, so then the user can scroll horizontally along the text. I have tried setting the maximum number of lines to 1, but then it just starts truncating text, or even more oddly the subsequent lines appear ABOVE the first few words of text. – KHAAAAAAAAN Jun 29 '15 at 21:12
  • I see, try this link http://stackoverflow.com/questions/50467/how-do-i-size-a-uitextview-to-its-content it should be able to help you solve your problem. – the_pantless_coder Jun 29 '15 at 22:04
  • @KHAAAAAAAAN Did you manage to solve your problem? If so and my answer helped please mark it as accepted. If not, post an update where you are stuck and I can try to help you solve it. – the_pantless_coder Jul 02 '15 at 16:26
  • 1
    I ended up changing my implementation to a UILabel. Like I said in my previous comment, this method didn't work because it would start truncating text and additionally text would appear above it in its frame. The label method works though, so its no problem. – KHAAAAAAAAN Jul 02 '15 at 16:53
  • @KHAAAAAAAAN Just wondering for my own knowledge. Why do you need a uilabel that will keep expanding(possibily off screen)? – the_pantless_coder Jul 02 '15 at 19:50
  • Creating an accelerometer controlled label where the text scrolls across the screen as controlled by the accelerometer data, so it doesn't actually matter if it extends beyond the confines of the screen at any given instance. – KHAAAAAAAAN Jul 02 '15 at 21:12
0

You can use uitextfield, it only has one line but if you insist on usint uitextview you can call,

txtView.sizeToFit();
kha96
  • 43
  • 6
  • When I use sizeToFit with the UITextView it just expands it vertically, not horizontally, which is what I want. – KHAAAAAAAAN Jun 29 '15 at 20:39