objective-c - NSMutableParagraphStyle 与 NSMutableAttributedString

标签 objective-c uitextview uicolor nsmutableattributedstring

我有一个段落正在使用 NSMutableParagraphStyle 来管理行高。另外,我想更改段落中一个单词的颜色,这是我正在使用的代码,但它只更改一个单词的颜色(attributedText 被覆盖)我该如何解决?有帮助吗??

NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];

paragraphStyle.lineHeightMultiple = 1.45f;

paragraphStyle.alignment = UITextAlignmentRight;

NSDictionary *ats = @{
                      NSFontAttributeName            : [UIFont fontWithName:@"Helvetica"
                                                                      size:currentSize],
                      NSParagraphStyleAttributeName  : paragraphStyle,

                      };


ayaTxt.attributedText    = [[NSAttributedString alloc] initWithString:ayaTxt.text
                                                             attributes:ats];

NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc]
                                         initWithString:ayaTxt.text];

NSRange range = [[AyatWords objectAtIndex:aya] rangeOfString:@":"];

[attrString addAttribute:NSForegroundColorAttributeName
                   value:[UIColor colorWithRed:(25.0/255.0)
                                         green:(168.0/255.0)
                                          blue:(167.0/255.0)
                                         alpha:1.0]
                   range:NSMakeRange(0, range.location + 1)];

ayaTxt.attributedText = attrString;

谢谢..

最佳答案

我解决了:)

NSMutableAttributedString *as = [[NSMutableAttributedString alloc] initWithString:wordsTxt.text];

NSRange range = [[AyatWords objectAtIndex:aya] rangeOfString:@":"];

NSRange wholeRange = NSMakeRange(0, as.length);

[as addAttribute:NSForegroundColorAttributeName
                   value:[UIColor colorWithRed:(25.0/255.0)
                                         green:(168.0/255.0)
                                          blue:(167.0/255.0)
                                         alpha:1.0]
                   range:NSMakeRange(0, range.location + 1)];

[as addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:currentSize]
                                     range:wholeRange];

NSMutableParagraphStyle *ps = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];

ps.lineHeightMultiple = 1.45f;

ps.alignment = UITextAlignmentRight;


[as addAttribute:NSParagraphStyleAttributeName value:ps range:wholeRange];

wordsTxt.attributedText = as;

关于objective-c - NSMutableParagraphStyle 与 NSMutableAttributedString,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24337975/

相关文章:

iOS:等到用户完成输入然后发送请求

ios - 在没有缓存的情况下检查 HTTP 状态代码

ios - 在 UITextView Objective C 中检测 Tap 不可编辑和不可选择

ios - 如何使用多个变量一起自定义具有不同字体大小和颜色的 UILabel

ios - 付款队列 :(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions is not called while restoreCompletedTransactions

ios - 如何在 scrollViewDidScroll 中添加一个 UIView 并使其滚动到最后?

swift - Swift 2 中 Pixel 的颜色

objective-c - 设置 UIColor 属性时 KVC 调用的 componentRGBA 方法的实现

ios - 删除 UITextView 链接上的下划线

ios - 动画字符串(逐个字符)在动画期间不会出现在 UITextView 上