为什么这些UITextViews中的背景不清晰? [英] Why aren't the backgrounds within these UITextViews clear?

查看:157
本文介绍了为什么这些UITextViews中的背景不清晰?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将单个字符显示在与带字距的单个字符串一起显示的确切位置上.问题在于,字符的边界框似乎是不透明的,因此每个新添加的字符都覆盖了某些先前的字符.在字距调整较大的地方(例如,在"ToT"组合中),问题很明显:

I am attempting to display individual characters in the exact positions that they would appear if displayed as a single string with kerning. The problem is that the characters' bounding boxes seem to be opaque, so that each newly added character covers some of the prior one. Where kerning is greater (e.g., in the combination "ToT"), the problem is obvious:

我的设置是这样的:我在容器视图中嵌入了一个SKView.在SKView的视图控制器的扩展中,在函数中按以下顺序设置了以下内容:

My setup is something like this: I have an SKView embedded in a container view. In an extension of the SKView's view controller, the following are set within a function in this order:

skView.allowsTransparency = true
scene.backgroundColor = .clear

charAttr – [NSAttributedStringKey.backgroundColor: UIColor.clear]

textView.isOpaque = false
textView.backgroundColor = UIColor.clear

每个UITextView都作为子视图连续添加到该视图(这是一个SKView).

Each UITextView is added successively as a subview to the view (which is an SKView).

我一直在代码中寻找一些线索,以寻找使角色边界框变得不透明的原因,但是我什么也没发现.可悲的是,我去年某个时候解决了这个问题,但不记得自己做了什么,也没有代码了.

I've looked all over my code for some clue as to what could be making the character's bounding boxes seem opaque, but I haven't found anything. The sad thing is that I solved this problem sometime last year, but don't remember what I did and don't have the code anymore.

任何见解或想法都会受到赞赏.

Any insights or ideas would be appreciated.

推荐答案

在操场上实现了抢手的效果后,我将此简单代码粘贴到了原始代码所在的扩展中.它仍然有效,因此我使它与原始版本尽可能接近,直到它也出现了问题.

After achieving the sought-after effect in a playground, I pasted this simple code into the extension where the original code was. It still worked, so I made it as close to identical to the original as possible, until it also exhibited the problem.

SKView和扩展方面无关.问题在于UITextView框架属性如何处理字符宽度.这是相关的代码:

The SKView and extension aspects were irrelevant. The problem lies with how UITextView frames property deals with character widths. Here's the relevant code:

// charInfoArray contains (among other things) an attributed character, its origin,
// its size, and info about whether or not it should be displayed

// For each char, the origin and size info were derived from...
let currentCharRect = layoutManager.boundingRect(forGlyphRange: currentCharRange, in: textContainer)

// To display each (attributed) char of "ToT\n" . . .
for (index, charInfo) in charInfoArray.enumerated() {

    guard charInfo.displayed == true else { continue }

    let textView = UITextView()
    textView.backgroundColor = UIColor.clear
    textView.attributedText = charInfo.attrChar
    textView.textContainerInset = UIEdgeInsets.zero

    // let width = charInfo.size!.width
    let width = 30 // arbitrary width

    // Using the automatically generated charInfo.size!.width here
    // appears to make the text view's background opaque!
    textView.frame = CGRect(origin: charInfo.origin!, 
        size: CGSize(width: width, height: charInfo.size!.height))

    textView.frame = textView.frame.offsetBy(dx: 0.0, dy: offsetToCenterY)
    textView.textContainer.lineFragmentPadding = CGFloat(0.0)
    textView.backgroundColor = UIColor(red: 1, green: 0, blue: 0, alpha: 0.2)
    textView.textColor = UIColor.black
    view.addSubview(textView)

    print(charInfo.size!.width)
}

将宽度设置为width = charInfo.size!.width似乎会使文本视图的背景变得不透明!这可能是由于在序列末尾分配给换行符char的宽度过大造成的.为了消除该问题,我将不得不以另一种方式处理换行符.无论如何,我不知道为什么为什么会导致文本视图的背景变得不透明,但是确实如此.将宽度设置为任意值(例如30)可以消除问题.

Setting the width to width = charInfo.size!.width seems to make the text view's background opaque! This may be caused by the unusually large width assigned to newline char at the end of the sequence. To eliminate the problem, I'll have to deal with newline chars in another way. In any case, I have no idea why this causes the text view's background to turn opaque, but it does. Setting the width to an arbitrary value of, say, 30 eliminates problem.

以下是分别使用自动生成的宽度和手动设置的宽度的结果:

Here are the results of using automatically generated and manually set widths, respectively:

半透明的红色区域(在黄色背景上)显示每个字符(包括换行符)的边界矩形.

The translucent red areas (on yellow backgrounds) show the bounding rectangles for each of the characters, including the newline character.

这篇关于为什么这些UITextViews中的背景不清晰?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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