ios - 如何将按钮添加到文本末尾,如 Facebook 的 "Continue reading"?

标签 ios objective-c

当状态帖子太长时,Facebook 应用程序会剪切文本并在末尾添加“继续阅读”。它怎么知道在哪里剪切文本并添加“...继续阅读”?

不仅仅是向 textView 或标签添加按钮,还有如何剪切字符串。 例如,在下图中,我将行数限制为 7。我可以只在 textView 或标签的右下角放置一个按钮,但它可能会与某些字符重叠。

enter image description here

最佳答案

这应该对你有帮助:)

NSString *str=self.strQuestionTitle;

CGRect rect=CGRectMake(51, 16, 257, 0);

CGSize size=[str sizeWithFont:self.lblQuestion.font constrainedToSize:CGSizeMake(257, 3000) lineBreakMode:self.lblQuestion.lineBreakMode];
int lines=(size.height/self.lblQuestion.font.pointSize);
self.lblQuestion.numberOfLines=lines;
rect.size=size;
if(lines>2)
{
    if(lines==3 &&[str length]>66)
    {
        str=[str substringToIndex:66];
    str=[str stringByAppendingString:@"...Read More"];
    size=[str sizeWithFont:self.lblQuestion.font constrainedToSize:CGSizeMake(257, 67) lineBreakMode:self.lblQuestion.lineBreakMode];

    int lines=(size.height/self.lblQuestion.font.pointSize);
    self.lblQuestion.numberOfLines=lines;

    rect.size=CGSizeMake(257, 67);
    }
    else if(lines>3)
    {
        str=[str stringByAppendingString:@"...Read More"];
        size=[str sizeWithFont:self.lblQuestion.font constrainedToSize:CGSizeMake(257, 67) lineBreakMode:self.lblQuestion.lineBreakMode


              ];

        int lines=(size.height/self.lblQuestion.font.pointSize);
        self.lblQuestion.numberOfLines=lines;

        rect.size=CGSizeMake(257, 67);
    }

    //self.lblQuestion.lineBreakMode=NSLineBreakByTruncatingHead;
}

关于ios - 如何将按钮添加到文本末尾,如 Facebook 的 "Continue reading"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18386498/

相关文章:

c# - 如何在 Monotouch 中绑定(bind) NSObject<UIGestureRecognizerDelegate>?

ios - 使用 Photo Kit 和 Swift 在 UICollectionViewCell 中模糊图像

iphone - 空的 nsarray 崩溃 uitableview

ios - Firebase 电话号码身份验证错误

objective-c - 以编程方式在另一个桌面(空间)中打开 NSWindow

objective-c - 为什么 initWithCoder 没有正确初始化项目?

ios - Xamarin iOS 绑定(bind) : Could not initialise an instance of the type, 返回 nil

iphone - 如何访问 UITableView subview 选择?

objective-c - iCarousel in swift version 1.2 报错(candidate is not Objc but protocol requires it)

ios - ipad-如何在 UITableViewCell 中打开弹出 View Controller