ios - 在 Attributed TextView (Xcode) 中更改文本颜色

标签 ios xcode textview

我正在尝试更改属性化的 TextView 中句子的颜色。我该怎么做?

比如说,我想把第一句话的颜色改成红色。

enter image description here

当我点击这些按钮时(左边是“文本颜色”,另一个是“背景颜色”)没有任何反应。 enter image description here

最佳答案

您可以通过编程方式实现这一点。使用 NSMutableAttributedString

NSMutableAttributedString *stringText = [[NSMutableAttributedString alloc] initWithString:@"This is awesome"];
//Green color for the first four characters.
[stringText addAttribute: NSForegroundColorAttributeName value: [UIColor greenColor] range: NSMakeRange(0, 4)];
// Sets the font color of last four characters to yellow.
[stringText addAttribute: NSForegroundColorAttributeName value: [UIColor yellowColor] range: NSMakeRange(14, 10)];

将此文本设置为您的 UITextView。它应该工作。还要确保文本是否动态更改,您需要充分注意 NSMakeRange。

关于ios - 在 Attributed TextView (Xcode) 中更改文本颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32637779/

相关文章:

objective-c - 从 MapBox map 中删除信息按钮

Android TextView 以及如何确定可见字符数

java - 从方法更改 TextView (Android)

Canvas 上的 Android 文本大小与 TextView 中的文本大小不同

ios - SCNCamera 是否以固定距离跟随节点?

ios - 在 Swift 中初始化 Objective-C 类的属性会导致意外的值和类型

ios - 如何在不打开 MailComposer 或 objective-c 中的新 View 的情况下通过代码发送邮件

ios - 当音频设备添加到 AVCaptureSession 时,UIImpactFeedbackGenerator 不工作

iphone - iOS:日期组件查询

iOS swift : Static Cell