首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用NSMutableAttributedString的NSMutableParagraphStyle

使用NSMutableAttributedString的NSMutableParagraphStyle
EN

Stack Overflow用户
提问于 2014-06-21 11:22:21
回答 1查看 3.4K关注 0票数 3

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

代码语言:javascript
复制
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;

谢谢..

EN

回答 1

Stack Overflow用户

发布于 2014-06-21 11:56:33

我解决了它:)

代码语言:javascript
复制
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;
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24337975

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档