63451

Why aren't the backgrounds within these UITextViews clear?

Question:

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:

<a href="https://i.stack.imgur.com/8kb5p.png" rel="nofollow"><img alt="enter image description here" class="b-lazy" data-src="https://i.stack.imgur.com/8kb5p.png" data-original="https://i.stack.imgur.com/8kb5p.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" /></a>

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

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.

Answer1:

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. 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) }

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 <em>why</em> 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:

<a href="https://i.stack.imgur.com/yu3oQ.png" rel="nofollow"><img alt="enter image description here" class="b-lazy" data-src="https://i.stack.imgur.com/yu3oQ.png" data-original="https://i.stack.imgur.com/yu3oQ.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" /></a> <a href="https://i.stack.imgur.com/07myN.png" rel="nofollow"><img alt="enter image description here" class="b-lazy" data-src="https://i.stack.imgur.com/07myN.png" data-original="https://i.stack.imgur.com/07myN.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" /></a>

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

Recommend

  • DataGridView DataGridViewButtonColumn doesn't notice a real Button
  • Split pipe delimited into new columns
  • Custom Radio Buttons - IE will kill me
  • using QuasirandomGenerator (for dummies)
  • Swift: How can I detect elements inside of a UIView?
  • (iphone) showing activity indicator before heavy ui updating
  • Custom font for MKAnnotationView Callout
  • FOSUserBundle force user to write a different password
  • C++ stl pop doesn't return [closed]
  • How to remove last utf8 char of a python string
  • Installing SSL on AWS EC2 Bitnami Mean Stack
  • Unable to set a breakpoint on main while debugging a program compiled with Rust 1.10 with GDB
  • Typecasting `this` of a base class template to its derived class
  • how do you obtain the address of an instance after overriding the __str__ method in python
  • Get localized short date pattern as String?
  • jQuery: add elements until a particular height is reached
  • uml Composition relationships to RDF and OWL
  • How to programatically 'login' a user based on 'remember me' cookie when using j
  • Casting between Interfaces and Classes
  • Azure table store snapshot/backup capability
  • Invalid Date on validation Date of js
  • Multicolored edittext hint
  • Copy to all folders batch file?
  • MySQL Order by column = x, column asc?
  • Inline R code in YAML for rmarkdown doesn't run
  • xtable package: Skipping some rows in the output
  • What and where is mdimport
  • QLPreviewController hide print button in ios6
  • Change JButton Shape while respecting Look And Feel
  • TFS: Get latest causes slow project reloading
  • Updated Ionic CLI but shows previous version (Windows)
  • Google cloud sdk not working when python points python3
  • Transpose CSV data with awk (pivot transformation)
  • Acquiring multiple attributes from .xml file in c#
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • How can I remove ASP.NET Designer.cs files?
  • Append folder name and increment by 1 using batch script
  • Android Heatmap on canvas or ImageView
  • Conditional In-Line CSS for IE and Others?
  • java string with new operator and a literal