str*_*get 51 iphone cocoa-touch objective-c uitextview ipad
我有一个用于文本编辑的UITextView.默认情况下,文本周围的边距较小.我想将这个边距增加几个像素.
contentInset属性为我提供了边距,但它不会更改文本"wrap width".文本以相同的宽度包装,额外的"边距"只会使视图水平滚动.
有没有办法让某个宽度的UITextView显示带有较窄"包裹宽度"的文本?
Kar*_*lis 105
从iOS 7开始,您可以使用以下textContainerInset
属性:
Objective-C的
textView.textContainerInset = UIEdgeInsetsMake(0, 20, 0, 20);
Run Code Online (Sandbox Code Playgroud)
迅速
textView.textContainerInset = UIEdgeInsets(top: 0, left: 20, bottom: 0, right: 20)
Run Code Online (Sandbox Code Playgroud)
小智 34
在摆弄了一段时间之后我找到了另一个解决方案,如果你只是为iOS 6开发.使用contentInset设置顶部和底部边距:
textView = [[UITextView alloc] init];
textView.contentInset = UIEdgeInsetsMake(20.0, 0.0, 20.0, 0.0);
Run Code Online (Sandbox Code Playgroud)
对于左边距和右边距,不要立即添加纯文本,而是使用NSAttributedString代替,并使用NSMutableParagraphStyle正确设置左右缩进:
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.headIndent = 20.0;
paragraphStyle.firstLineHeadIndent = 20.0;
paragraphStyle.tailIndent = -20.0;
NSDictionary *attrsDictionary = @{NSFontAttributeName: [UIFont fontWithName:@"TrebuchetMS" size:12.0], NSParagraphStyleAttributeName: paragraphStyle};
textView.attributedText = [[NSAttributedString alloc] initWithString:myText attributes:attrsDictionary];
Run Code Online (Sandbox Code Playgroud)
这为您提供了一个带有文本的UITextView(在我的情况下来自变量myText),其中有20个像素的填充,可以正确滚动.
Raj*_*esh 10
试试这个
UIBezierPath* aObjBezierPath = [UIBezierPath bezierPathWithRect:CGRectMake(0, 0, 20, 20)];
txtView.textContainer.exclusionPaths = @[aObjBezierPath];
Run Code Online (Sandbox Code Playgroud)
您可以使用较小的UITextView,并在后台放置UIView来模拟填充.
+----------+
| | <-- UIView (as background)
| +--+ |
| | | <---- UITextView
| | | |
| +--+ |
| |
+----------+
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
46424 次 |
最近记录: |