2015-10-24 22 views
0

我試圖對齊UITextView的文本。我的文本視圖在單元格中。 Using this link by stackoverflow我試圖對齊文本。我做了TableViewCell類如下,如何在表格視圖單元格中垂直對齊文本視圖文本?

- (void)awakeFromNib { 
    [self.postedText addObserver:self forKeyPath:@"contentSize" options:(NSKeyValueObservingOptionNew) context:NULL]; 
} 

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

當我運行這個,我得到了以下錯誤。

observeValueForKeyPath:ofObject:change:context:消息已收到但未處理。 關鍵路徑:contentSize 觀察對象:; layer =; contentOffset:{0,0}; contentSize:{256,204}> 更改:{ kind = 1; new =「NSSize:{256,204}」; } 上下文:爲0x0' ***第一擲調用堆棧: (0x1821a02d8 0x1939740e4 0x1821a0218 0x183075d44 0x182fc95c4 0x182fc90e4 0x182fb288c 0x187315944 0x186c8f024 0x186bdd760 0x186525e1c 0x186520884 0x186520728 0x18651febc 0x18651fc3c 0x186c63f90 0x186c63ef4 0x1821582a4 0x182155230 0x182155560 0x1820812d4 0x18b89f6fc 0x186c46fac 0x100060414 0x193ff2a08) 的libC++ ABI。 dylib:以NS類型的未捕獲異常終止

我該如何解決這個問題?

+0

請查看[此鏈接](http://stackoverflow.com/questions/6557178/is-it-possible-to-vertically-align-text-inside-labels-with-a-large-frame) –

回答

0

你怎麼知道self會持續?我認爲這是一個表格單元;他們來來去去。使KVO中的表格單元格爲觀察者是非常危險的(正如您剛發現的那樣)。

一般來說,這種方法不會成立,因爲觀察者需要堅持超越被觀察者的生活,並且需要在被觀察者不存在之前用觀察者註銷自己。你不能做這兩件事中的任何一件。

+0

好。那麼有沒有辦法讓文字垂直對齊? – codebot

+0

你希望實現什麼垂直對齊?這甚至意味着什麼?這是一個可編輯的文本視圖?如果用戶輸入大量文本,我們如何排列任何東西?目前還不清楚你想達到什麼(以及爲什麼)。 – matt

相關問題