AutoLayout + RTL + UILabel text alignment

前端 未结 9 1059
面向向阳花
面向向阳花 2020-11-28 11:18

I\'m finally getting round to wrestling with Auto Layout and can\'t seem to figure out how to get right-to-left (RTL) support to work the way I\'d expect/want...

I h

9条回答
  •  眼角桃花
    2020-11-28 11:46

    For me those solutions didn't help, and I ended up doing something pretty ugly but it's the only one that did the trick for me. I added it as an NSString category:

    NSString+Extras.h:

    #import 
    
    @interface NSString (Extras)
    - (NSTextAlignment)naturalTextAligment;
    @end
    

    NSString+Extras.m:

    #import "NSString+Extras.h"
    
    @implementation NSString (Extras)
    
    - (NSTextAlignment)naturalTextAligment {
        NSArray *tagschemes = [NSArray arrayWithObjects:NSLinguisticTagSchemeLanguage, nil];
        NSLinguisticTagger *tagger = [[NSLinguisticTagger alloc] initWithTagSchemes:tagschemes options:0];
        [tagger setString:self];
        NSString *language = [tagger tagAtIndex:0 scheme:NSLinguisticTagSchemeLanguage tokenRange:NULL sentenceRange:NULL];
        if ([language rangeOfString:@"he"].location != NSNotFound || [language rangeOfString:@"ar"].location != NSNotFound) {
            return NSTextAlignmentRight;
        } else {
            return NSTextAlignmentLeft;
        }
    }
    @end
    

    To detect the language I used this SO answer.

提交回复
热议问题