动态设置呢,边输入边设置
时间: 2023-09-16 10:11:08 浏览: 46
如果你想在用户输入时动态设置 `UITextView` 的行间距,你可以通过以下方式来实现:
1. 创建一个 `NSMutableParagraphStyle` 对象。
2. 在 `UITextViewDelegate` 的 `-textViewDidChange:` 方法中获取 `UITextView` 的文本范围并设置 `NSMutableParagraphStyle` 对象的 `lineSpacing` 属性。
3. 将 `NSMutableParagraphStyle` 对象赋值给 `UITextView` 的 `typingAttributes` 属性。
代码示例:
```
- (void)textViewDidChange:(UITextView *)textView {
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineSpacing = 10; // 设置行间距值
NSRange range = NSMakeRange(0, textView.text.length);
NSMutableDictionary *attributes = [textView.typingAttributes mutableCopy];
[attributes setObject:paragraphStyle forKey:NSParagraphStyleAttributeName];
[textView.textStorage addAttributes:attributes range:range];
}
```
这样,每当用户输入文本时,都会动态更新 `UITextView` 的行间距。