如何在UITextView中设置页边距(填充)?

我有一个UITextView的文本编辑。默认情况下,它具有文本周围的小幅度。我想通过几个像素,以增加利润率。 该contentInset物业的利润,但它不会改变文本“换行宽”。该文本被包裹在宽度和额外的“保证金”只是认为水平滚动。 有没有一种方法,使一定宽度的一个UITextView显示具有较窄的“包装宽度”文本?
本文地址 :CodeGo.net/174510/ 
-------------------------------------------------------------------------------------------------------------------------
1.  摆弄周围的这一段时间后,我发现了另外一个解决方案,如果你只是开发的iOS 6。设置在顶部和底部边距与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];
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值