현재 URL의 길이는 801 자입니다. URL 길이가 350자를 넘으면 UITextView에서 링크 자동 검색이 작동하지 않는다는 것을 발견했습니다. 또한 "메시지"응용 프로그램에서 내 링크를 테스트했는데 작동하지 않습니다 (메시지는 텍스트 내용을 표시하기 위해 UILabel 또는 UITextView를 사용합니다). 길이가 350자를 넘는 URL을 감지 할 수있는 방법이 있습니까?URL이 너무 큰 경우 UITextView에서 링크 자동 검색이 실패합니다.
0
A
답변
0
UITextView의 데이터 감지기가 길이가 350자를 초과하여 URL을 감지하지 못하는 경우 사용자가 조사 할 때 자신이 직접 수락 할 수 있다고 생각합니다. URL을 감지 한 후 attributedString을 사용하여 UITextView로 설정할 수 있습니다.
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor grayColor];
self.textView.textColor = [UIColor blackColor];
self.textView.contentInset = UIEdgeInsetsMake(0, 0, 10, 0);
NSString *testString = @"google dog meme https://www.google.com.sg/search?q=dog+meme&espv=2&biw=1672&bih=890&source=lnms&tbm=isch&sa=X&ved=0ahUKEwjWr8rexqXSAhUO9GMKHYHXD7QQ_AUIBigB#tbm=isch&q=doge+meme&imgrc=SFF-4BKQucOc4M: hhahaa";
NSDataDetector *detect = [[NSDataDetector alloc] initWithTypes:NSTextCheckingTypeLink error:nil];
NSArray *matches = [detect matchesInString:testString options:0 range:NSMakeRange(0, [testString length])];
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:testString attributes:nil];
NSTextCheckingResult *result = matches.firstObject;
if (result) {
[attributedString addAttribute:NSLinkAttributeName value:result.URL.absoluteString range:result.range];
}
self.textView.editable = NO;
self.textView.attributedText = attributedString;
self.textView.userInteractionEnabled = YES;
self.textView.delegate = self;
}
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange {
NSLog(@"#### %@", URL);
return YES;
}
또 다른 접근법은 URL 단축기를 사용하여 URL을 단축 한 다음 표시하는 것입니다. 나는 사용자가 정말로 긴 URL을 클릭하지 않을 수도 있기 때문에 이것이 UX의 관점에서 가치 있다고 생각한다.
이 정보가 도움이되기를 바랍니다.
작동하지 않습니다. – phamot
흠, 내 편에서 일했는데, 문제가 뭐니? – HDT
350자를 초과하는 URL을 사용해도됩니까? URL 단축키 제안 주셔서 감사합니다. – phamot