UITextView text Disappears - ios

I am using UITextView to display the NSAttributedString (Which Contains NSTextAttachment and HTML tables using NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType).
When I scrolling the UITextView at half of the screen the texts are disappearing.
Can any one explain how it happens? and how to resolve it?
UITextView *textView = [[UITextView alloc]init];
textView.frame = CGRectMake(0, 0, 768, 1024);
textView.editable = NO;
textView.selectable = NO;
textView.attributedText = attributedString; //My AttributedSting
[self.view addSubview:textView];

This is probably because of incorrect size of the textview frame. Change the background colour of the textview to see if the frame has been set appropriately.

Try making sure no constraints are created automatically by adding:
[textView setTranslatesAutoresizingMaskIntoConstraints:NO];
Ideally you would be best to use constraints so that the text view is pinned on its leading edge and trailing edge to the containing view and on its bottom edge and top edge to the bottom layout guide and top layout guide respectively.

Related

UITextView not scroll properly when using TextKit

I config a textView in a UIViewController like following:
textView configuration
but when controller viewDidAppear I found that UITextView's contentSize = {375, 242} and UITextView can not scroll.
But if i tap the textView, let the textView begin editing (but edit nothing), then i touch the controller's view let textView endEditing, log the textView, this time contentSize = {375, 361} and UITextView can scroll.
Is anybody know why? Thanks.
You can add textView something like,
UITextView *standardTextView = [[UITextView alloc]initWithFrame:CGRectMake(20, 50, self.view.frame.size.width - 40, 120)];
standardTextView.layer.borderWidth = 1.0;
standardTextView.layer.borderColor = [[UIColor lightGrayColor]CGColor];
standardTextView.delegate = self;
standardTextView.font = [UIFont fontWithName:#"Helvetica" size:17.0];
[self.view addSubview:standardTextView];
then when your content(i.e text) will be bigger than textview's height it will enable scroll otherwise it remains disable!!
NSTextContainer has a property called heightTracksTextView which may be interfering with your setup here, as the default is false.
I would double check the height of the NSTextContainer after you initialize the UITextView and after you add the UITextView to the view hierarchy.
Check the documentation here: https://developer.apple.com/reference/uikit/nstextcontainer/1444559-heighttrackstextview
If that doesn't help, let me know, I know I've solved this issue before, but I'm not at my computer right now.

How to show Dynamic UITextView Text Height

Hi have some product description, and data coming from the API,i want to show the data as dynamically. Dependents on this height i need to add some other UIElements please help me
Thanks in Advance
UILabel and UITextView has a method: "sizeThatFits", it can return the size fit the text base on a given size, so all you need to do is:
UITextView *textView = [[UITextView alloc] init];
textView.text = yourContent;
CGSize fitSize = [textView sizeThatFits:CGSizeMake(contentWidth, 10000)];
CGFloat contentHeight = fitSize.height;
Try this
UITextView *textView = [[UITextView alloc] initWithFrame:<set frame>];
textView.text = <text>
[textView sizeToFit]; // calls sizeThatFits: with current view bounds and changes bounds size.
If you don't need text editing and aim code purity, you can use UILabel instead of UITextView, just use AutoLayout and set constraints as shown below. Set Lines to 0 for Expanding Label. If you do everything right, resizing and offsetting of controls will occur automatically, you just can change text in Expanding Label

How to add a UILabel inside a UIScrollView programatically?

Ok i have already gone through similar questions, but none of them helped.
I want to add a UILabel inside a UIScrollView so that the Label can be scrolled if the contents are large. Here is my code:
ViewController.h:
#interface ViewController : UIViewController
{
UILabel *myLabel;
UIScrollView *myScroll;
}
ViewController.h:
myLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y+30, self.view.frame.size.width, self.view.frame.size.height)];
myScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y+30, self.view.frame.size.width, self.view.frame.size.height)];
myLabel.backgroundColor = [UIColor yellowColor];
myLabel.text = #"Large random text";
[myLabel setNumberOfLines:0];
myLabel.lineBreakMode = NSLineBreakByWordWrapping;
[myLabel sizeToFit];
myScroll.contentSize = CGSizeMake(myLabel.frame.size.width,
myLabel.frame.size.height);
[myScroll addSubview:myLabel];
[self.view addSubview:myScroll];
I searched a lot on the internet but could not find a answer, can someone let me know what the issue is ?
Thank You !
I'm not sure how to do that, but I would like to suggest an alternative that may suit your needs. Use a UITextView, but set " [textView userInteractionEnabled:NO] " and it will act as a label, since it cannot be edited. It might end up looking like how you want.
Based on your comments, you need to size the label as needed so it is big enough for the given text. Then add the label to scroll view. Then set the scroll view's contentSize so it fits the whole label. This will ensure the scroll view allows you to scroll to see the whole label.
In addition to this, make sure the label's frame is relative to the scroll view and not to self.view. In other words, setting the label's frame's origin to 0,0 will put the label in the top left corner of the scroll view regardless of the position of the scroll view relative to its parent.
Try by replacing your contentSize like below code,
myScroll.contentSize = CGSizeMake(self.view.frame.size.width,self.view.frame.size.height + 100);
Hope it will work for u.
1.You have to use View(ContentView) to place the labels.
2.Add the ContentView into ScrollVIew.
3.Assign the ContentView Size whatever You Want
Go Through This Link,You Will get a Clear idea.
https://www.youtube.com/watch?v=UnQsFlMGDsI
Remove scrollView = [[UIScrollView alloc] init]; this is not necessary.Please Upvote if it helps.

How do I set a UITextView as an inputAccessoryView and adjust height on rotate

I'm setting a UITextView as the accessory to another UITextView...
-(void)viewDidLoad{
UITextView *myAccessory =
[[UITextView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height / 3)];
[myAccessory setDelegate:self];
[self.textView setInputAccessoryView:myAccessory];
}
However, I want the height of the inputAccessoryView to expand/contract when the user rotates the device and also want the top of the UITextView to hug the top of the screen.
I've tried programatically creating autolayout constraints for the top of the inputAccessoryView, but this is to no avail, since inputAccessoryView has no superview hierarchy.
Any ideas?

UITextView can't change size with auto layout

I have a XIB file containing a view with a UITextView as subview. This textView is 30px high, because it should resize according to it's content.
So I set the text programmatically, call -sizeToFit and reconfigure the frame of the textView and its superview:
[_textView setText:[contents objectForKey:#"body"]];
[_textView sizeToFit];
CGSize txtSize = _textView.contentSize;
txtSize.height = _textView.frame.size.height;
_textView.contentSite = txtSize;
CGRect superFrame = _textView.superView.frame;
superFrame += _textView.contentSize.height;
_textView.superView.frame = superFrame;
The superView actually does resize correctly. Also, when logging the text views frame and content size, on the console it appears to be okay (about 1200px high).
But the textView is still just 30px high on the screen. Also calling -setNeedsLayout did not do the trick.
What could be the issue here?
This is a bug i ran into a couple of days ago. It seems that UITextView will cut off a part of the text no matter the height if its scroll is disabled.
A solution that works in certain situations is:
[_textView setScrollEnabled:YES];
[_textView setText:text];
[_textView setScrollEnabled:NO];
I have reported this bug to Apple, but I suggest you do the same.

Resources