From 3584816cad14f3bfd5d51a3d409167bc5f27e460 Mon Sep 17 00:00:00 2001 From: xuhom Date: Fri, 6 Jul 2018 20:35:37 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E4=BE=9BiOS=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ios/RNMeasureText.m | 36 ++++++++++++++++++++++++------------ package.json | 2 +- 2 files changed, 25 insertions(+), 13 deletions(-) diff --git a/ios/RNMeasureText.m b/ios/RNMeasureText.m index d5e0943..1b41ff8 100644 --- a/ios/RNMeasureText.m +++ b/ios/RNMeasureText.m @@ -25,8 +25,8 @@ - (dispatch_queue_t)methodQueue reject(@"invalid_width", @"missing required width property", nil); return; } - if ([options objectForKey:@"texts"] == nil) { - reject(@"invalid_texts", @"missing required texts property", nil); + if ([options objectForKey:@"text"] == nil) { + reject(@"invalid_text", @"missing required text property", nil); return; } if ([options objectForKey:@"fontSize"] == nil) { @@ -35,29 +35,41 @@ - (dispatch_queue_t)methodQueue } float width = [RCTConvert float:options[@"width"]]; - NSArray *texts = [RCTConvert NSArray:options[@"texts"]]; + NSString *text = [RCTConvert NSString:options[@"text"]]; CGFloat fontSize = [RCTConvert CGFloat:options[@"fontSize"]]; - NSMutableArray* results = [[NSMutableArray alloc] init]; + NSMutableDictionary* result = [[NSMutableDictionary alloc] init]; + UIFont *font = [UIFont systemFontOfSize: fontSize]; - - for (NSString* text in texts) { - NSTextStorage *textStorage = [[NSTextStorage alloc] initWithString:text]; + + CGFloat firstHeight = 0; + + for (NSString *s in [NSArray arrayWithObjects:@" ", text, nil]) { + NSTextStorage *textStorage = [[NSTextStorage alloc] initWithString:s]; NSTextContainer *textContainer = [[NSTextContainer alloc] initWithSize: CGSizeMake(width, FLT_MAX)]; NSLayoutManager *layoutManager = [[NSLayoutManager alloc] init]; - + [layoutManager addTextContainer:textContainer]; [textStorage addLayoutManager:layoutManager]; - + [textStorage addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, [textStorage length])]; [textContainer setLineFragmentPadding:0.0]; (void) [layoutManager glyphRangeForTextContainer:textContainer]; CGRect resultRect = [layoutManager usedRectForTextContainer:textContainer]; - - [results addObject:[NSNumber numberWithFloat:resultRect.size.height]]; + if (firstHeight == 0) { + firstHeight = resultRect.size.height; + } else { + CGFloat height = resultRect.size.height; + int lines = height / firstHeight; + result[@"width"] = @(resultRect.size.width); + result[@"height"] = @(height); + result[@"lineCount"] = @(lines); + result[@"lastLineWidth"] = @(0); + } } - resolve(results); + + resolve(result); } @end diff --git a/package.json b/package.json index a175717..06a8734 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "react-native-text-size", - "version": "1.0.0-beta.4", + "version": "1.0.0", "description": "Measure text height and width without laying it out.", "main": "index.js", "types": "./index.d.ts",