簡體   English   中英

如何更改NSTextView中選定文本的大小?

[英]How can I change the size of the selected text from a NSTextView?

我有一個NSTextView,並且正在嘗試創建一個簡單的文本編輯器。 我的文本視圖可以具有幾種不同的字體。 例如,如果從文本視圖中選擇的文本具有兩種不同的字體,我如何僅更改所選文本的大小?

當選擇了textView中的文本時,可以使用textViewDidChangeSelection: UITextViewDelegate回調,並根據所選范圍更改其屬性。 這是將所選文本更改為systemFont 25pt greenColor的示例:

- (void)viewDidLoad {
    [super viewDidLoad];
    self.defaultAttributes = @{NSFontAttributeName: [UIFont systemFontOfSize:17.0f], NSForegroundColorAttributeName : [UIColor blackColor]};
}

- (void)textViewDidChangeSelection:(UITextView *)textView {
    [[NSOperationQueue mainQueue] addOperationWithBlock:^{
        // set the entire text view to the default attributes initially (in case previously selected text had it's attributes changed)
        [[textView textStorage] setAttributes:self.defaultAttributes range:NSMakeRange(0, textView.attributedText.length)];
        // now set our selected text to the desired attributes
        NSDictionary <NSAttributedStringKey, id> *selectedAttributes = @{NSFontAttributeName : [UIFont systemFontOfSize:25.0f], NSForegroundColorAttributeName: [UIColor greenColor]};
        [[textView textStorage] setAttributes:selectedAttributes range:textView.selectedRange];
    }];
}

您顯然需要使此視圖控制器成為textView的委托

https://developer.apple.com/documentation/uikit/uitextviewdelegate/1618620-textviewdidchangeselection?language=objc

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM