For some reason, UITextView produces an empty line at the end which causes it to be scrollable for nonsense when the text actually fits perfectly into the bounds.
I figured out that this additional space added to the bottom of the text in the contentView of UITextView is almost exactly the font size. So it is an additional line of text.
I want to remove that line so the UITextView is not scrollable when there is no reason to scroll.
editable is set to NO.
I tried setting the contentSize property with a reduced height but this has no effect.
The string has no whitespace or return character at the end. It comes from UITextView. Tested with a lot of different strings.
You can try altering the contentInset property and set the bottom inset to -10. I personally think it would be a hack but it worked when I tested it so just check what the correct inset would be.
textView.text = [textView.text stringByTrimmingCharactersInSet:[NSCharacterSet whiteSpaceAndNewLineCharacterSet]];
Related
I am programmatically writing to a UITextView a comma separated string.
Example: NSString* str = #"cat,dog,big dog,kitten".
On [textView setText:str]; I get the results as:
cat,dog,big
dog,kitten.
I don't understand why is the space getting converted to a line break. Any idea?
the space gets converted to a line break, because the text did not fit into one line. This line break mode cannot be disabled, or changed. But you can rise the width of the textview.
I have a UITextField that is 300px width and 270px height. When I want to edit it, the cursor goes automatically in the middle and I can only write on the middle line.
How can I set this UITextField to have severals lines and a cursor starting at the top-left ?
Simple answer, just use UITextView instead.
UITextfield is designed for single line only, If you want to enter multi line text than use UITextView instead and set its editable property to YES than it would behave like textfield!
[myTextView setEditable:YES];
Note: You can also set it via Interface Builder, select the "editable" box & It will be multiline by default.
I have a UILabel with a dynamic string as the text property. If I always want the first word to be underlined, do I need to separate labels and then try to line them up correctly? Or could I subclass UILabel to have it do this?
Use a UIWebView and render the text as HTML.
I ended up using the textRectForBounds:limitedToNumberOfLines: to find the dynamic start point of the word (minus some x coordinate pixels), and then took away the first letter of the uilabel with stringByReplacingCharactersInRange:withString:, and then added another uilabel with the beginning letter of the original label, just with different font.
I have one line of text that is too long to be displayed in one single line in a UILabel, so I switched to a UITextView. I wanted to display that text still in one line, and be able to scroll horizontally, but instead, the UITextView will wrap the text into multiple lines, and allow me to scroll vertically. So, for the text "This is a very very long line", instead of displaying it like "This is a ", and then if scrolled horizontally, it would display the rest of it, it actually displays it like "This is a \n very very long \n line". How can I get the behavior I want?
Thanks,
Mihai
If you want that behavior you should place the label inside a scrollview. That's the only way you'll be able to scroll a label in any direction.
Using Merrimack's advice, this worked for me:
self.dishNameLabel.text = #"Here is some very longgggg text to test with";
float width = ceil([self.dishNameLabel.text sizeWithAttributes:#{NSFontAttributeName: [UIFont fontWithName:#"WorkSans-SemiBold" size:15.0]}].width);
self.dishNameLabel.frame = CGRectMake(self.dishNameLabel.frame.origin.x, self.dishNameLabel.frame.origin.y, width, self.dishNameLabel.frame.size.height);
self.dishNameScrollView.contentSize = CGSizeMake( width, self.dishNameScrollView.frame.size.height);
Of course, you will have to replace your text and font with your respective specs
I have multi-line non-HTML text (with newlines). Need to output it inside ScrollView or just UIView.
I can't output it as UITextView: can't find how to resize UITextView to the size of text and/or disable scroll in it.
Should I output text to Label?
Or there's something more proper for that?
Thanks.
Try using a UILabel, then set the lineBreakMode property to UILineBreakModeWordWrap, and the numberOfLines property to zero (unlimited lines).
Depending on the style of text you're using, you might try stripping out the newlines so the result looks better.