Browse Source

Merge pull request #456 from LawrenceZhang/master

文本长度自适应
M了个J 9 years ago
parent
commit
709e3abfe9
1 changed files with 31 additions and 1 deletions
  1. 31 1
      MJRefresh/Custom/Header/MJRefreshNormalHeader.m

+ 31 - 1
MJRefresh/Custom/Header/MJRefreshNormalHeader.m

@@ -55,6 +55,29 @@ - (void)prepare
     self.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
 }
 
+- (CGFloat)stringWidth:(UILabel *)_label
+{
+    CGFloat stringWidth = 0;
+    CGSize size = CGSizeMake(self.mj_w, self.mj_h);
+    if (_label.text.length > 0) {
+#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 70000
+        stringWidth =[_label.text
+                      boundingRectWithSize:size
+                      options:NSStringDrawingUsesLineFragmentOrigin
+                      attributes:@{NSFontAttributeName:_label.font}
+                      context:nil].size.width;
+#else
+        
+        stringWidth = [_label.text sizeWithFont:_label.font
+                              constrainedToSize:size
+                                  lineBreakMode:NSLineBreakByCharWrapping].width;
+#endif
+    }
+    
+    
+    return stringWidth;
+}
+
 - (void)placeSubviews
 {
     [super placeSubviews];
@@ -62,7 +85,14 @@ - (void)placeSubviews
     // 箭头的中心点
     CGFloat arrowCenterX = self.mj_w * 0.5;
     if (!self.stateLabel.hidden) {
-        arrowCenterX -= 100;
+        CGFloat offset = 20;
+        CGFloat stateWidth = [self stringWidth:self.stateLabel];
+        CGFloat timeWidth = 0.0;
+        if (!self.lastUpdatedTimeLabel.hidden) {
+            timeWidth = [self stringWidth:self.lastUpdatedTimeLabel];
+        }
+        CGFloat textWidth = MAX(stateWidth, timeWidth);
+        arrowCenterX -= textWidth / 2 + offset;
     }
     CGFloat arrowCenterY = self.mj_h * 0.5;
     CGPoint arrowCenter = CGPointMake(arrowCenterX, arrowCenterY);