Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在TextView中查找子字符串的CGPoint位置_Ios_Swift_Sprite Kit_Uitextview_Nsattributedstring - Fatal编程技术网

Ios 在TextView中查找子字符串的CGPoint位置

Ios 在TextView中查找子字符串的CGPoint位置,ios,swift,sprite-kit,uitextview,nsattributedstring,Ios,Swift,Sprite Kit,Uitextview,Nsattributedstring,我希望创建一个SpriteKit节点,该节点位于UITextView中的子字符串位置。如何检索CGPoint位置,以便将SKNode定位到该位置 let textFont = [NSFontAttributeName: UIFont(name: "GillSansMT", size: 30.0) ?? UIFont.systemFontOfSize(18.0)] attrString1 = NSMutableAttributedString(string: "My name is

我希望创建一个SpriteKit节点,该节点位于UITextView中的子字符串位置。如何检索CGPoint位置,以便将SKNode定位到该位置

    let textFont = [NSFontAttributeName: UIFont(name: "GillSansMT", size: 30.0) ?? UIFont.systemFontOfSize(18.0)]
    attrString1 = NSMutableAttributedString(string: "My name is Dug.", attributes: textFont)
    textShown1 = CustomTextView(frame: CGRectMake(CGRectGetMidX(self.frame), 175 + (90 * paragraphNumber), CGRectGetWidth(self.frame) - 80, CGRectGetHeight(self.frame)-400))
    textShown1.attributedText = attrString1

    self.view?.addSubview(textShown1)

您可以在
UITextView

let textFont = [NSFontAttributeName: UIFont(name: "GillSansMT", size: 30.0) ?? UIFont.systemFontOfSize(18.0)]
let attrString1 = NSMutableAttributedString(string: "My name is Dug.", attributes: textFont)

// range of substring to search
let str1 = attrString1.string as NSString
let range = str1.rangeOfString("name", options: nil, range: NSMakeRange(0, str1.length))

// prepare the textview
let textView = UITextView(frame:CGRectMake(0,0,200,200))
textView.attributedText = attrString1

// you should ensure layout
textView.layoutManager.ensureLayoutForTextContainer(textView.textContainer)

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

// text range of the range
let tRange = textView.textRangeFromPosition(start, toPosition: end)

// here it is!
let rect = textView.firstRectForRange(tRange)

我在你的问题中没有看到任何“子串”。你想做什么?对不起,我举了个例子。我希望能够找到属性字符串“name”中的“n”在textView中的坐标位置。原因是,我想在那个位置创建一个SKEmitter粒子节点。什么是
CustomTextView
?它是
UITextView
的子类吗?如果是这样,它是否会修改
UITextView
绘制文本的方式?因此,您可以使用文本工具包轻松完成此操作。它告诉你文本的所有部分在哪里。这并不是你想要做什么的一个例子,但它说明了如何在文本视图中处理文本的几何图形:谢谢。工作得很有魅力。我可以问一下为什么需要确保布局吗?当您修改
UITextView
的任何属性时,它在下一个绘图循环之前都没有有效的文本布局
ensureLayoutForTextContainer()
可以强制这样做。再次感谢,如果我能再问一个问题,我正在尝试在全局视图中找到rect的位置。我正在将包含UITextView的rect的frame.minY和minX添加到字符串的rect的x和y值中。x有效,y很差。想知道为什么吗?哎呀,忘了提了@我认为这是因为UIKit和SpriteKit之间的坐标系统不匹配。也许这个答案可以帮助你: