UITextView垂直居中对齐文本

时间:2013-06-10 12:54:48

标签: ios uitextview

我希望将文字垂直对齐UItextView

我正在使用以下代码

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}

不知何故,这在iOS 5中不起作用,因为返回的contentSize与iOS6中的内容不同。

任何想法为什么同一textView的contentSize在iOS 5和iOS 6中有所不同?

3 个答案:

答案 0 :(得分:20)

在加载视图时为UITextView的contentSize键值添加一个观察者: -

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

每次contentSize值更改时调整contentOffset: -

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

希望它对你有所帮助......

您可以从

获取指南

https://github.com/HansPinckaers/GrowingTextView

答案 1 :(得分:5)

在iOS7上的observeValueForKeyPath方法上试试这个:

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
    {
UITextView *tv = object;

CGFloat height = [tv bounds].size.height;
CGFloat contentheight;

if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7")) {
    contentheight = [tv sizeThatFits:CGSizeMake(tv.frame.size.width, FLT_MAX)].height;
    NSLog(@"iOS7; %f %f", height, contentheight);
}else{
    contentheight = [tv contentSize].height;
    NSLog(@"iOS6; %f %f", height, contentheight);
}

CGFloat topCorrect = height - contentheight;
topCorrect = (topCorrect <0.0 ? 0.0 : topCorrect);
tv.contentOffset = (CGPoint){.x = 0, .y = -topCorrect};
}

待定义:

#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v)  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)

答案 2 :(得分:0)

我修改了Arpit的解决方案,因此它可以适应扩展的textview contentView

static NSString *const kContentOffsetKeyPath = @"contentOffset";

-(void)viewDidLoad {
     [self.replyTextView addObserver:self
                         forKeyPath:kContentOffsetKeyPath
                            options:(NSKeyValueObservingOptionNew)
                            context:NULL];
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    if ([keyPath isEqualToString:kContentOffsetKeyPath]) {
        // To keep scrolling to bottom while typing and text content view is larger than maximum limit
        if (textView.contentSize.height > singleLineHeight) {
            UITextView *textView = object;
            CGFloat topCorrect = ([textView bounds].size.height - [textView contentSize].height * [textView zoomScale]) / 2.0;
            CGFloat fineTune = -3.0;
            topCorrect = (topCorrect < fineTune ? fineTune : topCorrect);
            // To avoid recursion
            [textView removeObserver:self forKeyPath:kContentOffsetKeyPath];
            textView.contentOffset = (CGPoint){.x = 0, .y = -topCorrect};
            // add observer back
            [textView addObserver:self
                       forKeyPath:kContentOffsetKeyPath
                          options:(NSKeyValueObservingOptionNew)
                          context:NULL];
        }
    }
}

- (void)dealloc {
    [self.replyTextView removeObserver:self forKeyPath:kContentOffsetKeyPath];
    }