I wish to make a horizontally scrollable uitextview in which a user can type text that is much longer than the width of the textview.
But currently, when i have implement a textfield, the text stops at the end of the textview, even when i continue to type and setting textContainer.maximumnumberoflines = 1.
I wish it to have the same vertical scrolling function where the height textview expands while users types more text onto the textview. But in my case, i want the width of textview to expand to accommodate words.
At the end of the long text, i wish to be able to scroll the textview horizontally to view the full text.
The current failed attempt by me looks like this.
currently, the word stops at "hahahh", no matter how much i type, nothing changes.
textContainer.maximumnumberoflines = 1 ----> This is not required (By default textField has scrolling property). You can initialize textField like this and it works. Here specify your textField frame, superView to which this textField has to be added, color and font.
UITextField *myTextField = [[UITextField alloc] initWithFrame:frame];
myTextField.font = giveFont;
myTextField.textColor = giveColor;
[superview addSubView:myTextField];
You can use TextView instead of TextField.
TextView is located just above the ScrollView in Object Library.
Check this out
Related
I have UILabel (in UIScrollView) with
self.testlabel.numberOfLines = 0;
self.testlabel.sizeToFit()
and text with 26455 characters, when I set text to label :
self.testlabel.text = content
I don't see my text, but if text is much smaller than 26455 characters then all fine.
So, how to place a big text in XCode?
Check whether the label height constraint is fixed.
Try adjusting the scrollview content size depending on the text height.
I want to add a "read more"-button at the end of the textview text. The text is in a UITextView and the button is a UIButton. So first I would like to make the button align vertically with the textview last line.
I'm getting the size of the text views text container in layoutSubviews (this is happening in a UICollectionReusableView):
let containerSize = descriptionTextView.layoutManager.usedRectForTextContainer(descriptionTextView.textContainer).size
showAllButtonHeight.constant = containerSize.height
I'm using Autolayout. I have a constraint (showAllButtonHeight) defining the y-value of the button defined like this:
How can i make the button align perfectly with the text? Or is there a better way of making this kind of "read more"-button?
My iOS app is not showing long attributed strings. I have a cell in a tableview which contains this textView. When the text is very long the tableview is unresponsive for a while but when it loads the text is not shown. All other cells are displayed fine. And the textView works fine with small text strings.
Here's the code:
descriptionCell = [tableView dequeueReusableCellWithIdentifier:#"CellAdDetailDescription"];
descriptionCell.bodyTextView.delegate = self;
NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:self.ad.body];
UIFont *cellFont;
cellFont = [UIFont fontWithName:#"HelveticaNeue" size:16.0];
NSDictionary *attributesDictionary;
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineSpacing = 10;
attributesDictionary = #{NSParagraphStyleAttributeName : paragraphStyle , NSFontAttributeName: cellFont};
[str addAttributes:attributesDictionary range:NSMakeRange(0, str.length)];
descriptionCell.bodyTextView.attributedText = str;
I pasted the long string here. I debugged and str is being loaded fine containing the desired text.
Whats wrong here?
What is the max allowed string length in UITextView?
EDIT: very odd, when trying selection in the textView, the text is being shown in the magnifying glass. I posted a video here.
Is it a bug in UITextView?
Here is the screenshot. The blank white at the bottom is the textView.
It could have something to do with the scrolling. If you are showing all the text (i.e. the text view is expanded to be as high as it needs to be, and so is the table view cell), the scrolling is done by the table view. If the text view is smaller, you have to scroll to see all the text - this might cause a conflict with the table view, which is also a scroll view.
It has been suggested that you disable the scrolling of the text view before adding the attributed text and reenable it afterwards. If you are showing the whole text in the table view, you can leave the text view scrolling disabled. In some cases, it will only work if scrolling is enabled. You should check this possibility as well.
I also had this issue (= very long attributed text didn't show up in the UITextView within an autosized UITableViewCell). I tried all accepted answers from here and from this question: UITextView not loading/showing the large text?. None did work.
However I then found out that - in my case - it just works fine on the device. So if you're still struggling with this kind of problem, don't rely on the iOS Simulator.
The reason that you UITextView not show all text because it Frame is too small so it truncates the text to fit with its frame. you can do follow step to show all text:
In your CustomUITableCell, override layoutSubView:
Use this function to calculate size of TextView that fit it content
[textView sizeThatFits:CGSizeMake(textView.frame.size.width, CGFLOAT_MAX)].height
After that, calculate and set new frame for TExtView (in uitableCell custom)
In the tableView:heightForCellAtIndexPath, calculate new size of textView (also height of cell) similar like above and return right height for cell.
At the moment in my IB I have a View Controller which is covered by a UIScroll View. Within the scroll view I have a UIImageView at the top, a UILableView in the middile and a MKMapView at the bottom. The UILableView number of lines is set to 0 (infinite) and word wrap allowing me display as much content as I want.
I want to be able to tap telephone numbers and website url's for the content in my UILableView. The best way I've found so far is to change it to a UITextView which handles all of this for you. However... I can not get the same behaviour with the scrolling.
Before the image, label and map used to scroll as a block. Now, only the textView scrolls. Any advice appreciated.
the displaying part is correct, that is calculate the frame of the textView based on the size of its text for scrolling add this [textView setScrollEnabled:NO];
You can try using this project:
https://github.com/mattt/TTTAttributedLabel
label.dataDetectorTypes = NSTextCheckingTypeLink; // Automatically detect links when the label text is subsequently changed
label.delegate = self; // Delegate methods are called when the user taps on a link (see `TTTAttributedLabelDelegate` protocol)
label.text = #"Fork me on GitHub! (http://github.com/mattt/TTTAttributedLabel/)"; // Repository URL will be automatically detected and linked
NSRange range = [label.text rangeOfString:#"me"];
[label addLinkToURL:[NSURL URLWithString:#"http://github.com/mattt/"] withRange:range]; // Embedding a custom link in a substring
I have a UITextView containing some text that fills the whole width without overflowing.
I'm reducing the width of the UITextView by changing its frame in a block animation, such that the decreased width causes the text to overflow and the end to be replaced with an ellipsis ('...').
Currently, the text does not respond smoothly to the changing width. It jumps from the full width case to the truncated '...' case without anything in between, and the same when animating back to the full width.
Is it possible to force the text to change and the ellipsis to be introduced/removed smoothly as the frame animates?
You should use the answer here: UITextView animating change of frame does not animate text reallocation
The text in textviews doesn't always act as expected. For this you'll have to set up a NSTimer and set the frame size on every tick.
Do something like:
textview.frame = CGRectMake (textview.frame.origin.x, textview.frame.origin.y, textview.frame.size.width-1, textview.frame.size.height-1);
Then when it's done I would completely remove the textview from superview and set it to nil, and then init a new one. The reason for this is that when changes the frames of textviews for some reason padding is added around the text's box. You won't notice it unless you change the frame a probably at least 100 times.
[textview removeFromSuperview];
textview = nil;
textview = [[UITextView alloc] initWithFrame:CGRectMake(x, y, width, height)];
textview.text = yourTextString;
//You will also have to set whatever non default properties you want, such as text or background color
[view addSubview:textview];