Skip to content

Commit

Permalink
Add RTL arabic text to list example (#5500)
Browse files Browse the repository at this point in the history
## The problem:

There wasn't any RTL text in our List example!

## The solution:

I added some RTL text to our List example.

Closes #5498

### Before:
![simulator screen shot - iphone x - 2018-10-23 at 11 28 37](https://user-images.githubusercontent.com/8020010/47372425-bb07a600-d6b7-11e8-997f-ba19d17dc85b.png)

### After:
![simulator screen shot - iphone x - 2018-10-23 at 11 28 02](https://user-images.githubusercontent.com/8020010/47372426-bb07a600-d6b7-11e8-86b1-e088a8737102.png)
  • Loading branch information
andrewoverton committed Oct 23, 2018
1 parent f5a7a0d commit d1961ea
Showing 1 changed file with 46 additions and 13 deletions.
59 changes: 46 additions & 13 deletions components/List/examples/MDCSelfSizingStereoCellExample.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

#import "MDCSelfSizingStereoCellExample.h"

#import <MDFInternationalization/MDFInternationalization.h>

#import "MaterialList+ColorThemer.h"
#import "MaterialList+TypographyThemer.h"
#import "MaterialList.h"
Expand Down Expand Up @@ -110,6 +112,8 @@ - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
forIndexPath:indexPath];
cell.titleLabel.text = self.randomStrings[indexPath.item];
cell.detailLabel.text = self.randomStrings[(indexPath.item + 1) % self.randomStrings.count];
cell.titleLabel.textAlignment = [self textAlignmentForText:cell.titleLabel.text];
cell.detailLabel.textAlignment = [self textAlignmentForText:cell.detailLabel.text];
cell.leadingImageView.image =
[[UIImage imageNamed:@"Cake"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
cell.trailingImageView.image =
Expand All @@ -122,21 +126,50 @@ - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
return cell;
}

- (NSString *)generateRandomString {
NSInteger numberOfWords = 0 + arc4random() % (25 - 0);
NSMutableArray *wordArray = [[NSMutableArray alloc] initWithCapacity:numberOfWords];
for (NSInteger i = 0; i < numberOfWords; i++) {
NSInteger lengthOfWord = 0 + arc4random() % (10 - 0);
NSMutableArray *letterArray = [[NSMutableArray alloc] initWithCapacity:lengthOfWord];
for (NSInteger j = 0; j < lengthOfWord; j++) {
int asciiCode = 97 + arc4random() % (122 - 97);
NSString *characterString = [NSString stringWithFormat:@"%c", asciiCode];
[letterArray addObject:characterString];
- (NSTextAlignment)textAlignmentForText:(NSString *)text {
if (text.length > 0) {
if (text.mdf_calculatedLanguageDirection == NSLocaleLanguageDirectionLeftToRight) {
return NSTextAlignmentLeft;
} else if (text.mdf_calculatedLanguageDirection == NSLocaleLanguageDirectionRightToLeft) {
return NSTextAlignmentRight;
}
NSString *word = [letterArray componentsJoinedByString:@""];
[wordArray addObject:word];
}
return [wordArray componentsJoinedByString:@" "];
return NSTextAlignmentNatural;
}

- (NSString *)generateRandomString {
static NSArray<NSString *> *ltrStrings;
static NSArray<NSString *> *rtlStrings;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
ltrStrings = @[
@"Lorem ipsum dolor sit amet, ", @"consectetur adipiscing elit, ",
@"sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. ",
@"Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea \
commodo consequat. ",
@"Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat \
nulla pariatur. ",
@"Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit \
anim id est laborum."
];
rtlStrings = @[
@"أوه ، من أي قوة قد تملكها هذه القوة ،",
@"مع عدم كفاية قلبي للتأثير؟",
@"لإعطائي الكذبة لوجهتي الحقيقية",
@"وأقسم أن السطوع لا نعمة اليوم؟",
@" من اين اصبحت هذه الامور مريضة",
@" هذا في رفض جدا من أفعالك",
@"هناك مثل هذه القوة وتضمن المهارة ،",
@"هذا ، في رأيي ، أسوأ ما تفوق كل شيء أفضل؟",
];
});
NSArray<NSString *> *strings = arc4random_uniform(2) == 0 ? ltrStrings : rtlStrings;
int numStrings = arc4random_uniform(4);
NSMutableString *string = [strings[arc4random_uniform((unsigned int)strings.count)] mutableCopy];
for (int i = 1; i < numStrings; ++i) {
[string appendString:strings[arc4random_uniform((unsigned int)strings.count)]];
}
return [string copy];
}

#pragma mark - CatalogByConvention
Expand Down

0 comments on commit d1961ea

Please sign in to comment.