ibireme / YYText

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

怎样可以做到像app store的详情一样 #77

Closed smallqiang closed 8 years ago

smallqiang commented 8 years ago

怎样可以做到像app store的详情一样,就是先显示四行的介绍,后面有更多的按钮,点击更多的时候展示全部。我现在把按钮加到最后面,但是因为一开始只展示2行的文字,所以高度是固定了,要将内容截掉才可以吗?谢谢!

NSMutableAttributedString *text = [NSMutableAttributedString new];
UIFont *font = [UIFont systemFontOfSize:14];

NSMutableAttributedString *descString = [[NSMutableAttributedString alloc] initWithString:@"很长的一段文字一段详情介绍啊,真的很长,十分长的,怎样做才可以呢?"];
descString.yy_font = font;
descString.yy_color = RGBCOLOR(87, 87, 87);
[text appendAttributedString:descString];

UIButton *moreButton = [UIButton buttonWithType:UIButtonTypeCustom];
moreButton.frame = CGRectMake(0, 0, 60, 15);
moreButton.titleLabel.font = font;
[moreButton setTitle:@"More ..." forState:UIControlStateNormal];
[moreButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];

NSMutableAttributedString *attachText = [NSMutableAttributedString yy_attachmentStringWithContent:moreButton contentMode:UIViewContentModeCenter attachmentSize:moreButton.bounds.size alignToFont:font alignment:YYTextVerticalAlignmentCenter];
[text appendAttributedString:attachText];

_descLabel.attributedText = text;
ibireme commented 8 years ago

可以试试设置一个 truncationToken

smallqiang commented 8 years ago

不知道要怎样设置才可以呢?

smallqiang commented 8 years ago

qq20151211-0 就想做到这种效果

ibireme commented 8 years ago

NSMutableAttributedString *token = [[NSMutableAttributedString alloc] initWithString@"More..."]; // set token's color/font/highlight/... label. truncationToken = token

这样试试。

smallqiang commented 8 years ago

不行,是不是要设置label的相关属性呢?

smallqiang commented 8 years ago

或者你可以弄个demo试试先,我是想用到这样的功能,但不知道怎么去写。

ibireme commented 8 years ago

上面已经给出代码了,没必要写 demo。

如果你觉得这实现不了或有问题,你可以直接用 YYTextLayout 自己计算和绘制。