Ios UITextView的顶行具有不同的文本颜色

Ios UITextView的顶行具有不同的文本颜色,ios,swift,uitextview,nsattributedstring,Ios,Swift,Uitextview,Nsattributedstring,我希望UITextView的顶行颜色与文本的其余部分不同,但我看不到一种方法。我发现我可以在添加文本时为文本添加属性文本特征,但当我添加更多文本时,它将不会对齐 此外,我还尝试通过以下代码创建此效果,但它无法正常工作 if let containerView = textView.superview { let gradient = CAGradientLayer(layer: containerView.layer) gradient.frame = containerView

我希望
UITextView
的顶行颜色与文本的其余部分不同,但我看不到一种方法。我发现我可以在添加文本时为文本添加属性文本特征,但当我添加更多文本时,它将不会对齐

此外,我还尝试通过以下代码创建此效果,但它无法正常工作

if let containerView = textView.superview {
    let gradient = CAGradientLayer(layer: containerView.layer)
    gradient.frame = containerView.bounds
    gradient.colors = [UIColor.clearColor().CGColor, UIColor.blueColor().CGColor]
    gradient.startPoint = CGPoint(x: 0.0, y: 1.0)
    gradient.endPoint = CGPoint(x: 0.0, y: 0.85)
    containerView.layer.mask = gradient
}

尝试一下,代码中有注释:

class ViewController: UIViewController, UITextViewDelegate {
    @IBOutlet weak var textView: UITextView!

    // Attributes for the first line. Here we set it to blue
    let firstLineAttributes = [
        NSForegroundColorAttributeName: UIColor.blueColor(),
        NSFontAttributeName: UIFont.systemFontOfSize(14)
    ]

    override func viewDidLoad() {
        super.viewDidLoad()
        self.textView.delegate = self
    }

    // The intial highlight of the first line
    override func viewDidAppear(animated: Bool) {
        textViewDidChange(self.textView)
    }

    func textViewDidChange(textView: UITextView) {
        // Get the range of the chacrters on the first line
        var firstLineRange = NSMakeRange(NSNotFound, 0)
        withUnsafeMutablePointer(&firstLineRange) {
            textView.layoutManager.lineFragmentRectForGlyphAtIndex(0, effectiveRange: $0)
        }

        guard firstLineRange.location != NSNotFound && firstLineRange.length > 0 else {
            return
        }

        let textStorage = textView.textStorage

        // Remove color for the whole UITextView
        // You should call as many `removeAttribute` as needed to udno the attributes set in `firstLineAttributes`
        textStorage.removeAttribute(NSForegroundColorAttributeName, range: NSMakeRange(0, textView.text.characters.count))

        // Set attributes for the first line
        textStorage.setAttributes(self.firstLineAttributes, range: firstLineRange)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        //Dispose of resources that can be re created.
    }
}

尝试一下,代码中有注释:

class ViewController: UIViewController, UITextViewDelegate {
    @IBOutlet weak var textView: UITextView!

    // Attributes for the first line. Here we set it to blue
    let firstLineAttributes = [
        NSForegroundColorAttributeName: UIColor.blueColor(),
        NSFontAttributeName: UIFont.systemFontOfSize(14)
    ]

    override func viewDidLoad() {
        super.viewDidLoad()
        self.textView.delegate = self
    }

    // The intial highlight of the first line
    override func viewDidAppear(animated: Bool) {
        textViewDidChange(self.textView)
    }

    func textViewDidChange(textView: UITextView) {
        // Get the range of the chacrters on the first line
        var firstLineRange = NSMakeRange(NSNotFound, 0)
        withUnsafeMutablePointer(&firstLineRange) {
            textView.layoutManager.lineFragmentRectForGlyphAtIndex(0, effectiveRange: $0)
        }

        guard firstLineRange.location != NSNotFound && firstLineRange.length > 0 else {
            return
        }

        let textStorage = textView.textStorage

        // Remove color for the whole UITextView
        // You should call as many `removeAttribute` as needed to udno the attributes set in `firstLineAttributes`
        textStorage.removeAttribute(NSForegroundColorAttributeName, range: NSMakeRange(0, textView.text.characters.count))

        // Set attributes for the first line
        textStorage.setAttributes(self.firstLineAttributes, range: firstLineRange)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        //Dispose of resources that can be re created.
    }
}

显示您的应答编码定义不对齐显示您的应答编码定义不对齐