简体   繁体   中英

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

I am using UITextView to do text editing. I want the edit zone of the UITextView is same as the 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.

Is there a way to remove the right padding of UITextView in iOS 6?

If you're only targeting iOS6 then you can give top and bottom margin with contentInset like this ,

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:

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

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

remember you'll need to check for it with respondToSelector for availability of textContainerInset .

UITextView has a property called textContainerInset. The default value for this inset is (top = 8, left = 0, bottom = 8, right = 0). So setting this inset to UIEdgeInsetsZero should get rid of the top and the bottom padding.

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. And this NSTextContainer object has a property called 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.

textView.textContainer.lineFragmentPadding = 0;

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM