UItextview cursor position - swift

I've a UITextview with these constraints:
Align Center x,y and width and height equals.
When I click the UItextview for the first time the cursor is like:
When I write inside:
When I delete characters:
what happened?

Your UITextView height is too small and it's contentSize height is bigger than it's frame height. A UITextView is a subclass of an UIScrollView so it's content is scrolling, this is what's happening in your case.

This is cause by textContainerInset.
you can add the follow code to solve:
tetxView.textContainerInset = UIEdgeInsetsZero;
other strange issue maybe you can solve by this:
textView.textContainer.lineFragmentPadding = 0;

Related

how to force horizontal scrolling only on UITextView

I have a UITextView with contentSize property set to a very large value 1000, the width of the UITextView is 200, scrolling is enabled, horizontal bouncing is enabled and vertical bouncing is disabled but this UITextView still scrolls vertically on large text.
how can I force horizontal scrolling on it ?
p.s. the solution here does't work.
UITextView is a subclass of - UIScrollView. So you can use ScrollViewDelegate method for disabling this.
Use -
- (void)scrollViewDidScroll:(id)aScrollView
{
[aScrollView setContentOffset:CGPointMake([scrollView contentOffset].x, 0.0)];
}
I have tried many different ways to have a horizontal scrolling functionality to work as expected, but non of these methods worked for me, so I created a UIScrollView and added a UILabel as a subview in the scroll view, and setting the frame's width of the label dynamically according to its text and the text font, after that I set the contentSize of the scroll view to be equal to the label's new frame width & height. Using this method you will have a horizontal scrolling text.
Using JAHelia'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

How can I set scroll offset after reset content inset?

I have a problem with UITextView. I must reset this content inset because I have to add some content to top and bottom.
I wrote a code in textViewDidChanged.
Because I'll set something, to textView's bottom, when I typing blah-blah, something is repositioned, otherwise blah-blah is typed on the something. I don't want that. So i wrote a code in textViewDidChange.
Here's my code:
UIEdgeInsets newInset = self.textView.contentInset;
newInset.top = 50;
newInset.bottom = 1000; // just example
self.textView.contentInset = newInset;
Ok done.
I reset the content inset, but textView's scrol always points to the end of uitextView.
I want scroll offset is set to typing position.
How can I fix it? I just want resize my textview's content inset.
How can I do it? Help me please!
As UITextView inherits from UIScrollView you can set the Scroll Indicator insets by calling:
UIEdgeInsets scrollInsets = UIEdgeInsetsMake(20.0, 0.0, 30.0, 0.0);
yourTextView.scrollIndicatorInsets = scrollInsets;
This will give you 20px at the top and 30px at the bottom of your textview.

How to zoom when scroll UIScrollView

I add few labels in UIScrollView and I want when I scroll, the the middle label font size can become bigger. And the previous middle label font size shrinks to smaller. And the change happens gradually. Like below. Label 1 move to left shrink smaller and label 2 move to middle becomes bigger. All labels in a UIScroll view.
I tried some, like when scroll I tried zoom scroll page, seems complex than I thought...
Anyone has good idea? Thanks!
Pretty simple really. Just stick a UIScrollViewDelegate to the scrollview and do something like this..
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
for (int i = 0; i < [scrollView.subviews count]; i++) {
UIView *v = [scrollView.subviews objectAtIndex:i];
float position = v.center.x - scrollView.contentOffset.x;
float offset = 2.0 - (abs(scrollView.center.x - position) * 1.0) / scrollView.center.x;
v.transform = CGAffineTransformIdentity;
v.transform = CGAffineTransformScale(v.transform, offset, offset);
}
}
But, if you aren't impressed by the affine transform, you could still scale the rect using the offset factor and set the UILabel to adjustsFontSizeToFitWidth... and you are done!
Just make sure there is enough space! Else it could get out of hand very easily.
Assumptions :
There are no other views in the scrollview apart from these labels.
Its required to work just horizontally.
It could be done with CoreAnimation. You have to keep the index of the main label (that one in the center), and after scrolling is done or when scrolling starts (use some proper for you method in UIScrollViewDelegate) and simply shrink side labels by animation.
Just make the size of the font bigger (with an animation block) when it is the middle one, and smaller when it is not.
You can add a category to UILabel with -(BOOL)isMiddle and set it to true/false.

Adding a UITextView and scrollview

scrollView = [[UIScrollView alloc] initWithFrame:
[[UIScreen mainScreen] applicationFrame]];
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(25, 100, 200, 250)];
textView .text =#"long text ";
I will be getting some data of unknown length. to be added to the UITextView . Sometimes the height of the content of the text might exceed the height of the UITextView which is 250 (shown above).
How can i increase the height of the UITextView based on the text i receive ? I would appreciate a sample code? Later i need to add this to a UIScrollView (i know how to add to a scrollview), but again i don't know how to increase the height of the scrollview. Can someone help me code this ?
note: according to my code i am defining the width and height of the UITextView before adding the text to it.
UITextView is a subclass of UIScrollView. When the text content grows larger than the frame of the text view, the content will automatically begin to scroll. There is no need to embed a UITextView in a UIScrollView because it already is one (and more!).
Set the size of textView equal to it's contentSize.
The scrollView you have to set it's contentSize equal to the size you want.

How to enforce horizontal scrolling on UITextView?

I have a UITextView and I would like to only allow it to be scrollable horizontally only.
Basically when the UITextView word-wraps I want the user to have to scroll horizontally to be able to view the rest of the contents.
You can set the content size of the text view to the height of the view. Just make sure the width of the content extends past the width of the textView's frame or else it won't scroll.
// UITextView* myTextView: gets declared somewhere...
// The text view (subclass of a UIScrollView) won't go past its content size
[myTextView setContentSize:CGSizeMake(width, myTextView.frame.size.height)];
// Just in case, if you don't want your user bouncing the view up/down
[myTextView setAlwaysBounceVertical:NO];
I hope that's what you were looking for.
The solution is to turn off all the scroll options on the UITextView itself, then embed it in another UIScrollView. You can get actual code by searching on this term "DualScrollTextView" in google. You cannot force changes to contentSize to accomplish this - many have tried.
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