【问题标题】:Height of string when unicode text is in the string in iPhoneiPhone中Unicode文本在字符串中时字符串的高度
【发布时间】:2012-07-26 08:23:52
【问题描述】:
当我尝试使用具有 unicode 文本的此函数时,它会根据字体大小返回错误的高度作为其计算高度,但与没有 unicode 的情况相比,表情符号会变得更大并且占用更多的高度。
[文本 sizeWithFont:[UIFont systemFontOfSize:12.0] constrainedToSize:CGSizeMake(233.0f, 999.0f) lineBreakMode:UILineBreakModeWordWrap];
字符串中包含 unicode 可以计算真实高度吗?
【问题讨论】:
标签:
iphone
ios
xcode
unicode
【解决方案1】:
看看 CoreText 框架。
你可以试试这样的:
CTFrameSetterRef framesetter = CTFramesetterCreateWithAttributedString(myAttributedString);
CGSize size = CTFramesetterSuggestFrameSizeWithConstraints(frame setter, CFRangeMake(0, stringLength), NULL, CGSizeMake(maxWidth, maxHeight), NULL);
这应该会为您提供正确的尺寸。您当然可以使 maxHeight 任意大,以确保字符串适合最大大小。或者,您可以将指向 CFRange 的指针作为最后一个参数传递,它会将其设置为能够适应您传递的最大大小的字符串范围。
希望这对你有用:)