gpt4 book ai didi

objective-c - NSMutableParagraphStyle 与 NSMutableAttributedString

转载 作者:搜寻专家 更新时间:2023-10-30 20:02:33 24 4
gpt4 key购买 nike

我有一个段落正在使用 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com