Nice programing

iOS 7에서 UITextView 링크 감지

nicepro 2020. 12. 13. 11:09
반응형

iOS 7에서 UITextView 링크 감지


나는이 UITextView인터페이스 빌더를 통해 관리된다. 데이터 감지로 "링크"를 확인했습니다. iOS 6에서는 모든 것이 잘 작동하고 링크가 강조 표시되고 클릭 할 수 있습니다. 하지만 iOS 7에서는 모든 링크가 일반 텍스트로 남아 있습니다. 편집 가능하고 선택할 수있는 확인란은 선택 취소됩니다.

문제가 될 수있는 UITextView것은 .NET Framework 내부에있는 컨테이너 뷰의 하위 뷰라는 것 UIScrollView입니다.


iOS 7에서 링크 감지 UITextView는 선택 가능한 경우에만 작동하는 것 같습니다 . 그래서 UITextView선택할 수 없게 만들면 링크 감지가 작동하지 않습니다.

나는 또한 이것을 iOS 6에서 테스트했으며 iOS 6에서 링크 감지 UITextView가 선택 불가능한 경우에도 제대로 작동하는지 확인할 수 있습니다.


오늘 전화 번호 감지에 문제가있었습니다. UITextView는 텍스트를 다른 것으로 설정 한 후에도 이전 전화 번호를 유지하고 텍스트를 강조 표시하는 것처럼 보였습니다.

텍스트를 새 문자열로 설정하기 전에 setText : nil을 설정하면 textview가 재설정되고 전화 번호가 정상적으로 강조 표시된다는 것을 알았습니다. 이것이 iOS 7.0의 UITextView와 관련된 버그인지 궁금합니다.

어느 쪽이든, 이것은 나를 위해 일했습니다.


iOS7이 처음 나왔을 때 이것이 나를 괴롭 혔 고이 스레드에서 답변을 찾았습니다 (실제 값을 설정하기 전에 UITextView의 텍스트 속성을 nil로 설정하면 트릭이 수행되었습니다). 그런 다음 갑자기 문제가 (저에게는 전체 문자열이 링크로 강조 표시됨) 다시 잘립니다 (iOS 업데이트로 인해).

마침내 나를 위해 트릭을 한 것은 text 속성 사용을 중지하고 attributeText를 설정하는 것입니다. 이 작업을 수행하면 글꼴 / 스크롤 / 선택 가능 / 편집 가능 / 등을 설정해야합니다. 프로그래밍 방식으로 사라졌습니다. IB에서 UITextView를 정의하고 원하는대로 값을 설정 한 다음 (스크롤 불가능, 편집 불가능, 선택 가능, 링크 및 전화 번호 감지) 속성 문자열을 작성하고 설정합니다.

myUITextView.attributedString = myAttributedString;

그리고 갑자기 모든 것이 예상대로 작동했습니다. 이것이 다른 사람에게 도움이되기를 바랍니다.


동일한 문제가 있었고 UITextView에서 스크롤을 비활성화하면 사용자가 textview와 상호 작용 한 후에 만 ​​작동하는 대신로드시 링크 감지가 활성화됩니다. UITextView도 선택 가능하고 편집 불가능해야했습니다.

detailTextView.scrollEnabled = NO;
detailTextView.editable = NO;
detailTextView.selectable = YES;

iOS6에서는 선택 가능하거나 스크롤을 활성화 할 필요가 없습니다.

확인해야 할 또 다른 사항은 사용자 상호 작용이 셀 및 셀의 콘텐츠보기에서 활성화되어 있다는 것입니다. 그렇지 않으면 링크를 클릭 할 수 없습니다.


textview에서 데이터 검출기 속성을 사용하려면이 라인을 추가해야합니다 UItableView.

    txtvwMsgText.userInteractionEnabled = YES;
    txtvwMsgText.dataDetectorTypes = UIDataDetectorTypeLink;
    txtvwMsgText.scrollEnabled = NO;
    txtvwMsgText.editable = NO;
    txtvwMsgText.selectable = YES;

확인해야합니다 NSDataDetector.

이를 사용하여 다른 데이터 (링크, 전화 번호 등)를 찾고 처리 할 수 ​​있습니다. 이 사이트를보십시오 :

http://nshipster.com/nsdatadetector/

dataDetectorTypes속성을 사용하여 UITextView코드에서 감지 할 항목을 설정할 수도 있습니다 . 스토리 보드 전환 문제 일 수 있습니다.

textView.dataDetectorTypes = UIDataDetectorTypeLink;

textview는 편집 할없는 경우에만 링크를 인식합니다 .

다음은 editable UITextView'링크 감지' 로 만드는 방법에 대한 멋진 튜토리얼입니다.

링크 감지 기능이있는 편집 가능한 UITextView

나는 지금부터 그 해결책으로 어떤 문제도 경험하지 않았습니다.

트릭은 GestureRecognizer 가 터치를 경고하고 편집을 활성화 / 비활성화하는 것입니다.

selectable / not selectable문제 와 동일한 것을 적용 할 수 있습니다.iOS7


몇 번의 테스트 끝에 해결책을 찾았습니다.

링크를 활성화하고 선택을 활성화하지 않으려면 gestureRecognizers를 편집해야합니다.

예를 들어, 3 개의 LongPressGestureRecognizer가 있습니다. 하나는 링크 클릭 (minimumPressDuration = 0.12), 두 번째는 편집 가능 모드 (minimumPressDuration = 0.5), 세 번째는 선택 (minimumPressDuration = 0.8)입니다. 이 솔루션은 선택을 위해 LongPressGestureRecognizer를 제거하고 편집 모드에서 확대 / 축소를 위해 두 번째를 제거합니다.

NSArray *textViewGestureRecognizers = self.captionTextView.gestureRecognizers;
NSMutableArray *mutableArrayOfGestureRecognizers = [[NSMutableArray alloc] init];
for (UIGestureRecognizer *gestureRecognizer in textViewGestureRecognizers) {
    if (![gestureRecognizer isKindOfClass:[UILongPressGestureRecognizer class]]) {
        [mutableArrayOfGestureRecognizers addObject:gestureRecognizer];
    } else {
        UILongPressGestureRecognizer *longPressGestureRecognizer = (UILongPressGestureRecognizer *)gestureRecognizer;
        if (longPressGestureRecognizer.minimumPressDuration < 0.3) {
            [mutableArrayOfGestureRecognizers addObject:gestureRecognizer];
        }
    }
}
self.captionTextView.gestureRecognizers = mutableArrayOfGestureRecognizers;

iOS 9에서 테스트되었지만 모든 버전 (iOS 7, 8, 9)에서 작동합니다. 도움이 되었기를 바랍니다. :)


트릭을 찾았습니다. 이것은 iOS 7에서 작동합니다!

xib에서 선택 가능하거나 프로그래밍 방식으로 UITextView를 설정해야합니다.

self.yourTextView.selectable = YES;

그런 다음 텍스트를 설정 한 후 스크롤을 비활성화하고 다시 활성화해야합니다.

self.yourTextView.scrollEnabled = NO;
[self.yourTextView setText:contentString];
self.yourTextView.scrollEnabled = YES;

따라서 UITextView를 사용, 선택 가능, 스크롤 불가능 및 링크 감지 가능 상태로 유지하는 것은보기만큼 간단하지 않습니다. iOS 8에서이 문제가 발생했습니다. 그래서 제 해결책은 viewDidLoad에서 이와 같은 작업을 수행 한 다음 textBox 편집이 완료 될 때 편집 가능한 속성을 NO로 설정하는 것이 었습니다 (일반적으로 doneIsTapped와 같은 메서드가 됨). 여기서 트릭은 textview에 텍스트 값을 설정 한 후 편집 가능한 속성을 NO로 설정하는 것입니다. 그러면 UITextview에서 링크가 활성화됩니다.

- (void)viewDidLoad 
{
    [super viewDidLoad];
    self.txtViewComment.editable = YES;
    self.txtViewComment.selectable = YES;
    self.txtViewComment.dataDetectorTypes = UIDataDetectorTypeLink;
    self.txtViewComment.scrollEnabled = NO;
}

- (IBAction)doneIsTapped:(id)sender 
{
    self.txtViewComment.text = @"set text what ever you want";
    self.txtViewComment.editable = NO; 
}

이것은 textview에서 링크를 활성화했습니다. 또한 지금은 스토리 보드를 사용하지 않고 (또는 애플이이 문제를 해결할 때까지) 불필요한 혼란을 피하기 위해 코드를 사용하는 것이 좋습니다. 이 도움을 바랍니다.


Deactivating UITextViews scrolling ability did the trick for me in a similar setup.


Changing the Tint color to other color actually works. However if selectable enable the tint will also be the same color.


Make the scrolling property of UITextView to No. it will work... Self.textView.ScrollingEnable = NO;


This workaround works for me:

textView.selectable = YES;
textView.delegate = self;

- (void) textViewDidChangeSelection:(UITextView *)textView;
{
    NSRange range = NSMakeRange(NSNotFound, 0.0);
    if ( range.length && !NSEqualRanges(range, textView.selectedRange) ) {
        textView.selectedRange = range;
    }
}

If you are adding UITextview programmatically just add below lines:

        _textView.userInteractionEnabled = YES;
        _textView.dataDetectorTypes = UIDataDetectorTypeLink;
        _textView.scrollEnabled = NO;
        _textView.editable = NO;

This worked for me.


None of the above worked for me, instead I did this:

[self.textView setDataDetectorTypes:UIDataDetectorTypeNone];
[self.textView.setTextColor:[UIColor whiteColor]];
[self.textView setDataDetectorTypes:UIDataDetectorTypeNone];

I did this with my textview that was supposed to detect all types, and which had non detected color set to white. You can change the code to represent your proper color and link types to detect.


While this thread is old, I didn’t see an answer that worked for me with Swift, so here goes for Swift 2.2

textView.dataDetectorTypes = UIDataDetectorTypes.Link
textView.selectable = true

참고URL : https://stackoverflow.com/questions/18962742/uitextview-link-detection-in-ios-7

반응형