Ios UITextView垂直居中对齐文本

Ios UITextView垂直居中对齐文本,ios,uitextview,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 = (CGPo

我想在
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}
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与我在iOS 6中得到的不同


知道为什么同一文本视图的
contentSize
在iOS 5和iOS 6中不同吗?

加载视图时,为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};
}
-(void)observeValueForKeyPath:(NSString*)对象的键路径:(id)对象更改:(NSDictionary*)更改上下文:(void*)上下文{
UITextView*tv=对象;
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};
}
希望它能帮助你

你可以从我这儿领路


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

我已经修改了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];
    }
静态NSString*const kContentOffsetKeyPath=@“contentOffset”;
-(无效)viewDidLoad{
[self.replyTextView addObserver:self
forKeyPath:kContentOffsetKeyPath
选项:(NSKeyValueObservingOptionNew)
上下文:NULL];
}
-(void)observeValueForKeyPath:(NSString*)对象的键路径:(id)对象更改:(NSDictionary*)更改上下文:(void*)上下文{
if([keyPath isEqualToString:kContentOffsetKeyPath]){
//在键入和文本内容视图大于最大限制时保持滚动到底部
如果(textView.contentSize.height>singleLineHeight){
UITextView*textView=对象;
CGFloat topCorrect=([textView bounds].size.height-[textView contentSize].height*[textView zoomScale])/2.0;
CGFloat fineTune=-3.0;
topCorrect=(topCorrect
这不会使文本始终垂直居中,因为如果调整UITextView的大小(例如,当键盘出现时),则UITextView中的内容将恢复为顶部对齐。这是一段非常棒的代码,除了作为键路径观察者的价值之外。谢谢。我在
UITableView
中使用此代码时发生崩溃。在ios8上运行良好,但在完成对齐后还需要删除观察者。将此行添加为
observeValueForKeyPath
方法的最后一行:
[tv removeObserver:self forKeyPath:@“contentSize”]可能重复的
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];
    }