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];
}];
이제 사용자 정의 속성은 깔끔하게 기인 문자열에 포함 당신은 그것을 보관 할 수 그것을 잃어버린없이 보관을 취소해야합니다.