iOS中的UITextView文本对齐方式

前端之家收集整理的这篇文章主要介绍了iOS中的UITextView文本对齐方式前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
用户设置焦点在UITextView上然后textview中的文本向上时,我有一个UITextView来获取用户输入的数字.我想将它水平放置在中心位置.有人可以帮忙吗?

enter image description here

解决方法

这将是一个有点棘手的修复.您需要观察UITextView的contentSize属性,然后调整其contentSize& contentOffset是这样的:

- (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};
}

猜你在找的iOS相关文章