当您键入时,我正在缩进UITextView
中的第一行。 这是一个演示。
这有效,但我无法弄清楚如何在出现文本视图时缩进。 如果您在键盘中添加和删除字符,它会正确缩进空行,但当它第一次出现时我不知道该怎么做。
@interface ViewController ()
@property (strong, nonatomic) UITextView *textView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.textView = [UITextView new];
self.textView.delegate = self;
[self.view addSubview:self.textView];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
self.textView.frame = self.view.bounds;
[self textViewDidChange:self.textView];
}
- (void)textViewDidChange:(UITextView *)textView {
NSMutableParagraphStyle *style = [NSMutableParagraphStyle new];
style.firstLineHeadIndent = 20.f;
NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:textView.text attributes:@{NSParagraphStyleAttributeName: style}];
textView.attributedText = string;
}
@end
我认为这与您的代码相同。
textView.textContainerInset = UIEdgeInsetsMake(8.0f, 20.0f, 8.0f, 0.0f);
如果我理解正确,您希望在用户点击空的 UITextView 开始键入时将光标缩进,并且目前它仅在用户开始键入后才开始缩进。
在您的视图中,使用 @" " 初始化属性文本,然后将文本设置为 @",如下所示:
-(void)viewDidLoad
{
[super viewDidLoad];
self.textView = [UITextView new];
self.textView.delegate = self;
[self.view addSubview:self.textView];
NSMutableParagraphStyle* style = [NSMutableParagraphStyle new];
style.firstLineHeadIndent = 20.f;
NSMutableAttributedString* string = [[NSMutableAttributedString alloc] initWithString:@" " attributes:@{NSParagraphStyleAttributeName: style}];
textView.attributedText = string;
textView.text = @"";
}
注意:我认为textViewDidChange中的代码是不必要的。