Browse Source

Support attributed text width calculation for UILabel

Frank 6 years ago
parent
commit
86ea62206f
1 changed files with 8 additions and 1 deletions
  1. 8 1
      MJRefresh/Base/MJRefreshComponent.m

+ 8 - 1
MJRefresh/Base/MJRefreshComponent.m

@@ -254,7 +254,14 @@ + (instancetype)mj_label
 - (CGFloat)mj_textWidth {
     CGFloat stringWidth = 0;
     CGSize size = CGSizeMake(MAXFLOAT, MAXFLOAT);
-    if (self.text.length > 0) {
+    
+    if (self.attributedText) {
+        if (self.attributedText.length == 0) { return 0; }
+        stringWidth = [self.attributedText boundingRectWithSize:size
+                                                        options:NSStringDrawingUsesLineFragmentOrigin
+                                                        context:nil].size.width;
+    } else {
+        if (self.text.length == 0) { return 0; }
         stringWidth = [self.text boundingRectWithSize:size
                                               options:NSStringDrawingUsesLineFragmentOrigin
                                            attributes:@{NSFontAttributeName:self.font}