2013-06-10 29 views
7

Ich möchte Text vertikal zentrieren in UItextView.UITextView alligine Text vertikal zentrieren

ich folgenden Code verwenden

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} 

;

Irgendwie funktioniert das nicht in iOS 5, da die dort zurückgegebene contentSize anders ist, was ich in iOS6 bekomme.

Irgendeine Idee, warum contentSize der gleichen TextView ist in iOS 5 und iOS 6 anders?

Antwort

20

einen Beobachter für den Schlüsselwert des UITextView content hinzufügen, wenn die Ansicht geladen: -

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

die Content Stellen Sie die Änderung Content Wert jedes Mal: ​​-

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

Hoffe, dass es Ihnen hilft ...

können Sie übernehmen Führung von

https://github.com/HansPinckaers/GrowingTextView

+1

Dies hält den Text nicht ständig vertikal zentriert, denn wenn die UITextView in der Größe geändert wird (fo Wenn beispielsweise die Tastatur angezeigt wird, kehrt der Inhalt in der UITextView-Datei zur oberen Ausrichtung zurück. – motionpotion

+1

Dies ist ein großer Teil des Codes abgesehen von seinem Wert als Schlüsselpfadbeobachter. Vielen Dank. –

+0

Ich hatte einen Absturz bei der Verwendung dieses Codes in einem 'UITableView'. Es funktioniert gut auf ios8, muss aber auch den Beobachter nach der Ausrichtung entfernen. Fügen Sie diese Zeile als letzte Zeile der Methode 'observeValueForKeyPath' hinzu: '[tv removeObserver: self forKeyPath: @" contentSize "];' –

5

versuchen, dies auf observeValueForKeyPath Methode auf 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}; 
} 

definiert werden:

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

I Arpit Lösung geändert haben, so dass es mit wachsenden Textview content

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]; 
    } 
passen
Verwandte Themen