You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

iOS Swift:选中JLAttributedTextView文本后内容全部变白问题求助

Fixing "All Text Turns White When Selected" in JLAttributedTextView

Hey there! Let's figure out why your JLAttributedTextView (inside JLStickerTextView) turns all white when text is selected. This is a common issue tied to how the text view handles attributed string styles during selection, so let's break down the fixes step by step.

1. Explicitly Set Selection Text Attributes

The most likely culprit is missing configuration for how text should look when selected. UITextView has a selectedTextAttributes property that lets you define the foreground and background color for selected text. Add this to your JLAttributedTextView setup:

// In your JLAttributedTextView subclass or initialization code
override init(frame: CGRect, textContainer: NSTextContainer?) {
    super.init(frame: frame, textContainer: textContainer)
    setupSelectionStyles()
}

required init?(coder: NSCoder) {
    super.init(coder: coder)
    setupSelectionStyles()
}

private func setupSelectionStyles() {
    // Set the tint color (cursor and selection highlight border color)
    self.tintColor = UIColor.systemBlue
    
    // Define exactly how selected text should appear
    self.selectedTextAttributes = [
        NSAttributedString.Key.foregroundColor: UIColor.white, // Adjust to your preferred text color
        NSAttributedString.Key.backgroundColor: UIColor.systemBlue // Adjust to your preferred highlight color
    ]
}

This overrides any default or inherited styles that might be forcing the text to turn white without a visible background.

2. Check for Conflicting Attributed String Attributes

Sometimes the base attributed text you're setting might have attributes that clash with selection styles. For example, if your normal text uses a white color, but the selection background is also light, it could look like everything is white. Verify your attributed string setup:

// Example of creating a safe attributed string
let yourText = "Sample text for JLAttributedTextView"
let baseAttributes: [NSAttributedString.Key: Any] = [
    .foregroundColor: UIColor.black, // Ensure normal text color is visible
    .font: UIFont.systemFont(ofSize: 16)
]
let attributedText = NSAttributedString(string: yourText, attributes: baseAttributes)
yourAttributedTextView.attributedText = attributedText

Make sure there's no logic that resets the text color when the selection changes.

3. Inspect JLStickerTextView's Custom Logic

Since JLAttributedTextView is part of the JLStickerTextView component, check if the parent class or the subclass has custom code that modifies text attributes on selection:

  • Look for overrides of textViewDidChangeSelection(_:) in any delegate implementations
  • Check if layoutSubviews or textDidChange methods are altering the attributed text's color
  • If this is a third-party library, browse its source code for references to selectedTextRange or forced color changes

4. Debug the Attributed Text in Selection State

To pinpoint the issue, add a breakpoint in the textViewDidChangeSelection(_:) delegate method and print the attributed text's properties:

func textViewDidChangeSelection(_ textView: UITextView) {
    guard let attributedText = textView.attributedText else { return }
    attributedText.enumerateAttributes(in: NSRange(location: 0, length: attributedText.length), options: []) { attributes, range, stop in
        print("Range \(range): Attributes \(attributes)")
    }
}

This will show you if the foregroundColor is being set to white across all ranges when selection happens, which would confirm where the override is occurring.


内容的提问来源于stack exchange,提问作者Ahsan Aasim

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 04:13:04