Rightpoint / BonMot

Beautiful, easy attributed strings in Swift
MIT License
3.56k stars 197 forks source link

lineSpacing not work properly #209

Closed HIIgor closed 8 years ago

HIIgor commented 8 years ago

hello, i am very glad to use your open source library, but when i wrote the code like following.

_titleLabel = [[UILabel alloc] init];
_titleLabel.lineBreakMode = NSLineBreakByCharWrapping;
_titleLabel.numberOfLines = 9;

BONChain *chain = BONChain.new.string(@"A").font([UIFont systemFontOfSize:20]).color([UIColor greenColor]);
[chain appendLink:BONChain.new.string(@"BCDEFGH").font([UIFont systemFontOfSize:14]).color([UIColor redColor])];

_titleLabel.attributedText = chain.lineSpacing(4).attributedString;

it only shows the A at the center of the label.

ZevEisenberg commented 8 years ago

@xiangyaguo thanks for writing in. The behavior your are encountering is expected, given your code, but I'm aware that the API is confusing. We are avoiding this problem in the upcoming BonMot 4.0 release in #180, but it's a full rewrite and it may not fully support Objective-C, so you may not be able to adopt it just yet.

The problem in your code is that chain.lineSpacing(4) does not copy chain.text.nextText, because that would break the way that inheritance works, but it means that it drops "BCDEFHG" from the string. The way to work around this problem in your example would be like this:

BONChain *chain = BONChain.new.string(@"A").font([UIFont systemFontOfSize:20]).color([UIColor greenColor]).lineSpacing(4);
[chain appendLink:BONChain.new.string(@"BCDEFGH").font([UIFont systemFontOfSize:14]).color([UIColor redColor])];

_titleLabel.attributedText = chain.attributedString;

Or, alternatively:

BONChain *baseChain = BONChain.new.lineSpacing(4);
BONChain *chain = baseChain.string(@"A").font([UIFont systemFontOfSize:20]).color([UIColor greenColor]);
[chain appendLink:baseChain.string(@"BCDEFGH").font([UIFont systemFontOfSize:14]).color([UIColor redColor])];

_titleLabel.attributedText = chain.attributedString;

The one you choose will depend on how you're actually using it in your app, since I assume the code you posted was simplified for the bug report.

I hope this helps! Please let me know if it does, or if you have any more questions.

HIIgor commented 8 years ago

I've just tried the code, it exactly take effect, thank you so much.