线上所有马甲包模板,与《猎豆》同UI。域名zhuadd

MLTextAttachment.m 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. //
  2. // MLTextAttachment.m
  3. // MLLabel
  4. //
  5. // Created by molon on 15/6/11.
  6. // Copyright (c) 2015年 molon. All rights reserved.
  7. //
  8. #import "MLTextAttachment.h"
  9. @interface MLTextAttachment()
  10. @property (nonatomic, assign) CGFloat width;
  11. @property (nonatomic, assign) CGFloat height;
  12. @property (nonatomic, assign) CGFloat lineHeightMultiple;
  13. @property (nonatomic, assign) CGFloat imageAspectRatio;
  14. @property (nonatomic, copy) UIImage * (^imageBlock)(CGRect imageBounds,NSTextContainer *textContainer,NSUInteger charIndex,MLTextAttachment *textAttachment);
  15. @end
  16. @implementation MLTextAttachment
  17. + (instancetype)textAttachmentWithWidth:(CGFloat)width height:(CGFloat)height imageBlock:(UIImage * (^)(CGRect imageBounds,NSTextContainer *textContainer,NSUInteger charIndex,MLTextAttachment *textAttachment))imageBlock
  18. {
  19. MLTextAttachment *textAttachment = [MLTextAttachment new];
  20. textAttachment.width = width;
  21. textAttachment.height = height;
  22. textAttachment.imageBlock = imageBlock;
  23. return textAttachment;
  24. }
  25. + (instancetype)textAttachmentWithLineHeightMultiple:(CGFloat)lineHeightMultiple imageBlock:(UIImage * (^)(CGRect imageBounds,NSTextContainer *textContainer,NSUInteger charIndex,MLTextAttachment *textAttachment))imageBlock
  26. imageAspectRatio:(CGFloat)imageAspectRatio
  27. {
  28. MLTextAttachment *textAttachment = [MLTextAttachment new];
  29. textAttachment.lineHeightMultiple = lineHeightMultiple;
  30. textAttachment.imageBlock = imageBlock;
  31. textAttachment.imageAspectRatio = imageAspectRatio;
  32. return textAttachment;
  33. }
  34. //重写以绘制
  35. - (UIImage *)imageForBounds:(CGRect)imageBounds textContainer:(NSTextContainer *)textContainer characterIndex:(NSUInteger)charIndex
  36. {
  37. if (self.imageBlock)
  38. {
  39. return self.imageBlock(imageBounds,textContainer,charIndex,self);
  40. }
  41. return [super imageForBounds:imageBounds textContainer:textContainer characterIndex:charIndex];
  42. }
  43. //重写以返回附件的大小
  44. - (CGRect)attachmentBoundsForTextContainer:(NSTextContainer *)textContainer proposedLineFragment:(CGRect)lineFrag glyphPosition:(CGPoint)position characterIndex:(NSUInteger)charIndex
  45. {
  46. if (self.imageBlock)
  47. {
  48. CGFloat width = self.width;
  49. CGFloat height = self.height;
  50. // 找到其是否有设置字体,如果有,就根据字体的descender调整下位置,以及lineHeight调整大小
  51. UIFont *font = [textContainer.layoutManager.textStorage attribute:NSFontAttributeName
  52. atIndex:charIndex
  53. effectiveRange:nil];
  54. CGFloat baseLineHeight = (font?font.lineHeight:lineFrag.size.height);
  55. if (self.lineHeightMultiple>0) {
  56. width = height = baseLineHeight*self.lineHeightMultiple;
  57. if (self.imageAspectRatio>0) {
  58. width = height*self.imageAspectRatio;
  59. }
  60. }else{
  61. if (width==0&&height==0) {
  62. width = height = lineFrag.size.height;
  63. }else if (width==0&&height!=0) {
  64. width = height;
  65. }else if (height==0&&width!=0) {
  66. height = width;
  67. }
  68. }
  69. CGFloat y = font.descender;
  70. y -= (height-baseLineHeight)/2;
  71. return CGRectMake(0, y, width, height);
  72. }
  73. return [super attachmentBoundsForTextContainer:textContainer proposedLineFragment:lineFrag glyphPosition:position characterIndex:charIndex];
  74. }
  75. #pragma mark - setter
  76. - (void)setLineHeightMultiple:(CGFloat)lineHeightMultiple
  77. {
  78. NSAssert(lineHeightMultiple>0, @"lineHeightMultiple必须大于0");
  79. _lineHeightMultiple = lineHeightMultiple;
  80. }
  81. @end