私のアプリにはUItextViewがあり、その入力ビューにはいくつかのボタンが付いたアクセサリビューがあります。
既に入力されているテキストを変更して、テキストに下線を引こうとしています。正常に動作しますが、変換後にフォントとフォントサイズが失われます。
これが私がやっていることです:
NSRange selectedRange = [self.commentsTextView selectedRange];
NSDictionary *currentAttributesDict = [self.commentsTextView.textStorage attributesAtIndex:selectedRange.location
effectiveRange:nil];
NSDictionary *dict;
if ([currentAttributesDict objectForKey:NSUnderlineStyleAttributeName] == nil ||
[[currentAttributesDict objectForKey:NSUnderlineStyleAttributeName] intValue] == 0) {
dict = @{NSUnderlineStyleAttributeName: [NSNumber numberWithInt:1]};
}
else{
dict = @{NSUnderlineStyleAttributeName: [NSNumber numberWithInt:0]};
}
[self.commentsTextView.textStorage beginEditing];
[self.commentsTextView.textStorage setAttributes:dict range:selectedRange];
[self.commentsTextView.textStorage endEditing];