YYText icon indicating copy to clipboard operation
YYText copied to clipboard

有Emoji表情,就会产生省略号.

Open xclidongbo opened this issue 9 years ago • 7 comments

麻烦帮忙查看下问题.图片中的红框内,明明可以显示全部的.结果都是省略号.

//根据model类拼凑NSMutableAttributedString类型的字符串.
- (NSMutableAttributedString *)configureChatWithModel:(PublicChatModel *)model {

    self.backView.backgroundColor = [UIColor colorWithRGB:0x000000 alpha:0.4];
    self.model = model;
    NSMutableAttributedString * text = [NSMutableAttributedString new];

    [text appendAttributedString:[self attributeWithLevel:model.level]];
    [text appendAttributedString:[[NSAttributedString alloc] initWithString:@" "]];
    [text appendAttributedString:[self attributeWithNickname:model.user_nicename color:UIColorWithRGB(0xffd600)]];
    [text appendAttributedString:[[NSAttributedString alloc] initWithString:@" "]];
    [text appendAttributedString:[self attributeWithMessage:[NSString stringWithFormat:@": %@",model.message] color:[UIColor whiteColor]]];
    return text;
}

//等级图片,这是用图片上添加水印,拼凑的等级.NSMutableAttributedString类型


- (NSMutableAttributedString *)attributeWithLevel:(NSString *)level {
    UIImage * image = [self imageWithLevel:level];
    NSMutableAttributedString * attributeStr = [NSMutableAttributedString yy_attachmentStringWithContent:image contentMode:UIViewContentModeCenter attachmentSize:CGSizeMake(image.size.width, image.size.height) alignToFont:[UIFont systemFontOfSize:kMessageFont] alignment:YYTextVerticalAlignmentCenter];
    return attributeStr;
}

//这是拼凑的昵称NSMutableAttributedString类型
- (NSMutableAttributedString *)attributeWithNickname:(NSString *)nickname color:(UIColor *)color {

    NSMutableAttributedString * text = [[NSMutableAttributedString alloc] initWithString:nickname attributes:@{NSForegroundColorAttributeName:color,
          NSFontAttributeName:[UIFont systemFontOfSize:kMessageFont]
        }];
    if (text.length>9) {
        [text replaceCharactersInRange:NSMakeRange(9, text.length-9) withString:@"..."];
    }

    @weakify(self);
    [text yy_setTextHighlightRange:text.yy_rangeOfAll color:UIColorWithRGB(0xffd600) backgroundColor:[UIColor grayColor] tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
//        DDLogDebug(@"Tap header %@",self.model.uid);
        @strongify(self);
        if (_tapGRBlock) {
            _tapGRBlock(self.model.uid);
        }

    }];

    return text;
}
//这是拼凑的聊天内容的类型.
- (NSMutableAttributedString *)attributeWithMessage:(NSString *)message color:(UIColor *)color{
    NSMutableAttributedString * text = [[NSMutableAttributedString alloc] initWithString:message attributes:@{NSForegroundColorAttributeName:color,
        NSFontAttributeName:[UIFont systemFontOfSize:kMessageFont]
        }];
    return text;
}

//根据等级图片上添加水印,生成等级图片.
- (UIImage *)imageWithLevel:(NSString *)level {
    NSInteger levelNum = level.integerValue;
    UIImage * imageSource;
    if (levelNum>0&&levelNum<=20) {
        imageSource = [UIImage imageNamed:@"04_dj_icon"];
    } else if(levelNum>20&&levelNum<=40){
        imageSource = [UIImage imageNamed:@"03_dj_icon"];
    } else if(levelNum>40&&levelNum<=60){
        imageSource = [UIImage imageNamed:@"01_dj_icon"];
    } else if(levelNum>60){
        imageSource = [UIImage imageNamed:@"02_dj_icon"];
    }
    return [UIImage imageWithOldImage:imageSource text:level];
}


xclidongbo avatar Aug 23 '16 02:08 xclidongbo

不知这次我说的内容,是否明白.

xclidongbo avatar Aug 23 '16 02:08 xclidongbo

能提供一个简单的可以复现问题的代码片段或者 Demo 吗?上面的代码无法运行,也暂时看不出问题,这样很难定位问题。

ibireme avatar Aug 25 '16 06:08 ibireme

if (text.length>9) { [text replaceCharactersInRange:NSMakeRange(9, text.length-9) withString:@"..."]; }

你的这句代码不是 把后面的内容替换成省略号么?

MeakerSy avatar Feb 08 '17 09:02 MeakerSy

你试试修改yLabel.lineBreakMode = NSLineBreakByCharWrapping;能不能搞定。

layout.textBoundingSize获得size,设置给yLabel,在emoji多的时候会时会显示...,改成charWrapp就没有三个点了。这是我刚刚试出来的,不确定真的能解决问题。

CGSize containerSize = CGSizeMake(size.width, CGFLOAT_MAX);
YYTextLayout *layout = [YYTextLayout layoutWithContainerSize:containerSize text:comment];
CGSize s = layout.textBoundingSize
yLabel.size = s;
yLabel.attributedString = as;//有时label末尾会出现...
yLabel.lineBreakMode = NSLineBreakByCharWrapping;//加上这行就没有三个点了

summer-wu avatar May 23 '17 07:05 summer-wu

不止是emoji表情会产生省略号。遇到一种case: 使用YYTextLinePositionSimpleModifier固定行高,同时若AttributedString中包含emoji、以及以view作为attachment,append到AttributedString的末尾,会导致展示整体展示不全,显示"...展开"。 解决方法: 使用Label.lineBreakMode = NSLineBreakByCharWrapping; 确实能解决眼前问题。

wenjunBZ avatar Aug 11 '21 03:08 wenjunBZ

不止是emoji表情会产生省略号。遇到一种case: 使用YYTextLinePositionSimpleModifier固定行高,同时若AttributedString中包含emoji、以及以view作为attachment,append到AttributedString的末尾,会导致展示整体展示不全,显示"...展开"。 解决方法: 使用Label.lineBreakMode = NSLineBreakByCharWrapping; 确实能解决眼前问题。

恭喜,你成功挖了个老坟

xclidongbo avatar Aug 11 '21 06:08 xclidongbo

不止是emoji表情会产生省略号。遇到一种case: 使用YYTextLinePositionSimpleModifier固定行高,同时若AttributedString中包含emoji、以及以view作为attachment,append到AttributedString的末尾,会导致展示整体展示不全,显示"...展开"。 解决方法: 使用Label.lineBreakMode = NSLineBreakByCharWrapping; 确实能解决眼前问题。

恭喜,你成功挖了个老坟

也不能说是老坟吧。也有可能是其他地方写的有问题,比如显示整个AttributedString的YYLabel的布局。使用的是自动布局,但是会出现有时候能全部展示,有时候却只能展示”...展开“。所以具体原因还真不一定是框架问题。稍后看能不能稳定复现push demo

wenjunBZ avatar Aug 11 '21 07:08 wenjunBZ