Skip to content

Commit 9f42b88

Browse files
author
Alberto Martínez
authored
Merge pull request #3 from xuhom/master
Preliminary implementation for iOS
2 parents d2de993 + 3584816 commit 9f42b88

File tree

2 files changed

+25
-13
lines changed

2 files changed

+25
-13
lines changed

ios/RNMeasureText.m

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ - (dispatch_queue_t)methodQueue
2525
reject(@"invalid_width", @"missing required width property", nil);
2626
return;
2727
}
28-
if ([options objectForKey:@"texts"] == nil) {
29-
reject(@"invalid_texts", @"missing required texts property", nil);
28+
if ([options objectForKey:@"text"] == nil) {
29+
reject(@"invalid_text", @"missing required text property", nil);
3030
return;
3131
}
3232
if ([options objectForKey:@"fontSize"] == nil) {
@@ -35,29 +35,41 @@ - (dispatch_queue_t)methodQueue
3535
}
3636

3737
float width = [RCTConvert float:options[@"width"]];
38-
NSArray *texts = [RCTConvert NSArray:options[@"texts"]];
38+
NSString *text = [RCTConvert NSString:options[@"text"]];
3939
CGFloat fontSize = [RCTConvert CGFloat:options[@"fontSize"]];
4040

41-
NSMutableArray* results = [[NSMutableArray alloc] init];
41+
NSMutableDictionary* result = [[NSMutableDictionary alloc] init];
42+
4243
UIFont *font = [UIFont systemFontOfSize: fontSize];
43-
44-
for (NSString* text in texts) {
45-
NSTextStorage *textStorage = [[NSTextStorage alloc] initWithString:text];
44+
45+
CGFloat firstHeight = 0;
46+
47+
for (NSString *s in [NSArray arrayWithObjects:@" ", text, nil]) {
48+
NSTextStorage *textStorage = [[NSTextStorage alloc] initWithString:s];
4649
NSTextContainer *textContainer = [[NSTextContainer alloc] initWithSize: CGSizeMake(width, FLT_MAX)];
4750
NSLayoutManager *layoutManager = [[NSLayoutManager alloc] init];
48-
51+
4952
[layoutManager addTextContainer:textContainer];
5053
[textStorage addLayoutManager:layoutManager];
51-
54+
5255
[textStorage addAttribute:NSFontAttributeName value:font
5356
range:NSMakeRange(0, [textStorage length])];
5457
[textContainer setLineFragmentPadding:0.0];
5558
(void) [layoutManager glyphRangeForTextContainer:textContainer];
5659
CGRect resultRect = [layoutManager usedRectForTextContainer:textContainer];
57-
58-
[results addObject:[NSNumber numberWithFloat:resultRect.size.height]];
60+
if (firstHeight == 0) {
61+
firstHeight = resultRect.size.height;
62+
} else {
63+
CGFloat height = resultRect.size.height;
64+
int lines = height / firstHeight;
65+
result[@"width"] = @(resultRect.size.width);
66+
result[@"height"] = @(height);
67+
result[@"lineCount"] = @(lines);
68+
result[@"lastLineWidth"] = @(0);
69+
}
5970
}
60-
resolve(results);
71+
72+
resolve(result);
6173
}
6274

6375
@end

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-native-text-size",
3-
"version": "1.0.0-beta.4",
3+
"version": "1.0.0",
44
"description": "Measure text height and width without laying it out.",
55
"main": "index.js",
66
"types": "./index.d.ts",

0 commit comments

Comments
 (0)