當我的UiTextView
中添加了兩行或更多行時,它看起來不錯,但是當用戶只輸入一行時,文本停留在UiTextView
的中間。我如何強制它停留在UiTextView
的左上角?UiTextView - 僅添加一行時的位置
1
A
回答
1
你可以查看這個博客(iOS: Vertical aligning text in a UITextView),它解釋瞭如何做中心和底部垂直對齊。使用類似的邏輯並將contentOffset y參數設置爲零,以使其頂部對齊。
這是從博客的代碼,
- (void) viewDidLoad {
[textField addObserver:self forKeyPath:@"contentSize" options:(NSKeyValueObservingOptionNew) context:NULL];
[super viewDidLoad];
}
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
UITextView *tv = object;
//Center vertical alignment
//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};
//Bottom vertical alignment
CGFloat topCorrect = ([tv bounds].size.height - [tv contentSize].height);
topCorrect = (topCorrect <0.0 ? 0.0 : topCorrect);
tv.contentOffset = (CGPoint){.x = 0, .y = -topCorrect};
}
1
我只是找到了一個更簡單的解決方案。下一個代碼將移動文本:[textView setContentInset:UIEdgeInsetsMake(5, 0, 5,0)]
相關問題
- 1. UITextView的位置
- 2. 向UITextView添加換行符
- 3. 添加\ N到NSString或搜索UITextView中的位置
- 4. 有點uitextview位置
- 5. UITextView撤消位置
- 6. UITextView內容在增加高度的同時改變位置
- 7. CCUIViewWrapper忽略UITextView的位置
- 8. SQLite:添加包含行位置的列
- 9. 在DataView的最後位置添加行
- 10. 添加到TreeSet時的錄製位置
- 11. SQLite:僅當結果在唯一行時才添加行
- 12. UITextView無意中添加新行嗎?
- 13. 添加TapGestureRecognizer到的UITextView
- 14. 僅在獲取當前位置時執行一些操作
- 15. 輸出文件僅添加一行
- 16. VBA僅插入添加最後一行
- 17. 重複UITextView滾動位置
- 18. Uitextview滾動視圖位置
- 19. 將文本添加到UITextView時重疊UITextView
- 20. 讓UITextView使第一行當前光標位置?
- 21. Search.twitter.com添加位置
- 22. 將標題僅添加到具有Apache代理的位置
- 23. 如何添加一個動作到UITextview?
- 24. 添加一個清除按鈕到UITextview
- 25. 如何添加一個UIImage到UITextView
- 26. 運行時添加視圖不知道它的位置
- 27. 方法對類添加到一個數組保持添加的類到所述第一位置僅
- 28. dataTables - 在指定位置添加行
- 29. 將位置添加到javascript時間表
- 30. CSS位置:固定 - 運行時僅位本地糾正
檢查此博客[iOS:在UITextView中垂直對齊文本](http://imagineric.ericd.net/2011/03/10/ios-vertical-aligning-text -in-a-uitextview /),或者只是將'contentOffset'設置爲合適的值(零左右)。 – iDev
你見過這個http://stackoverflow.com/questions/3760924/set-line-height-in-uitextview –