如何在UITextView中设置页边距

 我来答
很尴尬的萨嘎bC
2016-07-25 · TA获得超过944个赞
知道大有可为答主
回答量:881
采纳率:0%
帮助的人:791万
展开全部
1、设置在顶部和底部边距与contentInset:
textView = [[UITextView alloc] init];
textView.contentInset = UIEdgeInsetsMake(20.0, 0.0, 20.0, 0.0);
为左,右页边距不正确,而不是设置左,右缩进与NSMutableParagraphStyle添加您的纯文本的时候了一个NSAttributedString:
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];
这使你与你的文字一个UITextView(在我的情况下,从会将myText变量)与20像素填充 CodeGo.net,妥善滚动。

2. 你可以一个较小的UITextView,并放置一个UIView的背景来模拟填充。
+----------+
| | <-- UIView (as background)
| +--+ |
| | | <---- UITextView
| | | |
| +--+ |
| |
+----------+

3. 这是工作改变text查看contentInset和插图/位置,以得到正确的填充和自动换行。然后改变TextView的border,以防止水平滚动。您还需要重新设置每个文本被选中,改变了填充。
- (void)setPadding
{
UIEdgeInsets padding = UIEdgeInsetsMake(30, 20, 15, 50);
textView.contentInset = padding;
CGRect frame = textView.frame;
// must change frame before bounds because the text wrap is reformatted based on frame, don't include the top and bottom insets
CGRect insetFrame = UIEdgeInsetsInsetRect(frame, UIEdgeInsetsMake(0, padding.left, 0, padding.right));
// offset frame back to original x
CGFloat offsetX = frame.origin.x - (insetFrame.origin.x - ( padding.left + padding.right ) / 2);
insetFrame = CGRectApplyAffineTransform(insetFrame, CGAffineTransformMakeTranslation(offsetX, 0));
textView.frame = insetFrame;
textView.bounds = UIEdgeInsetsInsetRect(textView.bounds, UIEdgeInsetsMake(0, -padding.left, 0, -padding.right));
[textView scrollRectToVisible:CGRectMake(0,0,1,1) animated:NO];
}
-(void)textViewDidChangeSelection:(UITextView *)textView
{
[self setPadding];
}
-(void)textViewDidChange:(UITextView *)textView
{
[self setPadding];
}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式