如何允许NSAttributedString文本输入UITextView? [英] How to allow NSAttributedString text be typed into a UITextView?

查看:167
本文介绍了如何允许NSAttributedString文本输入UITextView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将不同样式的文本输入到UITextView中,有点像文本编辑器,使用粗体或斜体等简单属性。我理解通过使用textView的 attributedText 属性,我可以将属性应用于特定范围的文本。这很好,但我希望能够在textView中输入属性文本,这将通过按钮切换(例如输入粗体文本)。

I'm trying to allow different styles of text to be typed into a UITextView, a bit like a text editor using simple attributes such as bold or italics. I understand by using the textView's attributedText property I can apply attributes to certain ranges of text. This is nice, but I would like to be able to type attributed text into the textView, which would be toggled by a button (such as the typing of bold text).

这是我到目前为止所想到的:

Here's what I've thought of so far:

我使用了 - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange: (NSRange)range replacementText:(NSString *)text UITextView委托方法获取 text 参数,并通过创建NSAttributedString来使用属性修改它同一文字。然后创建一个NSMutableAttributedString,它是 textView 的attributedText的副本。使用 appendAttributedString 附加两个,然后将textView的 attributedText 属性设置为生成的attributionString。

I've used the -(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text UITextView delegate method to take the text argument, and modify it with attributes by creating an NSAttributedString with the same text. Then create a NSMutableAttributedString, which is a copy of the textView's attributedText. Append the two using appendAttributedString, and then set the textView's attributedText property to the resulting attributedString.

这是代码:

-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {

   if (self.boldPressed) {

        UIFont *boldFont = [UIFont boldSystemFontOfSize:self.textView.font.pointSize];
        NSDictionary *boldAttr = [NSDictionary dictionaryWithObject:boldFont forKey:NSFontAttributeName];
        NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc]initWithString:text attributes:boldAttr];
        NSMutableAttributedString *textViewText = [[NSMutableAttributedString alloc]initWithAttributedString:textView.attributedText];
        [textViewText appendAttributedString:attributedText];
        textView.attributedText = textViewText;

           return NO;
       }
   return YES;                
}

每次输入字符时都必须重置textViews attributionText似乎有点像一个简单的行动。不仅如此,它还不能正常工作。以下是启用粗体属性时的样子:

Having to reset the textViews attributedText every time a character is typed seems like a bit much for a simple action. Not only that, but it doesn't work properly. Here's what it looks like when the bold attribute is enabled:

这有两个问题。最明显的是每个新角色如何被放到一个新线上。但同样奇怪的是插入点始终位于textview中文本的第一个索引处(仅当启用粗体时,粗体字符才会插入新行)。因此,如果您输入粗体启用,然后关闭加粗,请在所有现有文本前键入简历。

There are two problems with this. The most obvious being how every new character is put onto a new line. But what's also strange is that the insertion point is always at the very first index of the text in the textview (only when bold is enabled, yet the bold character is inserted on a new line). So if you're typing with bold enabled, and then turn bold off, typing resumes in front of all existing text.

我不确定为什么会发生这些错误。我也不认为我的解决方案非常有效,但我想不出任何其他方式来实现它。

I'm not sure why these error are happening. I also just don't think that my solution is very efficient, but I can't think of any other way of implementing it.

推荐答案

这是 setTypingAttributes:的用途。只要用户按下您的某个属性按钮,就会将其设置为属性字典,并且新字符将获取所请求的属性。

This is what setTypingAttributes: is for. Set this to your attribute dictionary whenever the user presses one of your attribute buttons and new characters will pick up the requested attributes.

这篇关于如何允许NSAttributedString文本输入UITextView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆