简体   繁体   English

如何在iOS 6中删除UITextView右侧填充?

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

I am using UITextView to do text editing. 我正在使用UITextView进行文本编辑。 I want the edit zone of the UITextView is same as the UILabel . 我希望UITextView的编辑区域与UILabel相同。 I use UIEdgeInsetsMake(-4,-8,0,-8) method and it helps a little, it removes the left padding and top padding but the right padding is still exists. 我使用UIEdgeInsetsMake(-4,-8,0,-8)方法,它有一点帮助,它删除了左侧的填充和顶部的填充,但右侧的填充仍然存在。

Is there a way to remove the right padding of UITextView in iOS 6? 有没有办法在iOS 6中删除UITextView的正确填充?

If you're only targeting iOS6 then you can give top and bottom margin with contentInset like this , 如果您仅定位到iOS6,则可以使用 contentInset 这样 提供最高和最低边距

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

For the left and right margins don't add your plain text right away but use an NSAttributedString instead with properly set left and right indents with an NSMutableParagraphStyle: 对于左边距和右边距,请不要立即添加纯文本,而应使用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];

If you're also supporting iOS7 then, use textContainerInset 如果您还支持iOS7,请使用 textContainerInset

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

remember you'll need to check for it with respondToSelector for availability of textContainerInset . 记住,您需要使用respondToSelector进行检查以确保textContainerInset可用性。

UITextView has a property called textContainerInset. UITextView具有一个名为textContainerInset的属性。 The default value for this inset is (top = 8, left = 0, bottom = 8, right = 0). 此插图的默认值为(顶部= 8,左侧= 0,底部= 8,右侧= 0)。 So setting this inset to UIEdgeInsetsZero should get rid of the top and the bottom padding. 因此,将此插入设置为UIEdgeInsetsZero应该摆脱顶部和底部的填充。

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

But there is still some padding to the left and the right of the text. 但是在文本的左侧和右侧仍然有一些填充。 The solution to get rid of that is not as obvious as setting the insets to zero. 消除该问题的解决方案并不像将插图设置为零那么明显。 UITextView uses a NSTextContainer object to define the area in which the text is displayed. UITextView使用NSTextContainer对象定义显示文本的区域。 And this NSTextContainer object has a property called lineFragmentPadding. 这个NSTextContainer对象具有一个名为lineFragmentPadding的属性。 This property defines the amount by which the text is inset within line fragment rectangles, or in other words: the left and right padding of the text. 此属性定义在线段矩形内插入文本的数量,即换句话说:文本的左侧和右侧填充。 The default value for this is 5.0, so setting this to 0 removes the padding. 默认值为5.0,因此将其设置为0将删除填充。

textView.textContainer.lineFragmentPadding = 0; textView.textContainer.lineFragmentPadding = 0;

link: http://www.pixeldock.com/blog/how-to-get-rid-of-the-padding-insets-in-an-uitextview/ 链接: 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