2013-06-10 40 views
7

Tôi muốn căn chỉnh văn bản theo chiều dọc trung tâm trong UItextView.UITextView alligining văn bản theo chiều dọc trung tâm

Tôi đang sử dụng đoạn mã sau

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} 

;

Bằng cách nào đó tính năng này không hoạt động trong iOS 5 khi contentSize trả về có những thứ khác nhau trong iOS6.

Bất kỳ ý tưởng nào tại sao contentSize của cùng một chế độ xem văn bản khác nhau trong iOS 5 và iOS 6?

Trả lời

20

Thêm một người quan sát cho contentSize giá trị quan trọng của UITextView khi xem nạp: -

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

Điều chỉnh contentOffset mỗi khi thay đổi giá trị 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}; 
} 

Hy vọng nó giúp bạn ...

Bạn có thể hướng dẫn từ

https://github.com/HansPinckaers/GrowingTextView

+1

Điều này không giữ cho văn bản liên tục tập trung theo chiều dọc bởi vì nếu UITextView được thay đổi kích thước (fo Ví dụ r, khi bàn phím xuất hiện) thì nội dung trong UITextView sẽ trở lại căn chỉnh trên cùng. – motionpotion

+1

Đây là một đoạn mã lớn ngoài giá trị của nó như là một người quan sát keypath. Cảm ơn. –

+0

Tôi đã gặp sự cố khi sử dụng mã này trong 'UITableView'. Làm việc tốt trên ios8 nhưng cũng cần phải loại bỏ người quan sát sau khi căn chỉnh được thực hiện. Thêm dòng này làm dòng cuối cùng của phương thức 'observValueForKeyPath':' [tv removeObserver: self forKeyPath: @ "contentSize"]; ' –

5

thử này trên phương pháp observeValueForKeyPath trên iOS7:

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

phải được xác định:

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

Tôi đã sửa đổi giải pháp Arpit, do đó nó có thể phù hợp với mở rộng 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]; 
    } 
Các vấn đề liên quan