无法将标签内的图像对齐到右侧

时间:2018-02-18 13:05:48

标签: ios objective-c

我试图在标签内设置图像对齐但没有成功。

通过使用Xcode在本地化选项上选择阿拉伯语,我已成功将文本设置为右侧。 但图像似乎不起作用。

这是我的contructView函数:

- (void)constructAlertCardView {
    UIView* alertView = [[UIView alloc] initWithFrame:CGRectMake(0.f, 0.f, self.view.frame.size.width, self.view.frame.size.height)];
    alertView.backgroundColor = _alertViewBackgroundColor;
    [self.view addSubview:alertView];

    CGFloat heightCorrection = _alertPosition == ISAlertPositionBottom ? -10.f : (_statusBarHeight > 0.f ? _statusBarHeight : 10.f);

    CGFloat insetCorrection = 0;
    if (@available(iOS 11.0, *)) {
        if (_alertPosition == ISAlertPositionBottom) {
            UIWindow *window = UIApplication.sharedApplication.keyWindow;
            insetCorrection = -window.safeAreaInsets.bottom;
        }
    }

    UIImageView* iconImage = [[UIImageView alloc] initWithFrame:CGRectMake(kDefaultInset, (_alertViewHeight - _iconImageSize.height + heightCorrection + insetCorrection) / 2.f, _iconImageSize.width, _iconImageSize.height)];
    iconImage.contentMode = UIViewContentModeScaleAspectFit;
    iconImage.image = _iconImage;
    iconImage.isAccessibilityElement = true;
    iconImage.accessibilityTraits = UIAccessibilityTraitButton;
    iconImage.accessibilityLabel = NSLocalizedString(@"OK", comment: "");
    [alertView addSubview:iconImage];

    heightCorrection = heightCorrection + (_alertPosition == ISAlertPositionBottom ? 10.0f : insetCorrection + (_statusBarHeight > 0.f ? (_statusBarHeight / 4) : 0.f));

    UILabel* titleLabel = [UILabel new];
    titleLabel.frame = CGRectMake((kDefaultInset*2.f) + _iconImageSize.width, kDefaultInset + heightCorrection, self.view.frame.size.width - ((kDefaultInset*3.f) + _iconImageSize.width), _titleLabelHeight);
    titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
    titleLabel.textAlignment = NSTextAlignmentRight;
    titleLabel.numberOfLines = 0;
    titleLabel.textColor = _titleLabelTextColor;
    titleLabel.font = _titleLabelFont;
    titleLabel.text = _titleString;
    if (self.alertTypeAcessibilityLabel != nil) {
        titleLabel.accessibilityLabel = [NSString stringWithFormat:@"%@, %@", self.alertTypeAcessibilityLabel, _titleString];
    }
    [alertView addSubview:titleLabel];

    UILabel* messageLabel = [UILabel new];
    messageLabel.frame = CGRectMake((kDefaultInset*2.f) + _iconImageSize.width, kDefaultInset + heightCorrection + _titleLabelHeight + 3.f, self.view.frame.size.width - ((kDefaultInset*3.f) + _iconImageSize.width), _messageLabelHeight);
    messageLabel.lineBreakMode = NSLineBreakByWordWrapping;
    messageLabel.textAlignment = NSTextAlignmentRight;
    messageLabel.numberOfLines = 0;
    messageLabel.textColor = _messageLabelTextColor;
    messageLabel.font = _messageLabelFont;
    messageLabel.text = _messageString;
    [alertView addSubview:messageLabel];

    // Sets custom order of accessibility elements.
    alertView.accessibilityElements = @[iconImage,titleLabel, messageLabel];

    if (_hideOnSwipe) {
        UISwipeGestureRecognizer* swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(gestureAction)];
        swipeGesture.direction = UISwipeGestureRecognizerDirectionUp | UISwipeGestureRecognizerDirectionDown;
        [alertView addGestureRecognizer:swipeGesture];
    }

    if (_hideOnTap) {
        UITapGestureRecognizer* tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(gestureActionWithHandler)];
        [alertView addGestureRecognizer:tapGesture];
    }

}

2 个答案:

答案 0 :(得分:0)

在支持阿拉伯语和英语的应用程序中,我也面临类似的问题,我必须在英语标签之前显示图像。

因此,我使用UIButton的属性而不是UILabel来设置其图像。我还禁用了按钮的用户交互,以便给出Label的感觉。

enter image description here
完成后,根据所选语言,您可以调整此UIButton的标题插入和图像插入,以微调图像的间距。

    if(isArabicLanguage){
        [button setTitleEdgeInsets:UIEdgeInsetsMake(0, -20, 0, 0)];
    }else{
        [button setTitleEdgeInsets:UIEdgeInsetsMake(0, 20, 0, 0)];
    }

希望有所帮助

答案 1 :(得分:-1)

阿拉伯语是一种RTL(从右到左的语言)因此,您必须根据RTL或LTL语言对齐图像,您的代码将在LTL语言中正常工作,但对于RTL语言,您已将图像对齐。