AutoLayout + RTL + UILabel text alignment

前端 未结 9 1033
面向向阳花
面向向阳花 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:51

    This function helped me out

    -(void)fnForWritingDirection:(UILabel*)label textFor:(NSString *)stringForText{
    
        NSMutableAttributedString* attrStr = [[NSMutableAttributedString alloc] initWithString: [stringForText stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    
        [paragraphStyle setBaseWritingDirection:NSWritingDirectionRightToLeft];
    
        [attrStr addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [attrStr length])];
    
        label.attributedText=attrStr;
    
    }
    
    0 讨论(0)
  • 2020-11-28 11:53

    Follow up from Ken's answer

    Setting textAlignment to NSTextAlignmentNatural is not possible on UILabel, it will result in an exception getting thrown:

    Terminating app due to uncaught exception 'NSInvalidArgumentException',
    reason: 'textAlignment does not accept NSTextAlignmentNatural'
    

    It does work when using attributed text and this can be set in Interface Builder as shown:

    attributed text natural alignment

    However, it would appear that attributed text is not picked up when localising the storyboard.

    To get around this I have left the UILabel configured as plain in Interface Builder and created an NSAttributedString with the label's text, set the alignment on the attributed string and assign it to the label's attributedText property:

    -(void)viewDidLoad
    {
        [super viewDidLoad];
    
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
        paragraphStyle.alignment = NSTextAlignmentNatural;
    
        NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:self.lbl.text];
        [string addAttribute:NSParagraphStyleAttributeName
                       value:paragraphStyle
                       range:NSMakeRange(0, string.length)];
    
        self.lbl.attributedText = string;
    }
    

    This works fine in this simple case but I can see it falling over when you need more complex attributed string styling. But obviously in that case you'd probably just be using NSLocalizedString or equivalents when creating the NSAttributedString.

    0 讨论(0)
  • 2020-11-28 11:55

    I think you don't want to use text alignment in this case, for a label.

    You can just let the width be determined by intrinsicContentSize, and remove any width constraints on the label. You will achieve the desired effect of the label text aligned to the view.

    For x axis, you only need this constraint between label and imageview: [imageview]-[label]

    This is only a horizontal spacing constraint. No leading or trailing to superview.

    0 讨论(0)
提交回复
热议问题