ibireme / YYText

Powerful text framework for iOS to display and edit rich text.
MIT License
8.84k stars 1.69k forks source link

YYLabel设置属性字符串无法居中 #873

Open ma125125t opened 5 years ago

ma125125t commented 5 years ago

示例代码如下

NSMutableAttributedString *hiddenTagText = [[NSMutableAttributedString alloc] initWithString:@"阿萨德"];
    hiddenTagText.color = UIColor.blackColor;
    hiddenTagText.font = [UIFont systemFontOfSize:10];

    NSLog(@"%f",hiddenTagText.size.width);

    YYTextContainer *c = [YYTextContainer containerWithSize:CGSizeMake(400, CGFLOAT_MAX) insets:UIEdgeInsetsZero];
    YYTextLayout *_privateTagTextLayout = [YYTextLayout layoutWithContainer:c text:hiddenTagText];
    YYLabel *_profileTag = [[YYLabel alloc] initWithFrame:CGRectMake(100, 100, 0, 30)];
    _profileTag.backgroundColor = UIColor.yellowColor;
//    _profileTag.width = _privateTagTextLayout.textBoundingSize.width+30;
    _profileTag.textLayout = _privateTagTextLayout;
    [self.view addSubview:_profileTag];
CoracleMobi commented 5 years ago

[hiddenTagText setYy_alignment:NSTextAlignmentCenter];