当 .text 值更改时,UITextView 行间距恢复为默认值

UITextView line spacing goes back to default when the .text value is changed

在属性检查器上,我将 UITextView 选项卡中的 .text 属性设置为 'attributed',然后将 'Line' 值更改为 1.5。

将新值分配给 UITextView 中的 .text 字段后,行距会缩回。

左边是main.storyboard中显示的视图控制器,右边是在UITextView、运行中赋值String后的结果模拟器。

更改 .text 后是否有约束或其他方法来修复行间距值?我认为它应该由代码管理..

let style = NSMutableParagraphStyle()
style.lineSpacing = 40
let attributes = [NSParagraphStyleAttributeName : style]
aboutNeyong.attributedText =
NSAttributedString(string: aboutNeyong.text!, attributes:attributes)

上面的代码是我在赋值语句之后添加的。

尝试设置 UITextField 的 "attributedText" 字段而不是 "text" 字段。确保传入具有所需间距的属性字符串:

NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineSpacing = 15; // Desired line spacing here
NSDictionary *attrsDictionary = @{ NSParagraphStyleAttributeName: paragraphStyle };

textView.attributedText = [[NSAttributedString alloc] initWithString:@"Your new string here!" attributes:attrsDictionary];

您必须设置最大和最小高度

示例:

style.maximumLineHeight = 10 style.minimumLineHeight = 10