簡體   English   中英

如何在iOS 6中刪除UITextView右側填充?

[英]How to remove UITextView right-hand-side padding in iOS 6?

我正在使用UITextView進行文本編輯。 我希望UITextView的編輯區域與UILabel相同。 我使用UIEdgeInsetsMake(-4,-8,0,-8)方法,它有一點幫助,它刪除了左側的填充和頂部的填充,但右側的填充仍然存在。

有沒有辦法在iOS 6中刪除UITextView的正確填充?

如果您僅定位到iOS6,則可以使用 contentInset 這樣 提供最高和最低邊距

textView.contentInset = UIEdgeInsetsMake(20.0, 0.0, 20.0, 0.0);

對於左邊距和右邊距,請不要立即添加純文本,而應使用NSAttributedString並通過NSMutableParagraphStyle正確設置左右縮進:

NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.headIndent = 20.0;
paragraphStyle.firstLineHeadIndent = 20.0;
paragraphStyle.tailIndent = -20.0;

NSDictionary *attrsDictionary = @{NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue" size:12.0], NSParagraphStyleAttributeName: paragraphStyle};
textView.attributedText = [[NSAttributedString alloc] initWithString:@"SomeText...." attributes:attrsDictionary];

如果您還支持iOS7,請使用 textContainerInset

textView.textContainerInset = UIEdgeInsetsMake(0, 20.0, 0, 20.0)

記住,您需要使用respondToSelector進行檢查以確保textContainerInset可用性。

UITextView具有一個名為textContainerInset的屬性。 此插圖的默認值為(頂部= 8,左側= 0,底部= 8,右側= 0)。 因此,將此插入設置為UIEdgeInsetsZero應該擺脫頂部和底部的填充。

textView.textContainerInset = UIEdgeInsetsMake(20.0,0.0,20.0,0.0)

但是在文本的左側和右側仍然有一些填充。 消除該問題的解決方案並不像將插圖設置為零那么明顯。 UITextView使用NSTextContainer對象定義顯示文本的區域。 這個NSTextContainer對象具有一個名為lineFragmentPadding的屬性。 此屬性定義在線段矩形內插入文本的數量,即換句話說:文本的左側和右側填充。 默認值為5.0,因此將其設置為0將刪除填充。

textView.textContainer.lineFragmentPadding = 0;

鏈接: http//www.pixeldock.com/blog/how-to-get-rid-of-the-padding-insets-in-an-uitextview/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM