向UITextView添加左边距

Cam*_*oft 12 iphone uiscrollview uitextview ios

我正在尝试向UITextView添加左边距.

我试过设置属性contentInset,见下文:

UITextView *textView = [[UITextView alloc] initWithFrame: CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
textView.editable = YES;
textView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
textView.backgroundColor = [UIColor clearColor];
textView.opaque = NO;
textView.contentInset = UIEdgeInsetsMake(0, 15.0f, 0, 0);
Run Code Online (Sandbox Code Playgroud)

这似乎工作,虽然它导致TextView上的水平滚动,我不想要.

我只是希望左侧的文本插入而不使textview更宽.

Dum*_*oko 23

请记住,对于iOS 7,有一个特殊的属性叫做

textContainerInset文本视图的内容区域中文本容器的布局区域的插入.

@property(nonatomic,assign)UIEdgeInsets textContainerInset

此属性为文本视图中布置的文本提供文本边距.

可用性适用于iOS 7.0及更高版本.在UITextView.h中声明

在iOS 6中,contentInset正在完成这项工作.我个人遇到了这个问题.一切都结束了:

if (isIOS7()) {
    textView.textContainerInset = UIEdgeInsetsMake(0, 10, 0, 10);
} else {
    textView.contentInset = UIEdgeInsetsMake(0, 10, 0, 10);
}
Run Code Online (Sandbox Code Playgroud)


小智 16

如果您只想移动文本,请尝试

[textView setContentInset:UIEdgeInsetsMake(<#CGFloat top#>, <#CGFloat left#>, <#CGFloat bottom#>, <#CGFloat right#>)];
Run Code Online (Sandbox Code Playgroud)

如果正数将"文本框架"移向中间,则负数将其从中间移出.

例如,[textView setContentInset:UIEdgeInsetsMake(0, 20, 0,-20)]将文本向右移动20个像素!