UITextView top of the text is shown half when pressed on new line, rest is clipped - ios

Check out the gif below, as you see the textview is normal. When I press the return button the top text gets clipped, when I enter a letter or press return again its fixed. After that the next return also clips the text. There is also unnecessary space created at the bottom.
Here is the code I use under UITextViewDelegate:
func textViewDidChange(textView: UITextView) {
textView.h = textView.contentHeight
contentTextContainer.h = textView.contentHeight + 20 //Unnecessary but..
}

Related

Inserting (Appending) text on the top of a UITextView - Swift

I have a UITextView where I insert text using:
MyUITextView.insertText(my_string_to_be_inserted)
it works fine but it appends text to the bottom of the UITextView.
Is there any way to add text on the top (like a stack) where the last added text will be always on the top?
You can try the snippet below.
if let position = textView.textRange(from: textView.beginningOfDocument, to: textView.beginningOfDocument) {
textView.replace(position, withText: "Your text goes here")
}
Refer the api here.

Slide view together with UICollectionView cell

i have this structure. Scrollview -> UICollectionview + Label
This viewcontroller has array of items (BlockItem). On scrollViewDidScroll i change current test label (description)
func scrollViewDidScroll(_ scrollView:UIScrollView)
{
let midX:CGFloat = scrollView.bounds.midX
let midY:CGFloat = scrollView.bounds.midY
let point:CGPoint = CGPoint(x:midX, y:midY)
guard
let indexPath:IndexPath = collectionView.indexPathForItem(at:point)
else
{
return
}
let currentPage:Int = indexPath.item
if let found = items.first(where: {$0.id == String(block[currentPage])}) {
description.text = found.description
}
}
The main issue that i want my description label will be moving together when i move my collectionview cell and appear from the next cell. In other words, if I scroll to the left my description should move along with the cell, i.e. go to the left and appear on the right of the screen.
How can i do this? I know i could make a big collectionview but i need that only image part should be scrollable, not the entire block.
Here is what i want to achieve
Here is video example: scroll works only if i swipe on the image area, and doesn't work when i scroll down the page
https://drive.google.com/file/d/1kl1GYgXvK4bL3toTfOvpxF2WqS56pQO9/view?usp=sharing
You exactly said this:
"The main issue that I want my description label will be moving together when I move my collection view cell and appear from the next cell."
If you want your description label moving together, just include them into your collection cell. It's very clear from your point there
I don't understand why u need to separate it, but you want it to slide from new cell.
If you insisted that you want to archive the label, not in a scroll view, then use 2 label
One label for this cell and one label will come from scroll direction, u can archive this by creating manually. This is a simple code for a swipe left to scroll
UIView.animate(withDuration: 0.5) {
//insert your label animation
//current label moved with the scroll to left
//create a new label from the outer right of the current view, scroll it to the middle of your view
}
But it will be hard work for good animation or scroll support.

Handling buttons in textview iOS

I am displaying a information in the textview which consists of phone numbers and mail_id on clicking on the numbers the users should be able to dial to the contact, for that i have added a button in that particular place.
When the users scrolls the button is not scrolling only the text is scrolling.
So i have called the Textview & ScrollView Delegate and got the offset value of the scroll View. so that i can set buttons frame .
func scrollViewDidScroll(_ scrollView: UIScrollView){
let rol_y = aboutTextView.contentOffset.y;
let rol_x = aboutTextView.contentOffset.x;
print("\(rol_x),\(rol_y)")
}
After getting the offset value, how can i set the frame..?
Is this method works..?
Or
How can i do this ...?
Just select the UITextView in your storyboard and go to "Show Attributes inspector" and select phone numbers and links in Data Detectors.

UITextView bounces uncontrollably while typing

I have been having this problem with a few of my UITextViews on my most recent project. All I have done is added a UITextView onto a ViewController and added some constraints to it. When I run my program and attempted to type in the UITextView it begins to bounce out of control. For instance, when I type the first character, the view will slide down and out of the screen. Then when I type the next few characters the textview will bounce back up to where I can see what I typed. Then I type a few more, and once again it bounces out of the screen. Has anyone else experienced this?
Also, sometimes the text will start in the middle of the UITextView.
Try add this code to textViewDidChange: method
func textViewDidChange(textView: UITextView) {
if textView.text?.characters.count ?? 0 > 0 {
let range = NSMakeRange(textView.text!.characters.count - 1, 1)
textView.scrollRangeToVisible(range);
}
}
It helped me.

Make a UILabel push other UILabels when it expands?

So I'm making an app, and its going to have a couple of uilabels, stacked on top of each other, all collapsed to a certain size. I have a button that expands each uilabel, individually, but I want to make it so that when one expands, the positions of the others move. Is there a way to do it other than changing the y position of each label, every time?
my code atm is
#IBAction func expandbutton(sender: AnyObject) {
uilabel1.frame = CGRectMake(uilabel1.frame.origin.x, uilabel1.frame.origin.y, uilabel1.frame.width, uilabel1.frame.height + 120)
uilabel2.center = CGPointMake(deltaX, deltaY)
uilabel3.center = CGPointMake(deltaX, deltaY)
....
}

Resources