How can I set scroll offset after reset content inset? - iphone

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.

Related

UItextview cursor position

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;

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 to set a margin in UITextView?

I'm currently developing a simple text-editing app for iPad.
I want to set left/right margins like the attached picture.
Just adding UITextView into another UIView with larger width won't work because a scroll indicator won't be properly located.
Instead of UIView, I added UITextView into UIScrollView, and it works almost fine. But they sometimes show strange behaviors, and UITextViewDelegate doesn't work with my UIViewController.
Is there any way to set left/right margins only using UITextView?
Thank you.
You can use the property textContainerInset
Following the example of George Green:
myTextView.textContainerInset = UIEdgeInsetsMake(0.0f, 10.0f, 0.0f, 10.0f);
UITextView is a subclass of UIScrollView. I haven't tried it but you could try something like:
myTextView.contentInset = UIEdgeInsetsMake(0.0f, 10.0f, 0.0f, 10.0f);
UIEdgeInsetsMake() is as follows:
UIEdgeInsets UIEdgeInsetsMake (
CGFloat top,
CGFloat left,
CGFloat bottom,
CGFloat right
);
So you should be able to inset your textView content.
Hope this helps, let me know if it works!! :)
I wanted to do the same thing, but textview.contentInset didn't work.
I put UITextView with a narrow width on UIView, then move the scrollview indicator of textview to the right side, so I got what I wanted really well.
textView.clipsToBounds = NO;
textView.scrollIndicatorInsets = UIEdgeInsetsMake(0.0f, 0.0f, 0.0f, -20.0f);
This answer might help.
Is there a way to put UITextView's scroll indicator to outside UITextView?
You can set the scroller right inset value of the UITextView to negative value and disable the clip subview option to achieve your require. No other scrollview is needed.
In code it would be
textView.clipsToBounds = NO;
textView.scrollIndicatorInsets = UIEdgeInsetsMake(0.0f, 0.0f, 0.0f, -50.0f);

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

UILabel - how to change text position

I am trying to add subviews to my UITableView cells.
It works just perfect for me, but the text appears at the very left edge of a cell. How can I move it a little bit to the right side? I want to have a little margin from the left cell border.
Thank you in advance.
CGRect frame = theLabel.frame;
frame.origin.x += LEFT_MARGIN;
theLabel.frame = frame;