iOS - UITextView lineSpacing使光标高度不同 [英] iOS - UITextView lineSpacing make cursor height not same
问题描述
我在 UITextview
中使用 NSMutableParagraphStyle
在每行文本之间添加行间距。
I'm using NSMutableParagraphStyle
in my UITextview
for adding linespace between each row text.
当我在textview中输入内容时,光标高度正常。但是当我将光标位置移动到第二行(不是最后一行)的文本时,光标高度越来越大。
When I type something in textview, cursor height is normal. but when I move cursor position to text on the 2nd row (not the last row), cursor height is getting bigger.
如何在文本的每一行中使光标高度正常?
这是我目前正在使用的代码:
What should I do to make cursor height normal in every row of the texts? This is code I'm currently using:
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineSpacing = 30.;
textView.font = [UIFont fontWithName:@"Helvetica" size:16];
textView.attributedText = [[NSAttributedString alloc] initWithString:@"My Text" attributes:@{NSParagraphStyleAttributeName : paragraphStyle}];
推荐答案
最后我得到的解决方案可以解决我的问题。
Finally I get solution that solves my problem.
通过继承 UITextView
,然后重写 caretRectForPosition:position <,可以更改光标高度/ code>功能。例如:
Changing the cursor height is possible by subclassing the UITextView
, then overriding the caretRectForPosition:position
function. For Example:
- (CGRect)caretRectForPosition:(UITextPosition *)position {
CGRect originalRect = [super caretRectForPosition:position];
originalRect.size.height = 18.0;
return originalRect;
}
文档链接: https://developer.apple.com/documentation/uikit/uitextinput/1614518-caretrectforposition
这篇关于iOS - UITextView lineSpacing使光标高度不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!