目前有這樣一個(gè)需求,在用戶輸入文字的時(shí)候要設(shè)置行間距,因?yàn)椴辉O(shè)置的話行與行之間挨太近了,設(shè)置行間距設(shè)置行間距從網(wǎng)上找的方法是
-
(void)textViewDidChange:(UITextView *)textView
{
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineSpacing = 5; // 字體的行間距
paragraphStyle.lineBreakMode = NSLineBreakByCharWrapping;NSDictionary *attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:14],
但是這不好的地方是打出漢字會(huì)出現(xiàn)字母,越往后,字母出現(xiàn)越多.
NSParagraphStyleAttributeName:paragraphStyle
};
textView.attributedText = [[NSAttributedString alloc] initWithString:textView.text attributes:attributes];
}
Untitled.gif
最后我自己試出來了, 但是要判斷過濾候選字符,如果沒有候選字符才給他換行 -
(void)textViewDidChange:(UITextView *)textView
{
// 判斷是否有候選字符,如果不為nil醉冤,代表有候選字符
if(textView.markedTextRange == nil)
{
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineSpacing = 5; // 字體的行間距
paragraphStyle.lineBreakMode = NSLineBreakByCharWrapping;NSDictionary *attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:14], NSParagraphStyleAttributeName:paragraphStyle }; textView.attributedText = [[NSAttributedString alloc] initWithString:textView.text attributes:attributes];
}
}
Untitled.gif
最后我想說的是網(wǎng)上很多解決方法都是我考你你考我,不是很靠譜, 還是要自己多測(cè)一下.
另外textView設(shè)置間距后引發(fā)的其他問題,可以參考這位仁兄的簡(jiǎn)書.http://www.reibang.com/p/0a14e6304b68