在NSParagraphStyle中修改行间距属性会导致游标伸长。

时间:2023-02-01 08:56:57

I have a subclassed UITableViewCell that contains a UITextView. I've added NSParagraphStyle as an attribute to the string in a subclassed NSTextStorage. In the following code, I've increased the space between each row in the UITextView.

我有一个子类化的UITableViewCell,它包含一个UITextView。我已经将NSParagraphStyle添加到子类NSTextStorage中的字符串属性中。在下面的代码中,我增加了UITextView中每一行之间的空间。

swift

斯威夫特

let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineSpacing = 11
myCustomTextStorage.appendAttributedString(NSAttributedString(string: someText, attributes: [NSParagraphStyleAttributeName: paragraphStyle]))

The cursor height elongates till the height of the next line as shown below. This only happens on the rows before the last row.

游标高度延长至下一行的高度,如下所示。这只发生在最后一行之前的行上。

在NSParagraphStyle中修改行间距属性会导致游标伸长。

I've looked at several posts on SO regarding this issue, including this post; however, none of the proposed solutions seem to be working for me.

关于这个问题,我已经看过几篇文章,包括这篇文章;然而,所提出的解决方案中似乎没有一个对我有效。

I've read through the TextKit documentation but haven't found any solution for this issue.

我已经阅读了文本文档,但还没有找到解决这个问题的方法。

Is there a way to reduce the cursor height?

有没有办法降低光标的高度?

2 个解决方案

#1


0  

This works as designed and it's the same on the Mac. It's meant to provide the user with visual feedback of both which line they're on and the height of that line. You should think hard before changing this just because you don't like the way it looks.

这是设计的,在Mac上也是一样的,它的目的是为用户提供他们所在的线和线的高度的视觉反馈。你应该在改变这一切之前好好想想,因为你不喜欢它的样子。

That said, the method in the SO post you linked above is the general approach for adjusting the cursor (though the adjusted rect's origin may need to be adjusted as well). What about it didn't work for you? It'd be better to start from there.

也就是说,上面链接的SO post中的方法是调整光标的通用方法(尽管调整后的rect的起源可能也需要调整)。它对你没用呢?最好从这里开始。

#2


0  

an answer related to this issue is given on this thread. just pasting here for future reference. iOS - UITextView lineSpacing make cursor height not same

在这个线程上给出了与这个问题相关的答案。粘贴在这里,供以后参考。iOS - UITextView行扫描使光标高度不同

"you could change cursor height by subclassing the UITextView, then override the caretRectForPosition:position function. For Example :

你可以通过子类化UITextView来改变光标的高度,然后覆盖caretRectForPosition:position函数。例如:

  • (CGRect)caretRectForPosition:(UITextPosition *)position { CGRect originalRect = [super caretRectForPosition:position]; originalRect.size.height = 18.0; return originalRect; } "
  • (CGRect)caretRectForPosition:(UITextPosition *)position {CGRect originalRect = [super caretRectForPosition:position];originalRect.size。身高= 18.0;返回originalRect;}”

#1


0  

This works as designed and it's the same on the Mac. It's meant to provide the user with visual feedback of both which line they're on and the height of that line. You should think hard before changing this just because you don't like the way it looks.

这是设计的,在Mac上也是一样的,它的目的是为用户提供他们所在的线和线的高度的视觉反馈。你应该在改变这一切之前好好想想,因为你不喜欢它的样子。

That said, the method in the SO post you linked above is the general approach for adjusting the cursor (though the adjusted rect's origin may need to be adjusted as well). What about it didn't work for you? It'd be better to start from there.

也就是说,上面链接的SO post中的方法是调整光标的通用方法(尽管调整后的rect的起源可能也需要调整)。它对你没用呢?最好从这里开始。

#2


0  

an answer related to this issue is given on this thread. just pasting here for future reference. iOS - UITextView lineSpacing make cursor height not same

在这个线程上给出了与这个问题相关的答案。粘贴在这里,供以后参考。iOS - UITextView行扫描使光标高度不同

"you could change cursor height by subclassing the UITextView, then override the caretRectForPosition:position function. For Example :

你可以通过子类化UITextView来改变光标的高度,然后覆盖caretRectForPosition:position函数。例如:

  • (CGRect)caretRectForPosition:(UITextPosition *)position { CGRect originalRect = [super caretRectForPosition:position]; originalRect.size.height = 18.0; return originalRect; } "
  • (CGRect)caretRectForPosition:(UITextPosition *)position {CGRect originalRect = [super caretRectForPosition:position];originalRect.size。身高= 18.0;返回originalRect;}”