Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

yylabel中全部是emoji表情时,设置行间距无效 #309

Closed
wusw opened this issue May 6, 2016 · 3 comments
Closed

yylabel中全部是emoji表情时,设置行间距无效 #309

wusw opened this issue May 6, 2016 · 3 comments

Comments

@wusw
Copy link

wusw commented May 6, 2016

初始化view

  • (void)initContentView
    {
    YYLabel *sendMessage = [YYLabel new];
    sendMessage.numberOfLines = 0;
    sendMessage.lineBreakMode = NSLineBreakByCharWrapping;
    // sendMessage.textVerticalAlignment = YYTextVerticalAlignmentCenter;
    // sendMessage.textColor = [UIColor whiteColor];
    // sendMessage.font = [UIFont systemFontOfSize:16];
    // sendMessage.backgroundColor = [UIColor redColor];
    [self.contentView addSubview:sendMessage];
    self.sendMessage = sendMessage;
    }

内容赋值

  • (void)setChatContentValue:(NSDictionary *)dict
    {
    NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] init];
    attr.yy_lineSpacing = 0;

    NSMutableAttributedString *nick_attr = [[NSMutableAttributedString alloc] initWithString:dict[@"nick"]];
    nick_attr.yy_color = [CommonFuction colorFromHexRGB:@"ffd944"];
    nick_attr.yy_font = [UIFont boldSystemFontOfSize:16];
    [attr appendAttributedString:nick_attr];

    NSMutableAttributedString *message_attr = [[NSMutableAttributedString alloc] initWithString:dict[@"message"]];
    message_attr.yy_color = [UIColor whiteColor];
    message_attr.yy_font = [UIFont boldSystemFontOfSize:16];
    [attr appendAttributedString:message_attr];

    YYTextShadow *shadow = [YYTextShadow new];
    shadow.color = [UIColor colorWithWhite:0.000 alpha:0.5];
    shadow.offset = CGSizeMake(0, 0);
    shadow.radius = 1;
    attr.yy_textShadow = shadow;

    self.sendMessage.attributedText = attr;
    }

@ibireme
Copy link
Owner

ibireme commented May 6, 2016

在拼接好最终字符串后,再设置 yy_lineSpacing。

@wusw
Copy link
Author

wusw commented May 9, 2016

初始化view

(void)initContentView { YYLabel *sendMessage = [YYLabel new]; sendMessage.numberOfLines = 0; sendMessage.lineBreakMode = NSLineBreakByCharWrapping; // sendMessage.textVerticalAlignment = YYTextVerticalAlignmentCenter; // sendMessage.textColor = [UIColor whiteColor]; // sendMessage.font = [UIFont systemFontOfSize:16]; // sendMessage.backgroundColor = [UIColor redColor]; [self.contentView addSubview:sendMessage]; self.sendMessage = sendMessage; }
内容赋值

(void)setChatContentValue:(NSDictionary *)dict
{
NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] init];

NSMutableAttributedString *nick_attr = [[NSMutableAttributedString alloc] initWithString:dict[@"nick"]];
nick_attr.yy_color = [CommonFuction colorFromHexRGB:@"ffd944"];
nick_attr.yy_font = [UIFont boldSystemFontOfSize:16];
[attr appendAttributedString:nick_attr];

NSMutableAttributedString *message_attr = [[NSMutableAttributedString alloc] initWithString:dict[@"message"]];
message_attr.yy_color = [UIColor whiteColor];
message_attr.yy_font = [UIFont boldSystemFontOfSize:16];
[attr appendAttributedString:message_attr];

YYTextShadow *shadow = [YYTextShadow new];
shadow.color = [UIColor colorWithWhite:0.000 alpha:0.5];
shadow.offset = CGSizeMake(0, 0);
shadow.radius = 1;
attr.yy_textShadow = shadow;

attr.yy_lineSpacing = 0;
self.sendMessage.attributedText = attr;
}
我这样写了还是不可以。。。

@ibireme
Copy link
Owner

ibireme commented May 9, 2016

我猜,你是在添加了 Emoji 后,发现每行的高度变大了,然后希望调整 lineSpacing 来去掉行距?

如果你的问题是这样,那是因为 emoji 本身 font 自带的 ascent 和 descent 比一般字体要大,会把文本行撑高,这不是 line spacing 所能控制的。你可以试着给 label 添加一个 linePositionModifier,或者给 attr 设置一个 maximumLineHeight 属性。

如果不是这样,那我就看不出代码的问题了,提供一下截图和 Demo 来说明问题吧。

@ibireme ibireme closed this as completed Jun 15, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants