如何水平对齐文字&垂直在UITextView? [英] How to align text horizontally & vertically in UITextView?

查看:71
本文介绍了如何水平对齐文字&垂直在UITextView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何水平对齐文本&垂直在UITextView? 我想将UITextView中的文本与水平对齐&垂直对齐. 有什么自定义方式吗? 请帮帮我.....

How to align text horizontally & vertically in UITextView? I want align text in UITextView with horizontal alignment & vertical alignment. Is there any custom way? Please help me out.....

推荐答案

据我所知,还没有内置方法用于UITextView的垂直对齐.但是,通过更新contentOffset,您可以获得垂直居中的文本:

As far as I know there is no built in method for vertical alignment of a UITextView. However, by updating the the contentOffset you can get vertically centered text:

[textView setTextAlignment:NSTextAlignmentCenter];

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

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [textView removeObserver:self forKeyPath:@"contentSize"];
}

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

这篇关于如何水平对齐文字&amp;垂直在UITextView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆