Closed wangyanlong closed 8 years ago
我尝试这用一些YYKit的代码写了一个图文混排的demo,但发现了一点问题,为什么,文字会在表情后面换行呢?请问怎么解决
NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:@"[f100][f100][f100]123123123123123123123123123123123123123123123123123123123[f100]123123123123123123123123123123"]; UIFont *font = [UIFont systemFontOfSize:15]; // 匹配 [表情] NSArray *emoticonResults = [[WBStatusHelper regexEmoticon] matchesInString:text.string options:kNilOptions range:text.rangeOfAll]; NSUInteger emoClipLength = 0; for (NSTextCheckingResult *emo in emoticonResults) { if (emo.range.location == NSNotFound && emo.range.length <= 1) { continue; } NSRange range = emo.range; range.location -= emoClipLength; NSString *emoString = [text.string substringWithRange:range]; NSRange subrange = NSMakeRange (1, emoString.length-2); emoString = [emoString substringWithRange:subrange]; UIImage *image = [UIImage imageNamed:emoString]; image = [UIImage imageWithCGImage:image.CGImage scale:[UIScreen mainScreen].scale orientation:UIImageOrientationUp]; NSMutableAttributedString *attachText = [NSMutableAttributedString yy_attachmentStringWithContent:image contentMode:UIViewContentModeCenter attachmentSize:image.size alignToFont:font alignment:YYTextVerticalAlignmentCenter]; [text replaceCharactersInRange:range withAttributedString:attachText]; emoClipLength += range.length - 1; } //设置yy_font要在layout上面,否则无效 CGSize size = CGSizeMake(350, CGFLOAT_MAX); text.yy_font = font; YYTextLayout *layout = [YYTextLayout layoutWithContainerSize:size text:text]; // 显示文本排版结果 YYLabel *label = [YYLabel new]; label.textLayout = layout; [self.view addSubview:label]; label.backgroundColor = [UIColor colorWithRed:(arc4random()%255/255.0) green:(arc4random()%255/255.0) blue:(arc4random()%255/255.0) alpha:1]; [label mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(self.view.mas_left).offset(10); make.top.mas_equalTo(self.view.mas_top).offset(100); make.size.mas_equalTo(layout.textBoundingSize); }];
默认换行模式是 NSLineBreakByWordWrapping,就是尽量保证英文单词的连续性。这你可以试一下系统的记事本,表现也是这样的。
如果不太顾及英文单词换行处理,那可以把 lineBreakMode 设置为 NSLineBreakByCharWrapping。
谢谢你,了解了
我尝试这用一些YYKit的代码写了一个图文混排的demo,但发现了一点问题,为什么,文字会在表情后面换行呢?请问怎么解决