Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/100.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.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 boundingRectForGlyphRange始终返回字体大小为12的rect_Ios_Swift_Textkit - Fatal编程技术网

Ios boundingRectForGlyphRange始终返回字体大小为12的rect

Ios boundingRectForGlyphRange始终返回字体大小为12的rect,ios,swift,textkit,Ios,Swift,Textkit,我正在尝试使用boundingrectforglyprange方法计算单个glyph的矩形。一切正常,只是它总是返回rect,就好像字体大小是12一样。事件,但我在属性字符串中将其设置为20 let font = UIFont.systemFontOfSize(20) var attributedString = NSMutableAttributedString(string: text) let range = NSRange(location: 0, length: countElemen

我正在尝试使用
boundingrectforglyprange
方法计算单个glyph的矩形。一切正常,只是它总是返回rect,就好像字体大小是12一样。事件,但我在属性字符串中将其设置为20

let font = UIFont.systemFontOfSize(20)
var attributedString = NSMutableAttributedString(string: text)
let range = NSRange(location: 0, length: countElements(text))
attributedString.addAttribute(NSFontAttributeName, value: font, range: range)


let textContainer = NSTextContainer()
textContainer.size = CGSize(width: 300, height: 100)
let layoutManager = NSLayoutManager()
let textStorage = NSTextStorage(string: attributedString.mutableString)

layoutManager.textStorage = textStorage
layoutManager.addTextContainer(textContainer)

let rect = layoutManager.boundingRectForGlyphRange(NSRange(location: 0, length: 1), inTextContainer: textContainer)

如何使文本大小为20而不是12时返回rect?

只需使用
attributedString:
初始化
NSTextStorage

let textStorage = NSTextStorage(attributedString: attributedString)
而不是

let textStorage = NSTextStorage(string: attributedString.mutableString)