首页 > 开发 > iOS > 正文

UITextView以设置边距

2017-09-08 14:03:43  来源:网友分享

UITextView的对齐方式如果选择左对齐,内容直接顶到左边了,如果选择剧中,换行的时候就超级恶心,两个字也会在中间。
我现在的做法是,在UITextView背后贴了一个背景,感觉有点恶心

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];


有没有什么办法,让UITextView可以预留边距的?用一个UITextView解决问题

解决方案

可以使用contentInset这个属性

textView.contentInset = UIEdgeInsetsMake(10.0f, 10.0f, 10.0f, 10.0f);

再看效果