TYText
TYText copied to clipboard
某些文案宽度计算错误问题
视乎是系统的接口有问题。在出现阿拉伯这种神奇文本的时候,宽度计算有问题
在 >>不知道:ج<< 这样一串文案中 1.使用:
[self boundingRectForGlyphRange:[self visibleGlyphRange]].size;
计算出来的宽度是:124.4062158397378 (×)
2.使用
[_layoutManager usedRectForTextContainer:_textContainer].size
计算出来的宽度是:53.183025966320074 (√)
在 tytextrender 中代码打印相关信息:
- (CGSize)textSizeWithRenderWidth:(CGFloat)renderWidth {
if (!_textStorageOnRender) {
return CGSizeZero;
}
CGSize size = _textContainer.size;
_textContainer.size = CGSizeMake(renderWidth, MAXFLOAT);
[self.textStorage enumerateAttributesInRange:NSMakeRange(0, self.textStorage.length) options:0 usingBlock:^(NSDictionary<NSAttributedStringKey,id> * _Nonnull attrs, NSRange range, BOOL * _Nonnull stop) {
NSLog(@"textSizeWithRenderWidth:文本>>%@<<完 =>大小%@ ",[[self.textStorage attributedSubstringFromRange:range]string],NSStringFromCGRect([self boundingRectForGlyphRange:range]));
}];
CGSize textSize = [_layoutManager usedRectForTextContainer:_textContainer].size;// //[self boundingRectForGlyphRange:[self visibleGlyphRange]].size;
_textContainer.size = size;
return CGSizeMake(ceil(textSize.width), ceil(textSize.height));
}
(lldb) po self.textStorage
不知道{
NSBackgroundColor = "UIExtendedGrayColorSpace 0 0";
NSColor = "UIExtendedSRGBColorSpace 1 0 0 1";
NSFont = "<UICTFont: 0x12741a470> font-family: \".PingFangTC-Regular\"; font-weight: normal; font-style: normal; font-size: 14.00pt";
TYTextAttribute = "<TYTextAttribute: 0x283984fc0>";
TYTextHighlightAttribute = "<TYTextHighlight: 0x282384a40>";
}:{
NSFont = "<UICTFont: 0x125fc9e20> font-family: \"Helvetica\"; font-weight: normal; font-style: normal; font-size: 12.00pt";
}ج{
NSFont = "<UICTFont: 0x1272123a0> font-family: \"Geeza Pro\"; font-weight: normal; font-style: normal; font-size: 12.00pt";
}
2018-12-13 11:00:29.255466+0800 LivingRoomTemplate[25684:4754996] textSizeWithRenderWidth:文本>>不知道<<完 =>大小{{-1.008, -1.2879999999999985}, {45.332000000000008, 18.92655334538879}}
2018-12-13 11:00:29.255707+0800 LivingRoomTemplate[25684:4754996] textSizeWithRenderWidth:文本>>:<<完 =>大小{{31.431796875000003, -1.4189843749999991}, {28.7578125, 19.23046875}}
2018-12-13 11:00:29.255882+0800 LivingRoomTemplate[25684:4754996] textSizeWithRenderWidth:文本>>ج<<完 =>大小{{44.931669727622065, -2.303580470162748}, {78.466546112115736, 21.786618444846297}}
2018-12-13 11:00:13:908(+0800) [com.overseaSignalKit.thread.user-interactive] [HTVLivingRoomImpl(line:72)] [HTVChannel] SIGNAL, PushReceiver onChatBroadcast
(lldb) p [self boundingRectForGlyphRange:[self visibleGlyphRange]].size;
(CGSize) $1 = (width = 124.4062158397378, height = 21.786618444846297)
(lldb) p [_layoutManager usedRectForTextContainer:_textContainer].size
(CGSize) $2 = (width = 53.183025966320074, height = 17.638553345388789)
(lldb)