ibireme / YYText

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

YYLabel添加标签的时候 怎么让他能够多行显示呢 #480

Open myouzh opened 8 years ago

myouzh commented 8 years ago
NSMutableAttributedString *text = [NSMutableAttributedString new];
for (int i = 0; i < tags.count; i++) {
    NSString *tag = tags[i];
    UIColor *tagStrokeColor = _bordColor;
    UIColor *tagFillColor = [UIColor clearColor];
    NSMutableAttributedString *tagText = [[NSMutableAttributedString alloc] initWithString:tag];
    [tagText yy_insertString:@"   " atIndex:0];
    [tagText yy_appendString:@"   "];
    tagText.yy_font = font;
    tagText.yy_color = [UIColor whiteColor];
    [tagText yy_setTextBinding:[YYTextBinding bindingWithDeleteConfirm:NO] range:tagText.yy_rangeOfAll];

    YYTextBorder *border = [YYTextBorder new];
    border.strokeWidth = 0.5;
    border.strokeColor = tagStrokeColor;
    border.fillColor = tagFillColor;
    border.cornerRadius = 100; // a huge value
    border.lineJoin = kCGLineJoinRound;

    border.insets = UIEdgeInsetsMake(-2, -5.5, -2, -8);
    [tagText yy_setTextBackgroundBorder:border range:[tagText.string rangeOfString:tag]];

    [text appendAttributedString:tagText];
}
NSInteger lineSpace = 10;
text.yy_lineSpacing = lineSpace;
[text yy_appendString:@"\n"];
[text appendAttributedString:text];
YYLabel *label = [YYLabel new];
label.attributedText = text;
label.numberOfLines = 0;
label.textAlignment = NSTextAlignmentCenter;
label.lineBreakMode = NSLineBreakByWordWrapping;
if (self.maxHeight) {
    label.frame = CGRectMake(0, 0, self.width, self.maxHeight);
} else {
    label.frame = CGRectMake(0, 0, self.width, textLayout.textBoundingSize.height);
}
//    textView.size = self.view.size;
self.height = textLayout.textBoundingSize.height - 5;
label.textContainerInset = UIEdgeInsetsMake(10, -5, 10, 5);

label.textColor = _textColor;
label.backgroundColor = [UIColor clearColor];
[self addSubview:label];

为啥非要加上
[text yy_appendString:@"\n"]; [text appendAttributedString:text]; 才可以?

HoneyLuka commented 8 years ago

preferredMaxLayoutWidth

myouzh commented 8 years ago

设置label了也不能换行

myouzh commented 8 years ago

YYTextContainer _container = [YYTextContainer containerWithSize:CGSizeMake(self.width-2_lineSpace, MAXFLOAT)]; YYTextLayout *textLayout = [YYTextLayout layoutWithContainer:container text:text]; 是高度计算的问题