-
Notifications
You must be signed in to change notification settings - Fork 593
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Wrong bounding rect for some strings when using "FS Albert Pro" font #121
Comments
Unfortunately CTFramesetter may return wrong results as well, but for height value. Playground code (you need to add FS Albert Pro font to your playground resources): import UIKit
func calculateSizes(string: NSAttributedString) {
let maxSize = CGSize(width: 180, height: CGFloat.max)
let stringSize = string.boundingRectWithSize(maxSize, options: [.UsesLineFragmentOrigin, .UsesFontLeading], context: nil).size
print("boundingRect: \(stringSize)")
let range = CFRangeMake(0, string.length)
let framesetter = CTFramesetterCreateWithAttributedString(string)
let framesetterSize = CTFramesetterSuggestFrameSizeWithConstraints(framesetter, range, nil, maxSize, nil)
print("framesetter: \(framesetterSize)")
}
let text = "Мруа паваЫотрПрпв."
// System Font
print("System font:")
let attributes = [NSFontAttributeName: UIFont.systemFontOfSize(16)]
calculateSizes(NSAttributedString(string: text, attributes: attributes))
// FS Albert Pro font
print()
print("FS Albert Pro font:")
let url = NSBundle.mainBundle().URLForResource("FS Albert Pro", withExtension: "otf")
CTFontManagerRegisterFontsForURL(url!, CTFontManagerScope.Process, nil)
calculateSizes(NSAttributedString(string: text, attributes: [NSFontAttributeName: UIFont(name: "FSAlbertPro", size: 16)!])) will produce next output:
As you can see, using CTFramesetter will solve the issue for "FS Albert Pro" font (all values are bigger than values returned from |
@AntonPalich I couldn't help myself investigating this interesting issue... Take a look at this rendering comparison between UILabel and UITextView. UITextView's text is wider, and the distance in between "р" and "у" is slightly bigger, which leads to text kerning: https://www.objc.io/issues/5-ios7/getting-to-know-textkit/ The following seems to solve the issue, although I didn't test it thoroughly with different font faces and sizes... @@ -237,7 +237,10 @@ private final class TextBubbleLayoutModel {
return self.layoutContext.text.boundingRectWithSize(
CGSize(width: width, height: CGFloat.max),
options: [.UsesLineFragmentOrigin, .UsesFontLeading],
- attributes: [NSFontAttributeName: self.layoutContext.font], context: nil
+ attributes: [
+ NSFontAttributeName: self.layoutContext.font,
+ NSKernAttributeName: 0,
+ ], context: nil
).size.bma_round()
}
} ps: our size calculation should match UITextView.sizeThatFits(_:) after rounding. We're using boundingRectWithSize because it's way faster and UITextView can't be used in the background. |
@diegosanchezr Great! This solved the issue, i'll revert previous changes |
boundingRectWithSize(_:options:attributes:context:)
returns wrong bounding rect for string "Мруа паваЫотрПрпв." with font FS Albert Pro of size 16.00AR: Width is equal 150
ER: Width is equal 151
A possible solution would be to use CTFramesetter to calculate string sizes because it returns correct width in this case
The text was updated successfully, but these errors were encountered: