또 다른 가능성은 숨기려는 텍스트에 사용자 정의 속성을 사용하는, 그리고 자신을 작성합니다 메서드를 NSAttributedString
에있는 범주에서 숨김으로 표시된 텍스트를 제외하는 새 특성 문자열을 만듭니다.
- (NSAttributedString *)attributedStringWithoutHiddenText {
NSMutableAttributedString *result = [[[NSMutableString alloc] init] autorelease];
NSRange fullRange = NSMakeRange(0, [self length]);
NSRange range = NSZeroRange;
while (NSMaxRange(range) < [self length]) {
NSDictionary *attributes = [self attributesAtIndex:range.location longestEffectiveRange:&range inRange:fullRange];
if ([[attributes objectForKey:MyHiddenTextAttribute] boolValue])
continue;
NSAttributedString *substring = [[NSAttributedString alloc] initWithString:[[self string] substringWithRange:range] attributes:attributes];
[result appendAttributedString:substring];
[substring release];
}
return result;
}
주의해야 할 점은 : 나는 완전히 내 머리 위로 떨어져 쓴, 등 네 개,
이는 것을 걷어차하지, 작업, 화재에 하드 드라이브에 불을 컴파일 보장 아니에요 그리기에 적합한 문자열을 생성하지만 숨겨진 텍스트에 액세스하려면 원래 문자열이 필요합니다. 문자열의 크기에 따라 큰 메모리 오버 헤드가 될 수 있습니다.
으로 설정하는 것입니다. 첫 번째 줄에는 NSMutableAttributedString이 있어야합니다. 내가 말했듯이 이것은 테스트되지 않았다 :-) – Alex
당신의 솔루션과 나의 차이점을 언급해야한다. 나의 솔루션은 표시된 범위에서 공백을 남기는 반면 실제로 코드는 'MyHiddenTextAttribute'로 문자를 제거한다. –
예,이 코드는 표시하기에 적합한 문자열을 만듭니다. 원래의 질문은 CSS 표시 : none 속성과 같은 것을 요구했고이 방법으로 생성 된 문자열을 그릴 때 비슷한 효과를 얻습니다. – Alex