如何通过点击UITextView中的特定字符来显示圆角灰色背景?

时间:2018-02-20 02:47:14

标签: ios swift uitextview textkit

我正在尝试使用UITextView显示文本。我在显示长文本时添加了“查看更多”。我想在点击它时改变背景颜色。我设置了NSAttributedString的背景,但我不能很好地设置圆角和边距。

谢谢!

我想做什么!

当点击添加到UITextView的角色时,灰色背景具有足够的边距和圆角。

注意:已经可以点按一个字符。这个问题是关于攻击的效果。

"See More" example

"Cross lines" example

类似问题

NSAttributedString background color and rounded corners

How to set NSString's background cornerRadius on iOS7

2 个答案:

答案 0 :(得分:0)

在UITextView的文本中添加带圆角的背景颜色。这个answer会为您的Question提供一些想法。

<强>逻辑:

UITextView中,我添加了UITapGestureRecognizer,用于Character检测用户的点击操作Character。如果用户点击subString中的任何一个字符,将创建新的UIView并触发Timer。当计时器结束时,将从UITextView中删除创建的UIView。

借助myTextView.position,我们可以获得subString的CGRect。这是Created UIView的框架。对于Size (WIDTH)中的每个字词,subString可以来自SizeAtrributes

@IBOutlet weak var challengeTextVw: UITextView!
let fullText = "We Love Swift and Swift attributed text "
var myString = NSMutableAttributedString ()
let subString = " Swift attributed text "
var subStringSizesArr = [CGFloat]()
var myRange = NSRange()
var myWholeRange = NSRange()
let fontSize : CGFloat = 25
var timerTxt = Timer()
let delay = 3.0


override func viewDidLoad() {
    super.viewDidLoad()

    myString = NSMutableAttributedString(string: fullText)
    myRange = (fullText as! NSString).range(of: subString)
    myWholeRange = (fullText as! NSString).range(of: fullText)
    let substringSeperatorArr = subString.components(separatedBy: " ")

    print(substringSeperatorArr)
    print(substringSeperatorArr.count)
    var strConcat = " "

    for str in 0..<substringSeperatorArr.count
    {

        strConcat = strConcat + substringSeperatorArr[str] + " "
        let textSize = (strConcat as! NSString).size(withAttributes: [NSAttributedStringKey.font : UIFont.systemFont(ofSize: fontSize)])
        print("strConcatstrConcat   ", strConcat)

        if str != 0 && str != (substringSeperatorArr.count - 2)
        {
             print("times")
            subStringSizesArr.append(textSize.width)
        }

    }
    let myCustomAttribute = [NSAttributedStringKey.init("MyCustomAttributeName") : "some value", NSAttributedStringKey.foregroundColor : UIColor.orange] as [NSAttributedStringKey : Any]
    let fontAtrib = [NSAttributedStringKey.font : UIFont.systemFont(ofSize: fontSize)]
    myString.addAttributes(myCustomAttribute, range: myRange)
    myString.addAttributes(fontAtrib, range: myWholeRange)

    challengeTextVw.attributedText = myString
    let tap = UITapGestureRecognizer(target: self, action: #selector(myMethodToHandleTap))
    tap.delegate = self

    challengeTextVw.addGestureRecognizer(tap)

    challengeTextVw.isEditable = false
    challengeTextVw.isSelectable = false
}


@objc func myMethodToHandleTap(_ sender: UITapGestureRecognizer) {

    let myTextView = sender.view as! UITextView
    let layoutManager = myTextView.layoutManager

    let numberOfGlyphs = layoutManager.numberOfGlyphs
    var numberOfLines = 0
    var index = 0
    var lineRange:NSRange = NSRange()

    while (index < numberOfGlyphs) {

        layoutManager.lineFragmentRect(forGlyphAt: index, effectiveRange: &lineRange)
        index = NSMaxRange(lineRange);
        numberOfLines = numberOfLines + 1

    }

    print("noLin  ", numberOfLines)

    // location of tap in myTextView coordinates and taking the inset into account
    var location = sender.location(in: myTextView)
    location.x -= myTextView.textContainerInset.left;
    location.y -= myTextView.textContainerInset.top;

    // character index at tap location
    let characterIndex = layoutManager.characterIndex(for: location, in: myTextView.textContainer, fractionOfDistanceBetweenInsertionPoints: nil)

    // if index is valid then do something.
    if characterIndex < myTextView.textStorage.length
    {
        // print the character index
        print("character index: \(characterIndex)")

        // print the character at the index
        let myRangee = NSRange(location: characterIndex, length: 1)
        let substring = (myTextView.attributedText.string as NSString).substring(with: myRangee)
        print("character at index: \(substring)")

        // check if the tap location has a certain attribute
        let attributeName = NSAttributedStringKey.init("MyCustomAttributeName")

        let attributeValue = myTextView.attributedText.attribute(attributeName, at: characterIndex, effectiveRange: nil) as? String

        if let value = attributeValue
        {
            print("You tapped on \(attributeName) and the value is: \(value)")
            print("\n\n ererereerer")


            timerTxt = Timer.scheduledTimer(timeInterval: delay, target: self, selector: #selector(delayedAction), userInfo: nil, repeats: false)

            myTextView.layoutManager.ensureLayout(for: myTextView.textContainer)

            // text position of the range.location
            let start = myTextView.position(from: myTextView.beginningOfDocument, offset: myRange.location)!
            // text position of the end of the range
            let end = myTextView.position(from: start, offset: myRange.length)!

            // text range of the range
            let tRange = myTextView.textRange(from: start, to: end)

            // here it is!
            let rect = myTextView.firstRect(for: tRange!)   //firstRectForRange(tRange)
            var secondViewWidthIndex = Int()
            for count in 0..<subStringSizesArr.count
            {
                if rect.width > subStringSizesArr[count]
                {
                    secondViewWidthIndex = count
                }
            }

            let backHideVw = UIView()
            backHideVw.frame.origin.x = rect.origin.x
            backHideVw.frame.origin.y = rect.origin.y + 1
            backHideVw.frame.size.height = rect.height
            backHideVw.frame.size.width = rect.width

            backHideVw.backgroundColor = UIColor.brown
            backHideVw.layer.cornerRadius = 2
            backHideVw.tag = 10
            myTextView.addSubview(backHideVw)
            myTextView.sendSubview(toBack: backHideVw)


            if numberOfLines > 1
            {
                let secondView = UIView()
                secondView.frame.origin.x = 0
                secondView.frame.origin.y = backHideVw.frame.origin.y + backHideVw.frame.size.height
                secondView.frame.size.height = backHideVw.frame.size.height
                secondView.frame.size.width = (subStringSizesArr.last! - subStringSizesArr[secondViewWidthIndex]) + 2
                secondView.backgroundColor = UIColor.brown
                secondView.layer.cornerRadius = 2
                secondView.tag = 20
                backHideVw.frame.size.width = subStringSizesArr[secondViewWidthIndex]

                myTextView.addSubview(secondView)
                print("secondView.framesecondView.frame    ", secondView.frame)

                myTextView.sendSubview(toBack: secondView)
            }

            print("rectrect    ", rect)

        }

    }

}

@objc func delayedAction()
{

    for subVws in challengeTextVw.subviews
    {
        if (String(describing: subVws).range(of:"UIView") != nil)
        {
            if (subVws as! UIView).tag == 10 || (subVws as! UIView).tag == 20
            {
                subVws.removeFromSuperview()
            }
        }
    }

}

通过增加Font Size来尝试所有尝试。

尝试1

enter image description here

尝试2

enter image description here

尝试3

enter image description here

答案 1 :(得分:0)

如果问题是关于获取点击字符的范围,则应使用layoutManager.characterIndex(for:in:fractionOfDistanceBetweenInsertionPoints:)

private var selectGestureRecognizer: UITapGestureRecognizer!

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    selectGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(textTapped))
    addGestureRecognizer(selectGestureRecognizer)
}

@objc func textTapped(recognizer: UITapGestureRecognizer) {
    guard recognizer == selectGestureRecognizer else {
        return
    }
    var location = recognizer.location(in: self)
    // https://stackoverflow.com/a/25466154/1033581
    location.x -= textContainerInset.left
    location.y -= textContainerInset.top
    let charIndex = layoutManager.characterIndex(for: location, in: textContainer, fractionOfDistanceBetweenInsertionPoints: nil)
    let range = NSRange(location: charIndex, length: 1)

    // do something with the tapped range
}

您还可以根据需要使用其他UIGestureRecognizer。

如果您的问题是关于如何为一系列文字绘制自定义背景,您可以使用layoutManager.boundingRect(forGlyphRange:in:)

func layoutBackground(range: NSRange) -> CALayer {
    let rect = boundingRectForCharacterRange(range)
    let backgroundLayer = CALayer()
    backgroundLayer.frame = rect
    backgroundLayer.cornerRadius = 5
    backgroundLayer.backgroundColor = UIColor.black.cgColor.copy(alpha: 0.2)
    layer.insertSublayer(backgroundLayer, at: 0)
    return backgroundLayer
}

/// CGRect of substring.
func boundingRectForCharacterRange(_ range: NSRange) -> CGRect {
    let layoutManager = self.layoutManager
    var glyphRange = NSRange()
    // Convert the range for glyphs.
    layoutManager.characterRange(forGlyphRange: range, actualGlyphRange: &glyphRange)
    var glyphRect = layoutManager.boundingRect(forGlyphRange: glyphRange, in: textContainer)
    // https://stackoverflow.com/a/28332722/1033581
    glyphRect.origin.x += textContainerInset.left
    glyphRect.origin.y += textContainerInset.top
    return glyphRect
}

注意:

  • 此外,您应该保留对以这种方式创建的图层的引用,以便您可以删除或重新构建它们。
  • 当范围跨越多行时,
  • layoutManager.boundingRect(forGlyphRange:in:)可能无法提供您想要的内容,因此请考虑在适当的时候拆分范围。