截断 TTStyledTextLabel 中的文本
我在项目中使用 TTStyledTextLabel 来解析超链接,一切正常。我面临的唯一问题是截断长文本 - 如果文本不适合 TTStyledTextLabel 的边界,则显示省略号。
换句话说,我需要与 UILabel 相同的行为,它添加省略号来指示某些文本被剪裁。我在 TTStyledTextLabel
和 TTStyledText
类中进行了搜索,没有提供实现此目的的规定。以下是我在 UITableViewCell
子类中用于适当设置 TTStyledTextLabel
框架的代码:
-(void) layoutSubviews
{
[super layoutSubviews];
.
.
.
CGSize maxSize = CGSizeMake(self.contentView.frame.size.width -TEXT_OFFSET_WIDTH, TT_TEXT_MAX_HEIGHT);
[[[self textLabelTTStyled] text] setWidth:maxSize.width];
[[self textLabelTTStyled] sizeToFit];
double heigthForTTLabel = [[[self textLabelTTStyled] text] height];
if (heigthForTTLabel > maxSize.height)
heigthForTTLabel = maxSize.height; // Do not exceed the maximum height for the TTStyledTextLabel.
**// The Text was supposed to clip here when maximum height is set!**
CGSize mTempSize = CGSizeMake([[[self textLabelTTStyled] text] width], heigthForTTLabel);
CGRect frame = CGRectMake(TEXT_OFFSET_X,TEXT_OFFSET_Y,mTempSize.width, mTempSize.height);
self.textLabelTTStyled.frame = frame;
.
.
.
}
在 tableView:cellForRowAtIndexPath:
中我正在将这样的文本设置为我的 TTStyledTextLabel
:
TTStyledText *styledStatusMessage = [TTStyledText textFromXHTML:@"This is a really long text, how long can this go on? This is a really long text, how long can this go on? This is a really long text, how long can this go on? This is a really long text, how long can this go on? This is a really long text, how long can this go on? This is a really long text, how long can this go on?"
lineBreaks:YES URLs:YES];
if (nil == styledStatusMessage) {
styledStatusMessage = [TTStyledText textWithURLs:[statusMessage title] lineBreaks:YES];
[[cell textLabelTTStyled] setText:styledStatusMessage];
}
多余的文本只是被丢弃,默认情况下不会添加省略号来指示文本被剪裁。这个问题有什么解决办法吗?
谢谢, 拉吉
I am using a TTStyledTextLabel
in my project to parse the hyperlinks, it all works fine. The only problem I am facing is to truncate a long text - show ellipses if the text does not fit in the bounds of TTStyledTextLabel
.
In other terms, I need the same behavior as a UILabel which adds ellipses to indicate that some text is clipped. I have searched in TTStyledTextLabel
and TTStyledText
classes, there is no provision to achieve this. The following is the code which I have used in my UITableViewCell
subclass to set the frame of TTStyledTextLabel
appropriately:
-(void) layoutSubviews
{
[super layoutSubviews];
.
.
.
CGSize maxSize = CGSizeMake(self.contentView.frame.size.width -TEXT_OFFSET_WIDTH, TT_TEXT_MAX_HEIGHT);
[[[self textLabelTTStyled] text] setWidth:maxSize.width];
[[self textLabelTTStyled] sizeToFit];
double heigthForTTLabel = [[[self textLabelTTStyled] text] height];
if (heigthForTTLabel > maxSize.height)
heigthForTTLabel = maxSize.height; // Do not exceed the maximum height for the TTStyledTextLabel.
**// The Text was supposed to clip here when maximum height is set!**
CGSize mTempSize = CGSizeMake([[[self textLabelTTStyled] text] width], heigthForTTLabel);
CGRect frame = CGRectMake(TEXT_OFFSET_X,TEXT_OFFSET_Y,mTempSize.width, mTempSize.height);
self.textLabelTTStyled.frame = frame;
.
.
.
}
And in the tableView:cellForRowAtIndexPath:
I am setting text like this to my TTStyledTextLabel
:
TTStyledText *styledStatusMessage = [TTStyledText textFromXHTML:@"This is a really long text, how long can this go on? This is a really long text, how long can this go on? This is a really long text, how long can this go on? This is a really long text, how long can this go on? This is a really long text, how long can this go on? This is a really long text, how long can this go on?"
lineBreaks:YES URLs:YES];
if (nil == styledStatusMessage) {
styledStatusMessage = [TTStyledText textWithURLs:[statusMessage title] lineBreaks:YES];
[[cell textLabelTTStyled] setText:styledStatusMessage];
}
The excessive text is simply being discarded, the ellipses are not added by default to indicate that text is clipped. Any solutions to this problem?
Thanks,
Raj
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信您使用的是私有 api Three20,它有可能被应用商店拒绝。上传前检查一次即可。
一般来说,您可以设置任何带有设置标题的自定义按钮
[button setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
[按钮 setTitleEdgeInsets:UIEdgeInsetsMake(0.0, 10.0, 0.0, 0.0)]; - 该行可用于设置标题边缘。希望有帮助。
I believe you are using a private api Three20 which is having a chance oof rejection from appstore.Just check once before you upload.
Genrally you can set any set any custom button with setting title on it
[button setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
[button setTitleEdgeInsets:UIEdgeInsetsMake(0.0, 10.0, 0.0, 0.0)]; - This line can be used for settting title edges. Hope it helps.