UITextView scroll only horizontally - iphone

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

Related

UITableView cell multiline justification

I have seen questions posted on here asking how to left and right align two lumps of text, but how can I neatly left and right justify an entire paragraph inside a multiline cell? (Such as what MS Word etc would do if you click on the justification button so that left and right sides of the text are always aligned).
You can also create a custom cell with label which IBOutlet to this class that can be downloaded on github OHAttributedLabel, then try the code below
cell.label.textAlignment = UITextAlignmentJustify;
Is this what you're looking for?
yourTextLabel.textAlignment = UITextAlignmentCenter;
Along with the frame size that you want for your text label.

How to remove the last empty line of UITextView?

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]];

NSString drawInRect with UILineBreakModeTailTruncation doesn't appear the "..."

I am trying to use this method to draw a string into a custom UITableViewCell.
[self.text drawInRect:TEXT_RECT withFont:font lineBreakMode:UILineBreakModeTailTruncation alignment:UITextAlignmentLeft];
The problem is that if the text is too long, the text is actually tail truncate but it doesn't display the "..."
If I use the drawInPoint
[self.text drawAtPoint:CGPointMake(60, 0) forWidth:200 withFont:font minFontSize:15 actualFontSize:nil lineBreakMode:UILineBreakModeTailTruncation
baselineAdjustment:UIBaselineAdjustmentAlignBaselines];
Then, I get the "..." but now it make all of my text one line, so the quite long text will get truncated too soon. For example:
If I have the text "Hello all, here is my first book". If I use drawInRect, then I can display it in 2 lines, but if I use drawAtPoint, I only see the first line like : "Hello all, here ..."
So, any help to make either method work will be appreciated
UILineBreakModeTailTruncation only truncates the last line of text. Is TEXT_RECT big enough to hold multiple lines of text? What appears to be tail truncation might just be a rect that isn't tall enough.

How to display multi-line text without scroll in UIView?

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.

Problem in displaying texts in each cell of the UITableView control on iPhone

I display texts in a cell of the UITableView control. Now the texts that I display can be large enough to fit inside one cell width, so it displays the texts on the second line. But it breaks the word in the middle, (like if say "That sounds great, I will pick you up from the abc station", it will display "That sounds great, I will pick you up from the abc sta" on the first line and "tion" on the second line. However, I wanted to be like "That sounds great, I will pick you up from the abc" on the first line and "station" on the second line).
Could you let me know how can I do that?
Thanks.
I think you can achieve this by setting the lineBreakMode on the UILabel.
// setup cell 3.0 SDK code
cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
cell.textLabel.text = #"this is some really long string....";