ibireme / YYText

Powerful text framework for iOS to display and edit rich text.
MIT License
8.84k stars 1.69k forks source link

设置了行间距之后,与 placeholderText 位置有偏差 #862

Open lijialiang opened 5 years ago

lijialiang commented 5 years ago

大概通过如下代码设置行高以及 placeholder

_textView = [[YYTextView alloc] init];
_textView.placeholderText = @"测试的";

NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineSpacing = 10;

NSDictionary *attributes = @{
                              NSFontAttributeName:[UIFont systemFontOfSize:26.0f / 2],
                              NSParagraphStyleAttributeName:paragraphStyle
                              };
_textView.attributedText = [[NSAttributedString alloc] initWithString:_textView.text attributes:attributes];

请问该如何设置行高,让 placeholderText 位置与 输入文字 位置一致?

liuliangju commented 5 years ago

这么写试试

_textView = [[YYTextView alloc] initWithFrame:self.view.frame];
_textView.placeholderText = @"位置有偏差";
_textView.placeholderFont = [UIFont systemFontOfSize:18];
NSMutableAttributedString *mAtt = [[NSMutableAttributedString alloc] initWithString:@""];
[mAtt addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:18] range:NSMakeRange(0, mAtt.length)];
mAtt.yy_lineSpacing = 6.0f;
mAtt.yy_kern = @(0.5);
_textView.selectedRange = NSMakeRange(mAtt.length, 0);
_textView.attributedText = mAtt;
_textView.font = [UIFont systemFontOfSize:18];
_textView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;