UITextView missing text at the end? - ios

So the white background part is a UITextView and the blue bubble is a UIImageView. How come the UITextView lost some text at the end when given the same text at different row of the tableview?
Here’s how I configured the UITextView:
self.textView.textContainerInset = UIEdgeInsetsZero;
self.textView.textContainer.lineFragmentPadding = 0;
//self.textView.backgroundColor = [UIColor clearColor];
self.textView.editable = NO;
self.textView.scrollEnabled = NO;
I need the selection and copy capabilities of UITextView, so I can’t use UILabel. Basically, I want to make UITextView behaves like UILabel.
Thank you in advance.
Edit 1:
This is how I set the constraints:
[self.textView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self.msgContainerView).insets(UIEdgeInsetsMake(kPaddingTB, kPadding, kPaddingTB, kPadding))
}];
Edit 2:
It seems that the text disappears when tableview is scrolled.

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

UITextView text Disappears

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.

Avoid animation of UITableViewCell contents, when using AutoLayout and animating size of UITableView

Initially I have a UITableView which takes up only a small part of the screen, using AutoLayout constraints. The UITableViewCells are laid out nicely - they are very basic with just an orange left aligned label and a black right aligned label.
Now, if I swipe up on the UITableView, it animates (the AutoLayout constraints), to take up a larger part of the screen, while reloading the table and bringing additional UITableViewCells into the scene.
My problem is, that the layout of the new UITableViewCells is animated along with the resizing of the UITableView. The black right label occurs right after the orange label, and animates to the right. In the end, everything is aligned as expected, only I can't figure out why the black right labels isn't simply positioned to the very right as soon as the cell appears on screen, but instead is animated into position.
Is there a way to tell the UITableViewCell to layout it's contents right away with respect to the width of the UITableView and the specified AutoLayout constraints, and thus bypassing the animation of the UITableViewCell contents?
My code for creating the UITableViewCell looks like this:
- (instancetype)init
{
self = [super init];
if (self) {
self.textLabel = [[UILabel alloc]init];
self.textLabel.translatesAutoresizingMaskIntoConstraints = NO;
self.textLabel.font = [UIFont boldSystemFontOfSize:16.0];
self.textLabel.textColor = [UIColor orangeColor];
[self.contentView addSubview:self.textLabel];
self.amountLabel = [[UILabel alloc]init];
self.amountLabel.translatesAutoresizingMaskIntoConstraints = NO;
self.amountLabel.textAlignment = NSTextAlignmentRight;
self.amountLabel.font = [UIFont boldSystemFontOfSize:16.0];
[self.contentView addSubview:self.amountLabel];
NSDictionary *views = #{#"textLabel": self.textLabel, #"amountLabel": self.amountLabel};
[self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"|-10-[textLabel]-10-[amountLabel]-10-|" options:0 metrics:nil views:views]];
}
return self;
}
Initial TableView before animation:
TableView during animation (with black right label in the middle of it's journey to the right):
The following code works for me in swift. I have added the second label in detailTextLabel and in the attribute inspector for the TableView I had selected style as "Right Detail". I have attached the screenshot of the tableview with attribute inspector.
cell.textLabel!.text = arrayLabel[indexPath.row]
cell.detailTextLabel!.text = cntarray[indexPath.row]
Hope it might be helpful to you.

UIScrollView with dynamic content

I'm trying to implement a UIScrollView, but every tutorial out there deals with a preset number of items.
What I have are multiple UITextField's, but the number of textfields vary. Basically, as soon as one textField contains text, another empty one appears below it, allowing the user to fill in an unlimited number of textfields.
My code creates a new textfield as soon as the user types something into a previous one. The x-coordinatesof this is the same as the previous one, but the y-coordinatesto the new one equals the height of the previous + 5px.
I'm using storyboards, and I have placed my original textField within a UIScrollView. I have connected this scrollView to my code, and I add a new textfield this way: [self.scrollView addSubview:newTextField];
However, when the amount of textFields exceeds the scrollView, I cannot scroll to reveal the new one that's been added.
How would I go about doing this? I don't think I completely get the setContentSize thing, so it might have something to do with that. Here are some pictures and code to explain my problem:
Code to add new textfield:
UITextField *newTextField = [[UITextField alloc]initWithFrame:CGRectMake(textField.frame.origin.x, textField.frame.origin.y + textField.frame.size.height+5, textField.frame.size.width, textField.frame.size.height)];
newTextField.placeholder = #"Add more text";
newTextField.borderStyle = UITextBorderStyleNone;
newTextField.font = [UIFont systemFontOfSize:14];
newTextField.autocorrectionType = UITextAutocorrectionTypeYes;
newTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
newTextField.autocapitalizationType = UITextAutocapitalizationTypeSentences;
newTextField.returnKeyType = UIReturnKeyDone;
[newTextField setTag:textFieldTag];
newTextField.delegate = self;
[self.scrollView addSubview:newTextField];
Storyboard:
Here you can see how I have placed the original textfield within my scrollview
How it looks in the simulator:
When you enter text in the textfield, this happens:
An empty textfield appears below the previous one.
However, when you exceed the ScrollView, this happens
You can no longer see the new textField because it is below the scrollView's boundaries. You can not scroll to reveal it either.
Does anyone know how to solve this? And if you have time, how would you make it so the scrollview automatically scrolls down to reveal the new textfield that's been added?
Any help is greatly appreciated! Thanks!
The contentSize needs to be the size of content contained within the scroll view.
If the contentSize is wider than the bounds.size, then you can scroll left and right. If the contentSize is taller than the bounds.size, then you can scroll up and down.
You need to set the contentSize to be the entire area you wish to contain within your scroll view.
UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(textField.frame.origin.x, textField.frame.origin.y + textField.frame.size.height+5, textField.frame.size.width, textField.frame.size.height)];
textField.placeholder = #"Add more text";
textField.borderStyle = UITextBorderStyleNone;
textField.font = [UIFont systemFontOfSize:14];
textField.autocorrectionType = UITextAutocorrectionTypeYes;
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
textField.autocapitalizationType = UITextAutocapitalizationTypeSentences;
textField.returnKeyType = UIReturnKeyDone;
textField.tag = textFieldTag;
textField.delegate = self;
[self.scrollView addSubview:textField];
// Update the contentSize to include the new text field.
CGFloat width = self.scrollView.bounds.size.width;
CGFloat height = CGRectGetMaxY(textField.frame);
self.scrollView.contentSize = CGSizeMake(width, height);
NOTES:
Don't start variables or methods with new. It has special meaning and you will confuse other Objective-C developers and/or the compiler.
textField.tag = … is the same as [textfield setTag:…]; You seem to like the dot syntax in other places, so I switched to that.
I'm assuming you don't want the scroll view to pan left and right, so I pinned the content width to the scroll view's width.

Resources