TYAttributedLabel icon indicating copy to clipboard operation
TYAttributedLabel copied to clipboard

生成的container不会居中在xib中显示

Open lck93 opened this issue 8 years ago • 12 comments

  • (TYTextContainer *)creatTextContainer { NSString *text = @"滨江店火爆虾";

    // 属性文本生成器 TYTextContainer *textContainer = [[TYTextContainer alloc]init]; textContainer.text = text;

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; button.layer.cornerRadius = 2; button.titleLabel.font = [UIFont systemFontOfSize:8]; [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; [button setTitle:@"商铺" forState:UIControlStateNormal]; [button setBackgroundColor:RGB(102, 212, 255)]; button.frame = CGRectMake(0, 0, 30, 15); [textContainer addView:button range:NSMakeRange(0, 0)]; textContainer.linesSpacing = 2; textContainer = [textContainer createTextContainerWithTextWidth:(self.view.width-200)]; textContainer.isWidthToFit = YES; return textContainer; }

  • (TYAttributedLabel *)nameLab { if (!_nameLab) { _nameLab =[[TYAttributedLabel alloc]initWithFrame:CGRectMake(0, 90, CGRectGetWidth(self.view.frame), 30)]; _nameLab.characterSpacing = 2; // 文本行间隙 _nameLab.linesSpacing = 6;

      _nameLab.textColor = [UIColor colorWithHexString:@"#333333"];
      _nameLab.textContainer = [self creatTextContainer];
      _nameLab.isWidthToFit = YES;
      _nameLab.textAlignment = kCTTextAlignmentCenter;
    

    } return _nameLab; }

lck93 avatar Sep 07 '17 06:09 lck93

你应该用textContainer设置这些属性,textContainer会覆盖label的这些属性

12207480 avatar Sep 07 '17 07:09 12207480

我尝试过在textContainer中设置这些属性,但是不起作用 textContainer.textAlignment = kCTTextAlignmentCenter; textContainer = [textContainer createTextContainerWithTextWidth:(self.view.width)];

return textContainer;

lck93 avatar Sep 07 '17 07:09 lck93

位置不是在左就是在右

lck93 avatar Sep 07 '17 07:09 lck93

demo里测试没问题 你是否有demo

12207480 avatar Sep 07 '17 08:09 12207480

但是demo没有对textContainer做位置调整,如果是纯文本没有问题,但是在textContainer中添加button控件,拿到内容做位置处理会失败

lck93 avatar Sep 07 '17 08:09 lck93

TextContainerViewController.m textContainer.textAlignment = kCTTextAlignmentCenter;

// 生成 NSAttributedString
//_attString = [textContainer createAttributedString];

// 或者 生成 TYTextContainer
_textContainer = [textContainer createTextContainerWithTextWidth:kAttrLabelWidth];

显示没问题

12207480 avatar Sep 07 '17 08:09 12207480

无助

  • (TYTextContainer *)createTextContainer { NSString *text = @"滨江火爆虾店";

    // 属性文本生成器 TYTextContainer *textContainer = [[TYTextContainer alloc]init]; textContainer.text = text;

    // 整体设置属性 textContainer.linesSpacing = 2; textContainer.paragraphSpacing = 5;

    TYViewStorage *viewStorage = [[TYViewStorage alloc] init]; UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; button.layer.cornerRadius = 2; button.titleLabel.font = [UIFont systemFontOfSize:8]; [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; [button setTitle:@"商铺" forState:UIControlStateNormal]; [button setBackgroundColor:RGB(102, 212, 255)]; button.frame = CGRectMake(0, 0, 30, 15); viewStorage.view = button;

    viewStorage.size = CGSizeMake(15, 15); [textContainer addView:viewStorage.view range:NSMakeRange(0, 0)];

    // 生成 NSAttributedString //_attString = [textContainer createAttributedString];

    // 或者 生成 TYTextContainer textContainer.textAlignment = kCTTextAlignmentCenter; textContainer = [textContainer createTextContainerWithTextWidth:(CGRectGetWidth(self.view.frame)-20)]; return textContainer;

}

  • (TYAttributedLabel *)nameLab { if (!_nameLab) { _nameLab =[[TYAttributedLabel alloc]initWithFrame:CGRectMake(0, 90, CGRectGetWidth(self.view.frame), 30)]; _nameLab.textColor = [UIColor colorWithHexString:@"#333333"]; _nameLab.textContainer = [self createTextContainer];

    } return _nameLab; } 图片还是显示在右侧

lck93 avatar Sep 07 '17 09:09 lck93

image 复制你的createTextContainer代码在demo里测试 在中间 ,应该是你的label宽度问题

12207480 avatar Sep 07 '17 09:09 12207480

image image 这两个宽度应该要一样的

12207480 avatar Sep 07 '17 09:09 12207480

验证完毕了,如果是纯代码加载你的控件完全没有问题,如何ViewCOntroller创建时带了一个xib,那么设置富文本居中内容会向右偏移,我打算用纯oc重写控制器,你也可以验证一下。

lck93 avatar Sep 07 '17 13:09 lck93

好的 我看看

12207480 avatar Sep 07 '17 15:09 12207480

这是个bug

12207480 avatar Sep 08 '17 02:09 12207480