Ios 如何通过点击UITextView中的特定字符来显示圆形灰色背景?
我正在尝试使用UITextView显示文本。我在显示长文本时添加了“查看更多”。我想改变背景颜色时,点击它。我设置了NSAttributedString的背景,但是我不能很好地设置圆角和边距 谢谢 我想做什么强> 点击添加到UITextView中的字符时,具有足够边距和圆角的灰色背景 注意:已经可以点击字符了。这个问题是关于攻丝的效果 类似问题Ios 如何通过点击UITextView中的特定字符来显示圆形灰色背景?,ios,swift,uitextview,textkit,Ios,Swift,Uitextview,Textkit,我正在尝试使用UITextView显示文本。我在显示长文本时添加了“查看更多”。我想改变背景颜色时,点击它。我设置了NSAttributedString的背景,但是我不能很好地设置圆角和边距 谢谢 我想做什么 点击添加到UITextView中的字符时,具有足够边距和圆角的灰色背景 注意:已经可以点击字符了。这个问题是关于攻丝的效果 类似问题 在UITextView的文本中添加带圆角的背景色。此答案将为您的问题提供一些想法 逻辑: 在UITextView中,我添加了uitappesturer
在UITextView的文本中添加带圆角的背景色。此
答案
将为您的问题
提供一些想法
逻辑:
在UITextView
中,我添加了uitappesturerecognizer
,它通过Character
检测用户的点击动作Character
。如果用户点击子字符串
中的任何一个字符,将创建新的ui视图
,并触发计时器。当计时器结束时,创建的UIView将从UITextView中删除
借助于,myTextView.position
,我们可以得到子字符串的CGRect
。这是创建的UIView的框架<代码>大小(宽度)对于子字符串中的每个单词
,可以从SizeArributes
获取
@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()
}
}
}
}
@IBOutlet弱var challengeTextVw:UITextView!
let fullText=“我们喜欢快速和快速的属性文本”
var myString=nsmutableAttributeString()
let subString=“Swift属性文本”
var subStringSizesArr=[CGFloat]()
var myRange=NSRange()
var myWholeRange=NSRange()
让fontSize:CGFloat=25
var timerTxt=Timer()
设延迟=3.0
重写func viewDidLoad(){
super.viewDidLoad()
myString=nsmutableAttributeString(字符串:全文)
myRange=(全文为!NSString).range(of:subString)
myWholeRange=(全文为!NSString).range(of:fullText)
让substringseparatorar=subString.components(分隔符:“”)
打印(子字符串分隔符或RR)
打印(子字符串分隔符或总计数)
var strConcat=“”
对于0..1中的str
{
让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=(SubstringSizeSAR.last!-SubstringSizeSAR[secondViewWidthIndex])+2
secondView.backgroundColor=UIColor.brown
secondView.layer.cornerRadius=2
secondView.tag=20
backHideVw.frame.size.width=subStringSizesArr[secondViewWidthIndex]
myTextView.addSubview(第二视图)
打印(“secondView.framesecondView.frame”,secondView.frame)
myTextView.sendSubview(toBack:secondView)
}
打印(“rect”,rect)
}
}
}
@objc func delayedAction()
{
对于challengeTextVw.subview中的子VW
{
if(字符串(描述:subVws).range(of:“UIView”)!=nil
{
if(subVws as!UIView).tag==10 | |(subVws as!UIView).tag==20
{
subVws.removeFromSuperview()
}
}
}
}
通过增加字体大小
尝试的所有尝试
尝试1
尝试2
尝试3次
如果问题是关于获取点击字符的范围,则应使用
layoutManager.characterIndex(for:in:FractionofDistancebetween InsertionPoints:)
:
您还可以根据需要使用不同的UIgestureRecognitor
如果您的问题是关于如何为一系列文本绘制自定义背景,您可以使用layoutManager.boundingRect(forglyprange:in:)
:
注:
- 此外,还应保留对以这种方式创建的图层的引用,以便可以删除或重新格式化这些图层 <>代码> LayOutMault.Engulink ReCt(FoRyPrPHiLang:In:)/Case>不能在范围跨越多行时给出您想要的,因此在适当的时候考虑拆分范围。
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
}
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
}