Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

不支持UISemanticContentAttributeForceRightToLeft布局时候,根据内容自动算高宽? #1009

Open
liubingbing opened this issue Oct 14, 2024 · 0 comments

Comments

@liubingbing
Copy link

View 设置成UISemanticContentAttributeForceRightToLeft布局的时候,YYLabel 通过<Masonry/Masonry.h>设置约束的时候,不能像UISemanticContentAttributeForceLeftToRight布局的时候,不设置宽度约束的时候,宽度根据内容自动撑。请问是不支持UISemanticContentAttributeForceRightToLeft?如果支持请问该怎么设置约束,宽度按照内容自动设置。
案例如下

#import "ESDViewController.h"
#import <Masonry/Masonry.h>
#import <YYText/YYText.h>

@implementation ESDViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self testRightToLeft];
    
}
- (void)testRightToLeft{
    // 设置视图的整体语义方向
    self.view.semanticContentAttribute = UISemanticContentAttributeForceRightToLeft;
    YYLabel *label = [[YYLabel alloc] init];
    label.numberOfLines = 0;
    label.backgroundColor = [UIColor lightGrayColor];
    label.semanticContentAttribute = UISemanticContentAttributeForceRightToLeft;
    label.textAlignment = NSTextAlignmentRight;

    // 配置文本属性
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.alignment = NSTextAlignmentRight;
    paragraphStyle.baseWritingDirection = NSWritingDirectionRightToLeft;

    NSDictionary *attributes = @{
        NSFontAttributeName: [UIFont systemFontOfSize:16],
        NSParagraphStyleAttributeName: paragraphStyle,
        NSForegroundColorAttributeName: [UIColor blackColor]
    };

    NSString *text = @"testRightToLeft";
    NSAttributedString *attributedText = [[NSAttributedString alloc] initWithString:text attributes:attributes];
    label.attributedText = attributedText;

    // 将 label 添加到视图中
    [self.view addSubview:label];
    
    // 使用 Masonry 设置宽度和位置约束
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top).offset(100);
        make.leading.equalTo(self.view.mas_leading).offset(10);
        make.trailing.lessThanOrEqualTo(self.view.mas_trailing).offset(-20); //尾部
    }];
}

- (void)testLeftToRight{
    // 设置视图的整体语义方向
    self.view.semanticContentAttribute = UISemanticContentAttributeForceLeftToRight;
    YYLabel *label = [[YYLabel alloc] init];
    label.numberOfLines = 0;
    label.backgroundColor = [UIColor lightGrayColor];
    label.semanticContentAttribute = UISemanticContentAttributeForceLeftToRight;
    label.textAlignment = NSTextAlignmentLeft;

    // 配置文本属性
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.alignment = NSTextAlignmentLeft;
    paragraphStyle.baseWritingDirection = NSWritingDirectionLeftToRight;

    NSDictionary *attributes = @{
        NSFontAttributeName: [UIFont systemFontOfSize:16],
        NSParagraphStyleAttributeName: paragraphStyle,
        NSForegroundColorAttributeName: [UIColor blackColor]
    };

    NSString *text = @"testLeftToRight";
    NSAttributedString *attributedText = [[NSAttributedString alloc] initWithString:text attributes:attributes];
    label.attributedText = attributedText;

    // 将 label 添加到视图中
    [self.view addSubview:label];
    
    // 使用 Masonry 设置宽度和位置约束
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top).offset(150);
        make.leading.equalTo(self.view.mas_leading).offset(10);
        make.trailing.lessThanOrEqualTo(self.view.mas_trailing).offset(-20); //尾部
    }];
}
@end

效果图:
testRightToLeft:
testRightToLeft
testRightToLeft宽度不对,没有按照内容自动计算

testLeftToRight:
testLeftToRight

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant