我在 iOS 中使用 Objective C 设置了以下代码,但是添加链接的部分仅适用于从 30 或更少开始的范围。 Short_content 的长度应始终至少为 100 个字。
NSString *short_content = [self getFirstNWords:100 :content];
NSString *link = @" \nClick here to read more ";
NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:[short_content stringByAppendingString:link]];
[str addAttribute: NSLinkAttributeName value:@"http://mytest.com/" range:NSMakeRange(str.length - 100, short_content.length)];
最佳答案
如果你想设置字符串“链接”部分的范围,那么你需要:
[str addAttribute: NSLinkAttributeName value:@"http://mytest.com/" range:NSMakeRange(str.length - link.length, link.length)];
关于ios - 在 iOS 中添加指向 nsattributedstrings 的链接时出现越界错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33268811/