2013-10-18 3 views
0

iOS 7에서 속성이있는 텍스트를 사용하여 UITextView를 사용하고 있습니다. UITextView에 입력하기 전에 파싱되는 원시 텍스트는 다음과 유사합니다.iOS 7 UITextView 단어 또는 이미지 선택

"내 예를 들어, @ (홍길동) (johndoeid) @ (신원 미상) (janedoeid)에 오신 것을 환영합니다"

나는이 텍스트를 분석하고이처럼 보이는 UITextView에 넣어

.

는 "내 예를 홍길동신원 미상에 오신 것을 환영합니다"

내 질문은 이것이다.

텍스트 뷰에서 John Doe 또는 Jane Doe를 클릭하면 "johndoe"또는 "janedoe"사용자의 ID를 어떻게 얻을 수 있습니까? 나는 원래 위치와 새로운 위치에서 떨어져 가게를 생각하고 그것을 사용하지만 그 clunky 보인다.

답변

0

예, 모든 위치를 NSDictionary에 저장하고 해당 데이터 구조에서 조회하는 것이 가장 좋습니다.

나는 그것이 clunky 인 것처럼 알고있다. 그러나 그것은 정말로 당신의 유일한 선택이다.

1

NSAttributedString을 사용한다고 가정하고 텍스트에 사용자 지정 특성을 추가합니다. 텍스트와 관련된 특정 속성을 검색하는 방법의 예를 보려면 아래 링크를 참조하십시오.

예제는 실제로 사용자 지정 특성을 만들지 않지만 모든 글꼴 특성을 검색하고 글꼴 크기를 변경하는 방법을 볼 수 있습니다. 사용자 정의 속성을 첨부하면 비슷한 기능을 수행 할 수 있습니다. 막히게되면 알려주고 좀 더 구체적으로 해킹 해 볼 수 있습니다.

http://ossh.com.au/design-and-technology/software-development/implementing-rich-text-with-images-on-os-x-and-ios/

구체적으로 resizeText 방법에 대한 OSTextView.m 소스 목록 파일을 찾습니다. 여기에 NSFont 속성을 대체 할 수있는 코드는 코드의 일부는 또한 아래 방법에 NSFont 속성

[self.textStorage enumerateAttributesInRange:rangeAll options:NSAttributedStringEnumerationLongestEffectiveRangeNotRequired usingBlock: 
^(NSDictionary *attributes, NSRange range, BOOL *stop) { 

    // Iterate over each attribute and look for a Font Size 
    [attributes enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) { 
     if ([[key description] isEqualToString:@"NSFont"]) { 
      UIFont *font = obj; 
      float fontSize = font.pointSize + bySize; 
      smallestFontSize = MIN(smallestFontSize, fontSize); 
      largestFontSize = MAX(largestFontSize, fontSize); 
     } 

    }]; 
}]; 

을 검색입니다, 당신의 경우에 당신은 당신의 사용자 정의 속성을 추가 할 수 있습니다 - 우리가 먼저 복사주의 기존 속성을 제거하고 싶지 않을 수 있으므로 기존 속성을 추가 한 다음 추가하십시오.

[self.textStorage enumerateAttributesInRange:rangeAll options:NSAttributedStringEnumerationLongestEffectiveRangeNotRequired usingBlock: 
^(NSDictionary *attributes, NSRange range, BOOL *stop) { 

    NSMutableDictionary *mutableAttributes = [NSMutableDictionary dictionaryWithDictionary:attributes]; 

    // Iterate over each attribute and look for a Font Size 
    [mutableAttributes enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) { 

     if ([[key description] isEqualToString:@"NSFont"]) { 

      UIFont *font = obj; 
      float fontSize = font.pointSize; 
      fontSize += bySize; 
      fontSize = MIN(fontSize, MAXFONTSIZE); 

      // Get a new font with the same attributes and the new size 
      UIFont *newFont = [font fontWithSize:fontSize]; 

      // Replace the attributes, this overrides whatever is already there 
      [mutableAttributes setObject:newFont forKey:key]; 
     } 

    }]; 

    // Now replace the attributes in ourself (UITextView subclass) 
    [self.textStorage setAttributes:mutableAttributes range:range]; 
}]; 

이제 사용자 정의 속성은 깔끔하게 기인 문자열에 포함 당신은 그것을 보관 할 수 그것을 잃어버린없이 보관을 취소해야합니다.