39

I'm having a UITextView and set content inset as

[atextView setContentInset:UIEdgeInsetsMake(0, 10, 0, 0)];

This code working in iOS 6.1 and below, but nothing happens in iOS 7.0.

4
  • 3
    Is there a way to do this IN STORYBOARD ?! Commented May 13, 2014 at 16:42
  • did you tried updated answer? Commented May 14, 2014 at 9:30
  • Yes there is , check my solution below Joe Blow Commented May 26, 2016 at 16:53
  • @BencePattogato thanks for the update. Commented May 27, 2016 at 4:38

2 Answers 2

140

got answer:

[atextView setTextContainerInset:UIEdgeInsetsMake(7, 7, 0, 0)]; 

fixed my problem.

And in Swift...

@IBOutlet var tv:UITextView! override func awakeFromNib() { super.awakeFromNib() tv.textContainerInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0) } 

UPDATE: another option to do that.

UIView *spacerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 10)]; [msgtext setLeftViewMode:UITextFieldViewModeAlways]; [msgtext setLeftView:spacerView]; 
Sign up to request clarification or add additional context in comments.

8 Comments

hey @murugha23 , I need you help on how you solved textview setContentInset problem in IOS7
replace setTextContainerInset instead of setContentInset. If you want to provide previous versions of ios, then check a condition reg., current device system version
it would be good if you can provide me a sample code of your class,including keyboard and textview's various methods ,so i can I have clear idea
actually whats your problem ? is it setting improperly or completely not setting?
Yes I am posting question and posting link soon
|
0

An alternative and maybe a little more convenient way to do it, by subclassing the UITextField and add an @IBInspectable inset property.

import UIKit class UITextfieldWithInset: UITextField { @IBInspectable var textfieldInset: CGPoint = CGPointMake(0, 0) override func textRectForBounds(bounds: CGRect) -> CGRect { return CGRectInset(bounds, textfieldInset.x, textfieldInset.y) } override func editingRectForBounds(bounds: CGRect) -> CGRect { return CGRectInset(bounds, textfieldInset.x, textfieldInset.y) } override func placeholderRectForBounds(bounds: CGRect) -> CGRect { return CGRectInset(bounds, textfieldInset.x, textfieldInset.y) } } 

1 Comment

This is a nice answer for UITextField, but the question is about UITextView

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.