在120个字符后添加readmore标签,并使其在ios中可单击
数据来自解析器,如果文本超过120个字符,那么它应该像facebook一样附加“…ReadMore”。我有附加文本的代码,但不知道如何制作可点击链接。我用的是斯威夫特语言在120个字符后添加readmore标签,并使其在ios中可单击,ios,swift,Ios,Swift,数据来自解析器,如果文本超过120个字符,那么它应该像facebook一样附加“…ReadMore”。我有附加文本的代码,但不知道如何制作可点击链接。我用的是斯威夫特语言 if cell!.bhikmangaTextlbl!.text!.utf16Count >= 120 { var abc : String = (cell!.bhikmangaTextlbl!.text! as NSString).substringWithRange(NSRange(loc
if cell!.bhikmangaTextlbl!.text!.utf16Count >= 120
{
var abc : String = (cell!.bhikmangaTextlbl!.text! as
NSString).substringWithRange(NSRange(location: 0, length: 120))
abc += " ...ReadMore"
cell!.bhikmangaTextlbl!.text = abc
}
您可以在这里使用属性字符串。。。并使部分文本可点击 这是目标C代码。你可以为斯威夫特写同样的东西
NSDictionary *attribs = @{
NSForegroundColorAttributeName: BODY_FONT_COLOR,
NSFontAttributeName: [UIFont fontWithName:FontHelvetica size:AppFont16]
};
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:ACCIDENT_DETECTION_AUTOMATIC_TEXT attributes:attribs];
[attributedString addAttribute:NSLinkAttributeName
value:@"Accident detection"
range:[[attributedString string] rangeOfString:@"Read More"]];
NSDictionary *linkAttributes = @{NSForegroundColorAttributeName: LINKS_FONT_COLOR,
NSUnderlineColorAttributeName: [UIColor clearColor],
NSUnderlineStyleAttributeName: @(NSUnderlineStyleNone)};
// assume that textView is a UITextView previously created (either by code or Interface Builder)
self.accidentDetectionText.linkTextAttributes = linkAttributes; // customizes the appearance of links
self.accidentDetectionText.attributedText = attributedString;
我更喜欢添加一个名为“Read more”的额外标签,并在标签上添加点击手势以扩展第一个标签。@Mihir Mehta,这是我实现的代码
var bhikmangaTextlbl:UITextView?
if cell!.bhikmangaTextlbl!.text!.utf16Count >= 120
{
var abc : String = (cell!.bhikmangaTextlbl!.text! as NSString).substringWithRange(NSRange(location: 0, length: 120))
abc += "...ReadMore"
cell!.bhikmangaTextlbl!.text = abc
var attribs = [NSForegroundColorAttributeName: UIColor.blackColor(), NSFontAttributeName: UIFont.systemFontOfSize(14.0)]
var attributedString: NSMutableAttributedString = NSMutableAttributedString(string: abc, attributes: attribs)
attributedString.addAttribute(NSLinkAttributeName, value: "...ReadMore", range: NSRange(location: 120, length: 11))
attributedString.addAttribute(NSUnderlineStyleAttributeName, value: NSUnderlineStyle.StyleSingle.rawValue, range: NSRange(location: 120, length: 11))
cell!.bhikmangaTextlbl!.attributedText = attributedString
}
//The function you said i have written here
func textView(textView: UITextView, shouldInteractWithURL URL: NSURL, inRange characterRange: NSRange) -> Bool
{
return true
}
我已经创建了一个点击手势,我只希望“…readMore”应该是可点击的,因为现在点击手势将整个文本视为可点击的。删除点击手势。。。。只需使用AttributeText。。。并且您的-(BOOL)textView:(UITextView*)textView应该在范围:(NSRange)characterRange中插入URL:(NSURL*)URL将被调用我已经尝试过了,但是该函数没有被调用。func textView(textView:UITextView,应该在范围characterRange:NSRange中插入URL:NSURL)->BOOL{return true}此代码在我的应用程序中运行良好。。。不知道为什么不为你工作。。。是UITextView吗?是的,是UITextView。顺便说一句,当您单击readMore func textView(textView:UITextView,应在Range characterRange:NSRange中插入URL:NSURL)->布尔应被称为rite?我不想使用其他标签。bhikmangaTextlbl是哪个类的对象?UITextField、UILabel或UITextView?它是在UITableViewCell上创建的。var bhikmangaTextlbl:UITextView?是否仍然没有调用该函数。完成:-)@mihirmehta忘记注释单元格!。比曼格特克斯特尔布尔!。可选=错误,因为它不工作。谢谢你,伙计。