iOS-Coretext 图文混排占位符上下偏移处理

打印 上一主题 下一主题

主题 576|帖子 576|积分 1728

这里说的占位符,实际就是排版时需要展示的图片,图片基于占位符填充,那么处理图片时,怎么解决占位符(图片)的上下偏移
在设置占位符属性时,我通过以下方法来实现它:
  1. + (NSAttributedString *)wxImageAttributeCoreTextFromPaperQuestion:(WXTKCoretextQSourceImg *)image{
  2.    
  3.     CTRunDelegateCallbacks callbacks;
  4.     memset(&callbacks, 0, sizeof(CTRunDelegateCallbacks));
  5.     callbacks.version = kCTRunDelegateVersion1;
  6.     callbacks.getAscent = ascentCallbackPaper;
  7.     callbacks.getDescent = descentCallbackPaper;
  8.     callbacks.getWidth = widthCallbackPaper;
  9.     CTRunDelegateRef delegate = CTRunDelegateCreate(&callbacks, (__bridge void *)(image));
  10.     // 使用0xFFFC作为空白的占位符
  11.     unichar objectReplacementChar = 0xFFFC;
  12.     NSString * content = [NSString stringWithCharacters:&objectReplacementChar length:1];
  13.     NSMutableDictionary * attributes = [self wxAttributesPaperImg:image];
  14.     NSMutableAttributedString * space = [[NSMutableAttributedString alloc] initWithString:content attributes:attributes];
  15.     CFAttributedStringSetAttribute((CFMutableAttributedStringRef)space, CFRangeMake(0, 1),
  16.                                    kCTRunDelegateAttributeName, delegate);
  17.     CFRelease(delegate);
  18.     return space;
  19. }
复制代码
上述方法在引入 CTRunDelegateCallbacks 时,提供了控制占位符大小属性,即:getAscent、getDescent、getWidth
getWidth是占位符所取宽,getAscent与getDescent分别基于基准可上下偏移,一般情况,getDescent会提供返回0值,而getAscent一般是占位符(图片)的高度;下面通过设置不同数值,看下字符如何偏移;
向下不偏移,向上提供占位符高度
  1. ///占位基准上升度
  2. static CGFloat ascentCallbackPaper(void *ref){
  3.     WXTKCoretextQSourceImg *refP = (__bridge WXTKCoretextQSourceImg *)ref;
  4.     return refP.height;
  5. }
  6. ///占位基准下降度
  7. static CGFloat descentCallbackPaper(void *ref){
  8.     return 0;
  9. }
复制代码
视觉给我感觉默认不向下偏移,图片比左侧字符高一点点

向下偏移5,向上提供占位符高度 - 5
  1. ///占位基准上升度
  2. static CGFloat ascentCallbackPaper(void *ref){
  3.     WXTKCoretextQSourceImg *refP = (__bridge WXTKCoretextQSourceImg *)ref;
  4.     return refP.height - 5;
  5. }
  6. ///占位基准下降度
  7. static CGFloat descentCallbackPaper(void *ref){
  8.     return 5;
  9. }
复制代码

向下偏移10,向上提供占位符高度 - 10
  1. ///占位基准上升度
  2. static CGFloat ascentCallbackPaper(void *ref){
  3.     WXTKCoretextQSourceImg *refP = (__bridge WXTKCoretextQSourceImg *)ref;
  4.     return refP.height - 10;
  5. }
  6. ///占位基准下降度
  7. static CGFloat descentCallbackPaper(void *ref){
  8.     return 10;
  9. }
复制代码

向下偏移整个占位(图片)高度,向上提供占位符高度 0
  1. ///占位基准上升度
  2. static CGFloat ascentCallbackPaper(void *ref){
  3.     WXTKCoretextQSourceImg *refP = (__bridge WXTKCoretextQSourceImg *)ref;
  4.     return refP.height - refP.height;
  5. }
  6. ///占位基准下降度
  7. static CGFloat descentCallbackPaper(void *ref){
  8.     WXTKCoretextQSourceImg *refP = (__bridge WXTKCoretextQSourceImg *)ref;
  9.     return refP.height;
  10. }
复制代码

注意有个问题,上述的 getAscent、getDescent值加起来,其实就是图片的高度,那么如果比高度大或者小的情况下,图片会被拉伸,或者压缩
向下偏移小于整个占位(图片)高度( -10),向上提供占位符高度 0
  1. ///占位基准上升度
  2. static CGFloat ascentCallbackPaper(void *ref){
  3.     WXTKCoretextQSourceImg *refP = (__bridge WXTKCoretextQSourceImg *)ref;
  4.     return refP.height - refP.height;
  5. }
  6. ///占位基准下降度
  7. static CGFloat descentCallbackPaper(void *ref){
  8.     WXTKCoretextQSourceImg *refP = (__bridge WXTKCoretextQSourceImg *)ref;
  9.     return refP.height - 10;
  10. }
复制代码

总结

上下偏移要处理好图片的高度值,确保getAscent + getDescent = 占位符(图片)高度即可

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

正序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

西河刘卡车医

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表