简体   繁体   English

为什么这些UITextViews中的背景不清晰?

[英]Why aren't the backgrounds within these UITextViews clear?

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 (eg, in the combination "ToT"), the problem is obvious: 在字距调整较大的地方(例如,在“ ToT”组合中),问题很明显:

在此处输入图片说明

My setup is something like this: I have an SKView embedded in a container view. 我的设置是这样的:我在容器视图中嵌入了一个SKView。 In an extension of the SKView's view controller, the following are set within a function in this order: 在SKView的视图控制器的扩展中,在函数中按以下顺序设置了以下内容:

skView.allowsTransparency = true
scene.backgroundColor = .clear

charAttr – [NSAttributedStringKey.backgroundColor: UIColor.clear]

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

Each UITextView is added successively as a subview to the view (which is an SKView). 每个UITextView都作为子视图连续添加到该视图(这是一个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. 它仍然有效,因此我使它与原始版本尽可能接近,直到它也出现了问题。

The SKView and extension aspects were irrelevant. SKView和扩展方面无关。 The problem lies with how UITextView frames property deals with character widths. 问题在于UITextView框架属性如何处理字符宽度。 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)
}

Setting the width to width = charInfo.size!.width seems to make the text view's background opaque! 将width设置为width = charInfo.size!.width似乎会使文本视图的背景变得不透明! This may be caused by the unusually large width assigned to newline char at the end of the sequence. 这可能是由于在序列末尾分配给换行符char的宽度过大造成的。 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. 将宽度设置为任意值(例如30)可以消除问题。

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. 半透明的红色区域(在黄色背景上)显示每个字符(包括换行符)的边界矩形。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM