如何在 UITextView 中为文本着色
Posted
技术标签:
【中文标题】如何在 UITextView 中为文本着色【英文标题】:How to color text in UITextView 【发布时间】:2016-04-05 10:32:38 【问题描述】:我在视图控制器上有四个按钮和一个文本视图。这五个按钮有颜色,例如红色、黄色、绿色、蓝色和黑色。
当用户开始键入而不按这些按钮时,正在键入的文本视图的颜色应该是黑色文本。如果用户按下红色按钮,那么从该点开始的文本颜色应该是红色,直到用户按下任何其他颜色的按钮。
如何做到这一点?我已经按照这个教程https://www.objc.io/issues/5-ios7/getting-to-know-textkit/
但不知道如何将其定制为我想要实现的目标。
【问题讨论】:
您可能想尝试子类化UITextView
并实现/覆盖UITextInput
协议的textStylingAtPosition(_:inDirection:)
【参考方案1】:
如果对您有帮助,我会按照以下方式进行:
1- add one property to retain current Color and initialize it with black color
@property (nonatomic, retain) UIColor *curColor;//in your interface declaration
self.curColor = [UIColor blackColor];//Init it in Viewdidload for example
2- 实现 UITextViewDelegate
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
NSAttributedString *currentText = self.Textview.attributedText;//To store current text and its attributs
NSAttributedString *newOneText = [[NSAttributedString alloc] initWithString:text attributes:@NSForegroundColorAttributeName:self.curColor];//for the new text with selected color
NSMutableAttributedString *shouldDisplayText = [[NSMutableAttributedString alloc] initWithAttributedString: currentText];
[shouldDisplayText appendAttributedString: newOneText];// add old and new text
self.Textview.attributedText = shouldDisplayText;//set it ton control
return NO;
3- 添加 IBAction 来改变颜色 =>
- (IBAction) redColorClicked
self.curColor = [UIColor colorWithRed:1.0f green: 0.0f blue:0.0f alpha:1.0f];
- (IBAction) blueColorClicked
self.curColor = [UIColor colorWithRed:0.0f green: 0.0f blue:1.0f alpha:1.0f];
【讨论】:
虽然这是实现预期结果的方法,但当按下键盘中的字符时,它会加倍输入的字符。如何在不重复字符的情况下做到这一点? 嗨!抱歉回复晚了。对于如何在不重复字符的情况下做到这一点?只需更改 UITextView 委托的返回值: - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text 为 NO,您就完成了。我修改了上面的代码来满足你的需要【参考方案2】:你需要使用NSAttributedString
类。
let defaultAttributes = [NSFontAttributeName: UIFont.systemFontOfSize(UIFont.systemFontSize()),
NSForegroundColorAttributeName: UIColor.blackColor()]
let text = "this text is red and yellow"
let str = NSMutableAttributedString(string: text, attributes: defaultAttributes)
str.setAttributes([NSForegroundColorAttributeName: UIColor.redColor()], range: (text as NSString).rangeOfString("red"))
str.setAttributes([NSForegroundColorAttributeName: UIColor.yellowColor()], range: (text as NSString).rangeOfString("yellow"))
textView.attributedText = str
【讨论】:
【参考方案3】:您可以使用 NSMutableAttributedString 来实现。思路如下(我没有测试,这里是手写的):
NSString *str = @"***";
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:str];
// Set foreground color of "stack" substring in our string to red
[attributedString addAttribute:NSForegroundColorAttributeName
value:[UIColor redColor];
range:NSMakeRange(0, 5)];
使用此方法,您可以实现将颜色应用于文本中所需范围的目标。
你可以像这样为你的 UILabel 设置属性文本:
yourLabel.attributedText = attributedString
【讨论】:
以上是关于如何在 UITextView 中为文本着色的主要内容,如果未能解决你的问题,请参考以下文章