You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@weex.apache.org by ac...@apache.org on 2017/07/13 07:28:55 UTC

[3/6] incubator-weex git commit: * [ios] fix text trim tail white space and fix thread safe

* [ios] fix text trim tail white space and fix thread safe


Project: http://git-wip-us.apache.org/repos/asf/incubator-weex/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-weex/commit/83816574
Tree: http://git-wip-us.apache.org/repos/asf/incubator-weex/tree/83816574
Diff: http://git-wip-us.apache.org/repos/asf/incubator-weex/diff/83816574

Branch: refs/heads/0.15-dev
Commit: 83816574ae83a7ac1fb65c3fb96807c79ee3310a
Parents: 3d97c00
Author: acton393 <zh...@gmail.com>
Authored: Thu Jul 13 14:34:29 2017 +0800
Committer: acton393 <zh...@gmail.com>
Committed: Thu Jul 13 14:34:29 2017 +0800

----------------------------------------------------------------------
 .../WeexSDK/Sources/Component/WXTextComponent.m | 51 ++++++++------------
 1 file changed, 20 insertions(+), 31 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/83816574/ios/sdk/WeexSDK/Sources/Component/WXTextComponent.m
----------------------------------------------------------------------
diff --git a/ios/sdk/WeexSDK/Sources/Component/WXTextComponent.m b/ios/sdk/WeexSDK/Sources/Component/WXTextComponent.m
index a092019..680be09 100644
--- a/ios/sdk/WeexSDK/Sources/Component/WXTextComponent.m
+++ b/ios/sdk/WeexSDK/Sources/Component/WXTextComponent.m
@@ -119,9 +119,7 @@ CGFloat WXTextDefaultLineThroughWidth = 1.2;
     
     BOOL _needsRemoveObserver;
     NSAttributedString * _ctAttributedString;
-    CTFramesetterRef _ctframeSetter;
     
-    pthread_mutex_t _ctFrameSetterMutex;
     pthread_mutex_t _ctAttributedStringMutex;
     pthread_mutexattr_t _propertMutexAttr;
 }
@@ -149,7 +147,6 @@ CGFloat WXTextDefaultLineThroughWidth = 1.2;
         _needsRemoveObserver = NO;
         pthread_mutexattr_init(&(_propertMutexAttr));
         pthread_mutexattr_settype(&(_propertMutexAttr), PTHREAD_MUTEX_RECURSIVE);
-        pthread_mutex_init(&(_ctFrameSetterMutex), &(_propertMutexAttr));
         pthread_mutex_init(&(_ctAttributedStringMutex), &(_propertMutexAttr));
         
         if ([attributes objectForKey:@"coretext"]) {
@@ -186,12 +183,7 @@ CGFloat WXTextDefaultLineThroughWidth = 1.2;
         [[NSNotificationCenter defaultCenter] removeObserver:self name:WX_ICONFONT_DOWNLOAD_NOTIFICATION object:nil];
     }
     _ctAttributedString = nil;
-    if(NULL != _ctframeSetter) {
-        CFRelease(_ctframeSetter);
-        _ctframeSetter = NULL;
-    }
     pthread_mutex_destroy(&_ctAttributedStringMutex);
-    pthread_mutex_destroy(&_ctFrameSetterMutex);
     pthread_mutexattr_destroy(&_propertMutexAttr);
 }
 
@@ -260,10 +252,6 @@ do {\
 {
     _textStorage = nil;
     _ctAttributedString = nil;
-    if(NULL != _ctframeSetter) {
-        CFRelease(_ctframeSetter);
-        _ctframeSetter = NULL;
-    }
 }
 
 #pragma mark - Subclass
@@ -368,16 +356,6 @@ do {\
     return [_ctAttributedString copy];
 }
 
-- (CTFramesetterRef)ctFramesetterRef
-{
-    pthread_mutex_lock(&(_ctFrameSetterMutex));
-    if (NULL == _ctframeSetter) {
-        _ctframeSetter = CTFramesetterCreateWithAttributedString((CFTypeRef)[self ctAttributedString]);
-    }
-    pthread_mutex_unlock(&(_ctFrameSetterMutex));
-    return _ctframeSetter;
-}
-
 - (void)repaintText:(NSNotification *)notification
 {
     if (![_fontFamily isEqualToString:notification.userInfo[@"fontFamily"]]) {
@@ -655,13 +633,20 @@ do {\
         CTFrameRef _coreTextFrameRef = NULL;
         if (_coreTextFrameRef) {
             CFRelease(_coreTextFrameRef);
+            _coreTextFrameRef = NULL;
+        }
+        if(!attributedStringCopy) {
+            return;
         }
-        _coreTextFrameRef = CTFramesetterCreateFrame([self ctFramesetterRef], CFRangeMake(0, attributedStringCopy.length), cgPath, NULL);
+        CTFramesetterRef ctframesetterRef = CTFramesetterCreateWithAttributedString((__bridge CFAttributedStringRef)(attributedStringCopy));
+        _coreTextFrameRef = CTFramesetterCreateFrame(ctframesetterRef, CFRangeMake(0, attributedStringCopy.length), cgPath, NULL);
         CFArrayRef ctLines = NULL;
         if (NULL == _coreTextFrameRef) {
             // try to protect crash from frame is NULL
             return;
         }
+        CFRelease(ctframesetterRef);
+        ctframesetterRef = NULL;
         ctLines = CTFrameGetLines(_coreTextFrameRef);
         CFIndex lineCount = CFArrayGetCount(ctLines);
         NSMutableArray * mutableLines = [NSMutableArray new];
@@ -854,14 +839,17 @@ do {\
 
 - (CGSize)calculateTextHeightWithWidth:(CGFloat)aWidth
 {
-    if (isnan(aWidth)) {
-        aWidth = CGFLOAT_MAX;
-    }
-    
     CGFloat totalHeight = 0;
     CGSize suggestSize = CGSizeZero;
     NSAttributedString * attributedStringCpy = [self ctAttributedString];
-    suggestSize = CTFramesetterSuggestFrameSizeWithConstraints([self ctFramesetterRef], CFRangeMake(0, attributedStringCpy.length), NULL, CGSizeMake(aWidth, MAXFLOAT), NULL);
+    if (!attributedStringCpy) {
+        return CGSizeZero;
+    }
+    if (isnan(aWidth)) {
+        aWidth = [attributedStringCpy boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading context:nil].size.width;
+    }
+    CTFramesetterRef ctframesetterRef = CTFramesetterCreateWithAttributedString((__bridge CFAttributedStringRef)(attributedStringCpy));
+    suggestSize = CTFramesetterSuggestFrameSizeWithConstraints(ctframesetterRef, CFRangeMake(0, 0), NULL, CGSizeMake(aWidth, MAXFLOAT), NULL);
     
     CGMutablePathRef path = NULL;
     path = CGPathCreateMutable();
@@ -869,7 +857,7 @@ do {\
     CGPathAddRect(path, NULL, CGRectMake(0, 0, aWidth, suggestSize.height * 10));
         
     CTFrameRef frameRef = NULL;
-    frameRef = CTFramesetterCreateFrame([self ctFramesetterRef], CFRangeMake(0, attributedStringCpy.length), path, NULL);
+    frameRef = CTFramesetterCreateFrame(ctframesetterRef, CFRangeMake(0, attributedStringCpy.length), path, NULL);
     CGPathRelease(path);
     
     CFArrayRef lines = NULL;
@@ -877,6 +865,8 @@ do {\
         //try to protect unexpected crash.
         return suggestSize;
     }
+    CFRelease(ctframesetterRef);
+    ctframesetterRef = NULL;
     lines = CTFrameGetLines(frameRef);
     CFIndex lineCount = CFArrayGetCount(lines);
     CGFloat ascent = 0;
@@ -906,8 +896,7 @@ do {\
         }
         return suggestSize;
     }
-    
-    return CGSizeMake(suggestSize.width, totalHeight);
+    return CGSizeMake(!isnan(aWidth)? aWidth :suggestSize.width, totalHeight >suggestSize.height? totalHeight:suggestSize.height);
 }
 
 static void WXTextGetRunsMaxMetric(CFArrayRef runs, CGFloat *xHeight, CGFloat *underlinePosition, CGFloat *lineThickness)