Closed xiaou closed 10 years ago
Hi, and thanks for filing an issue for this. But I won't change and merge this because:
CTFramesetterSuggestFrameSizeWithConstraints
. You should really file a bug to Apple (bugreport.apple.com) instead so that Apple fixes it in next iOS releasesOHAttributedLabel
as sizeToFit
/sizeThatFits:
methods of OHAttributedLabel
are based on this method, and if you call e.g. [label sizeThatFits:CGSizeMake(10,10)]
with a font which lineHeight
is greater than 10
, with your patch it will return a height of 10
for the label whereas it should return the font's lineHeight
(as it does for now), exactly the same way as UILabel
does. As stated in the sizeThatFits:
documentation, this method may return a size greater than the maxSize
in some cases that make sense, like when the maxSize.height
is smaller than even one line of text, etcCTFramesetterSuggestFrameSizeWithConstraints
methods tells you that it needs a size of 260x100 whereas you asked for a maxSize
of 255x200, your fix will return 255x100
which is probably totally wrong, as reducing the width for the text rendering will likely provoke text wrapping, leading to some text going to the next line and probably add one extra line and takes not 100 but something like 120 in height. So instead of returning a size that is greater than your maxSize
but makes sense and is the real size the string would take when drawn, you instead return a totally wrong size with your fix.So changing this will totally break the behavior and moreover won't return values that make sense and reflect a real size, that's why I have to dismiss your fix. Please file a bugreport to Apple instead. (You can still tweak the size returned by this method by yourself in your own app code if you really need to alter it for your specific purpose, but still take into account the last bullet I wrote above)
Note: for the next time please create a pull request instead of pasting the code (without formatting) in the issue, I can merge much more easily if you do that (so your fix would have been included eariler in the master branch)
In NSAttributedString+Attributes.m, I find a bug about '-(CGSize)sizeConstrainedToSize:(CGSize)maxSize fitRange:(NSRange*)fitRange' function.
what I do to fix it: -(CGSize)sizeConstrainedToSize:(CGSize)maxSize fitRange:(NSRange)fitRange { CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)self); CFRange fitCFRange = CFRangeMake(0,0); CGSize sz = CTFramesetterSuggestFrameSizeWithConstraints(framesetter,CFRangeMake(0,0),NULL,maxSize,&fitCFRange); if (framesetter) CFRelease(framesetter); if (fitRange) fitRange = NSMakeRange(fitCFRange.location, fitCFRange.length);
CGSize res = CGSizeMake( floorf(sz.width+1) , floorf(sz.height+1) );
// now I fixed the result(It made a bug). -add by U. if(res.width > maxSize.width) res.width = maxSize.width; if(res.height > maxSize.height) res.height = maxSize.height;
return res; }
——that's what I do, maybe not the best resolution. ——xiaou_is_me@163.com