AliSoftware / OHAttributedLabel

UILabel that supports NSAttributedString
https://github.com/AliSoftware/OHAttributedLabel/wiki
1.51k stars 344 forks source link

fix a bug about compute string size #142

Closed xiaou closed 10 years ago

xiaou commented 11 years ago

In NSAttributedString+Attributes.m, I find a bug about '-(CGSize)sizeConstrainedToSize:(CGSize)maxSize fitRange:(NSRange*)fitRange' function.

  1. how to find this bug: my text is @"2 3"; and max size is (width=255.5, height=3.40282e+38); (other attribute like font, you can try it~). then invoke this function, the return value's width is bigger than maxSize.width.
  2. 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

AliSoftware commented 10 years ago

Hi, and thanks for filing an issue for this. But I won't change and merge this because:

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)