如何在UITextView中设置页边距
2个回答
2016-03-22
展开全部
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];
}
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];
}
2016-03-22 · 知道合伙人软件行家
关注
展开全部
self.view.backgroundColor = [UIColor lightGrayColor];
UIView *textViewBg = [[UIView alloc] initWithFrame:CGRectMake(10.0f, 10.0f, 300.0f, 170.0f)];
[self.view addSubview:textViewBg];
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(20.0f, 20.0f, 280.0f, 150.0f)];
textView.backgroundColor = [UIColor whiteColor];
textView.textAlignment = UITextAlignmentLeft;
textView.font = [UIFont systemFontOfSize:16.0f];
[self.view addSubview:textView];
UIView *textViewBg = [[UIView alloc] initWithFrame:CGRectMake(10.0f, 10.0f, 300.0f, 170.0f)];
[self.view addSubview:textViewBg];
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(20.0f, 20.0f, 280.0f, 150.0f)];
textView.backgroundColor = [UIColor whiteColor];
textView.textAlignment = UITextAlignmentLeft;
textView.font = [UIFont systemFontOfSize:16.0f];
[self.view addSubview:textView];
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询