UITextView text alignment in iOS

I have UITextViewto enter numbers from the user, when the user sets the focus to UITextView, then the text in textviewgoes up. I want to set it horizontally. Can anyone help?

enter image description here

+4
source share
1 answer

That would be a bit complicated. You will need to observe contentSizeyour property UITextView, and then adjust it contentSizeand contentOffsetas follows:

- (void) viewDidLoad {
    [self.textView addObserver:self forKeyPath:@"contentSize" options:(NSKeyValueObservingOptionNew) context:NULL];
    [super viewDidLoad];
}

- (void)observeValueForKeyPath:(NSString *)iKeyPath ofObject:(id)iObject change:(NSDictionary *)iChange context:(void *)iContext {
    UITextView *myTextView = (UITextView *)iObject;
    CGFloat topCorrect = (myTextView.bounds.size.height - [myTextView contentSize].height * [myTextView zoomScale]) / 2.0;
    topCorrect = (topCorrect < 0.0 ? 0.0 : topCorrect);
    myTextView.contentOffset = (CGPoint){.x = 0, .y = -topCorrect};
}
0
source

Source: https://habr.com/ru/post/1607797/


All Articles