将左边距添加到UITextView
我试图添加一个左边距到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);
这似乎工作,虽然它会导致我不想要的TextView水平滚动。
我只是想在左边的文字插入没有任何更广泛的文字。
如果您只想移动文字,请尝试
[textView setContentInset:UIEdgeInsetsMake(<#CGFloat top#>, <#CGFloat left#>, <#CGFloat bottom#>, <#CGFloat right#>)];
如果一个正数将“文本框架”移向中间,那么一个负数就会将其从中间移出。
例如, [textView setContentInset:UIEdgeInsetsMake(0, 20, 0,-20)]
会将文本向右移动20个像素!
请记住,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); }
唯一的东西对我来说是通过子类UITextView并重写下面的方法:
- (id)styleString { return [[super styleString] stringByAppendingString:@"; line-height: 1.6em;margin-right: 30px; margin-left: 45px; margin-top: 20px;"]; }
很明显; 你可以调整左边距,margin-top和任何你想要的;)
关于把UITextView放在一个更大的UIView中的build议对我来说不起作用,因为我需要在UITextView的背景下滚动行,我使用了下面的开源项目来做到这一点: https : //github.com/aahsanali/ NoteView
另外,下面的解决scheme根本没有帮助我:
[textView setContentInset:UIEdgeInsetsMake(0, 20, 0,-20)]
因为它没有正确包装文本。
我希望这个解决scheme有帮助,因为我真的花了一个星期的时间来挖掘这个简单的UI调整!
你可以用Swift 3.0在下面试试这个代码
// Align the text to the left edge of textContainer textView.textContainer.lineFragmentPadding = 16 // Align the text with the top of textContainer textView.textContainerInset = UIEdgeInsets.zero
这个对我有用。
UITextView
是使用Web视图在内部实现的。 这些内部使用了许多未logging的技巧,这些技巧也在OS更新之间改变。 其中一个技巧是重新设置 contentInsets
属性。 何时和为什么没有logging,据我所知,文本视图的布局的任何更改可以/将触发此重置。
我的第一个build议是不要试图调整文本视图的插页。 这只是脆弱的,未来可能会打破。
如果你真的坚持反正那么我会UITextView
,覆盖-[UITextView layoutSubviews]
并在那里强制新的插入。
无论哪种情况,我都会去http://bugreport.apple.com报告当前的行为是严重的错误。
如果你通过IB来做这件事情会容易得多。 find名为“ 滚动查看大小”并设置“内容插入”的面板。
在代码中,您可以尝试将scrollEnabled
设置为NO。