UITextView lineHeightMultiple剪辑文本的顶部第一行 [英] UITextView lineHeightMultiple Clips Top, first line, of Text

查看:148
本文介绍了UITextView lineHeightMultiple剪辑文本的顶部第一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在iOS 8中,我有一个vanilla UITextView ,当 lineHeightMultiple lineHeightMultiple 时,它会剪切第一行的顶部应用于它的 NSMutableParagraphStyle ,见下图:

In iOS 8, I have a vanilla UITextView that clips the top of the 1st line when a lineHeightMultiple is applied to it's NSMutableParagraphStyle, see image below:

好像 lineHeightMultiple 会影响第一行文字以及后续行。

It appears as though lineHeightMultiple affects the 1st line of text in addition to subsequent lines.

上设置 clipsToBounds = false UITextView 将至少启用剪裁部分显示,但您可以从下图中看到,现在文本的顶部显然位于其框架上方:

Setting clipsToBounds = false on the UITextView will at least enable the clipped part to show, but you can see from the image below that now the top part of the text is obviously above it's frame:

I只需在有问题的 UITextView 上设置顶部约束来补偿 clipsToBounds = false 就可以解决这个问题,但感觉就像是黑客攻击我。

I can fix this by just setting the top constraint on the offending UITextView to compensate for clipsToBounds = false but that feels like a hack to me.

我也试过使用 WKWebView 用于违规文本,只需设置css line-height 属性,这样就可以了。但是,对于UITextView,我必须简单地找到一些东西。

I have also tried using a WKWebView for the offending text, and just setting the css line-height property, and that works just fine. There must simply be something I am missing when it comes to UITextView though.

另外,在段落上设置 lineSpacing 样式小于0对文档没有影响:

Additionally, setting lineSpacing on the paragraph style to less than 0 has no affect, per the docs:

The distance in points between the bottom of one line fragment 
and the top of the next.

**This value is always nonnegative.**

This value is included in the line fragment heights in the 
layout manager.

我也试过设置 contentInset UITextView 以及使用系统字体都没有影响。

I have also tried setting the contentInset of the UITextView as well as using a system font, both had not affect.

我的示例代码如下:

let text = "THIS IS A MULTILINE RUN OF TEXT"

let font = UIFont(name: "MyFontName", size: 31.0)!
// let font = UIFont.systemFontOfSize(31.0)

let paragraph = NSMutableParagraphStyle()
paragraph.lineHeightMultiple = 0.75
paragraph.alignment = NSTextAlignment.Center

let attributes = [
    NSParagraphStyleAttributeName: paragraph,
              NSFontAttributeName: font
]

titleView.attributedText = NSAttributedString(string: text, attributes: attributes)

// titleView.contentInset = UIEdgeInsets(top: 50.0, left: 0.0, bottom: 0.0, right: 0.0)
titleView.clipsToBounds = false

有没有人遇到过这个并且克服了或者是最重要的限制黑客唯一的出路?

Has anyone encountered this and overcome or is the top constraint hack the only way to go?

推荐答案

我遇到了同样的问题。

我使用NSBaselineOffsetAttributeName补偿了它。

I compensated it using NSBaselineOffsetAttributeName.

你应该使用:

let attributes = [
    NSParagraphStyleAttributeName: paragraph,
    NSFontAttributeName: font,
    NSBaselineOffsetAttributeName: -5
]

您还必须设置较低的paragraph.lineHeightMultiple。

You will have to also set a lower paragraph.lineHeightMultiple.

有点棘手,但确实有效。

A little tricky, but it works.

这篇关于UITextView lineHeightMultiple剪辑文本的顶部第一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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