iOS UITextView:增加行间距而不改变光标的高度? [英] iOS UITextView: Increase line spacing without changing the height of the cursor?

查看:580
本文介绍了iOS UITextView:增加行间距而不改变光标的高度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经把头撞在墙上太久了。我想创建双倍行距的输入文本,光标与字体的行高保持一致。

I've been banging my head against a wall on this for too long. I'd like to create double-spaced input text with the cursor staying the same as the font's line height.

我尝试过多种方法:


  • 使用NSLayoutManager的委托-layoutManager:lineSpacingAfterGlyphAtIndex:withProposedLineFragmentRect:

  • 使用属性字符串并设置NSParagraphStyle的-lineHeightMultiple和-lineSpacing属性

  • 使用也尝试设置该段落样式的自定义NSTextStorage。

。但是他们最终都得到了一个太大的克拉,要么延伸到上面的空白区域,要么延伸到每条线下面的空白区域。

...but they all end up with a carat that's just too big, either extending into the whitespace above or the whitespace below each line.

关于我能尝试什么的任何想法接下来实现这个布局?

Any thoughts on what I can try next to achieve this layout?

推荐答案

如果你已经有了所需的布局而问题只是插入符号的大小,你可以简单地子类化UITextView并覆盖以下方法:

If you already have desired layout and the problem is just size of caret, you can simply subclass UITextView and override the following method:

- (CGRect)caretRectForPosition:(UITextPosition *)position
{
    CGRect *originalRect = [super caretRectForPosition:position];
    // Resize the rect. For example make it 75% by height:
    originalRect.size.height *= 0.75;
    return originalRect;
}

这就行了,这对我来说都适用于ios6和7

This'll do the trick, this works on both ios6 and 7 for me

这篇关于iOS UITextView:增加行间距而不改变光标的高度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆