您好我正在尝试从Label中找到角色的CGRect。我发现以下How do I locate the CGRect for a substring of text in a UILabel?有用,但代码在Objective C中。我尝试使用Swift语言。跟随Swift转换Objective C代码
var objLabel:UILabel = UILabel()
func getBoundingRectForCharacterRange(range:NSRange) -> CGRect {
var objTextStorage = NSTextStorage(attributedString: objLabel.attributedText!)
var objLayoutManager = NSLayoutManager()
objTextStorage.addLayoutManager(objLayoutManager)
var objTextContainer = NSTextContainer(size: CGSizeMake(100,100))
objTextContainer.lineFragmentPadding = 0
objLayoutManager.addTextContainer(objTextContainer)
var objRange:NSRange!
objLayoutManager.characterRangeForGlyphRange(range, actualGlyphRange: objRange)
return objLayoutManager.boundingRectForGlyphRange(objRange, inTextContainer: objTextContainer)
}
我正面临以下错误说
can not convert value of type 'NSRange!' to expected argument type 'NSRangePointer' (aka 'UnsafeMutablePointer<_NSRange>')
请帮助我解决问题。
此外,我尝试从一个类似的问题answer进行跟踪,但它也无效
var range : NSRange?
doSomethingWithRangePointer(&range)
func doSomethingWithRangePointer(range : UnsafeMutablePointer<NSRange?>) {
// Do stuff
}
答案 0 :(得分:-1)
将调用更改为objLayoutManager.characterRangeForGlyphRange(),如下所示:
objLayoutManager.characterRangeForGlyphRange(range,
actualGlyphRange: &objRange)