Ios 为什么只有在使用mutableCopy时,NSMutableAttributedString addAttribute才能工作
我有以下代码:Ios 为什么只有在使用mutableCopy时,NSMutableAttributedString addAttribute才能工作,ios,objective-c,nsstring,nsattributedstring,nsmutablecopying,Ios,Objective C,Nsstring,Nsattributedstring,Nsmutablecopying,我有以下代码: NSMutableAttributedString *attrS = [[NSMutableAttributedString alloc] initWithString:@"• Get Tested Son"]; NSMutableAttributedString *boldS = [[NSMutableAttributedString alloc] initWithString:@"Son"]; [boldS addAttribute:NSFontAttributeName
NSMutableAttributedString *attrS = [[NSMutableAttributedString alloc] initWithString:@"• Get Tested Son"];
NSMutableAttributedString *boldS = [[NSMutableAttributedString alloc] initWithString:@"Son"];
[boldS addAttribute:NSFontAttributeName value:SOMEBOLDFONT range:NSMakeRange(0, boldS.length)];
[attrS replaceCharactersInRange:[attrS.string rangeOfString:boldS.string]
withAttributedString:boldS];
如您所见,我想加粗子
部分。如果我做了上述陈述,这不起作用,但只有当我做了以下事情时,这才起作用:
[[attrS mutableCopy] replaceCharactersInRange:[attrS.string rangeOfString:boldS.string]
withAttributedString:boldS];
原因可能是什么?
addAttribute
无论您是否使用mutableCopy
都有效。你的问题基于错误的假设。因此,它没有答案
运行以下命令:
NSMutableAttributedString *attrS = [[NSMutableAttributedString alloc] initWithString:@"• Get Tested Son"];
NSMutableAttributedString *boldS = [[NSMutableAttributedString alloc] initWithString:@"Son"];
UIFont *someBoldFont = [UIFont fontWithName:@"Arial" size:23.0f];
[boldS addAttribute:NSFontAttributeName value:someBoldFont range:NSMakeRange(0, boldS.length)];
NSMutableAttributedString *attrSCopy = [attrS mutableCopy];
[attrS replaceCharactersInRange:[attrS.string rangeOfString:boldS.string]
withAttributedString:boldS];
[attrSCopy replaceCharactersInRange:[attrS.string rangeOfString:boldS.string]
withAttributedString:boldS];
NSLog(@"%@", [attrS isEqual:attrSCopy] ? @"equal" : @"different");
它将输出
相等的。注释掉attrS
或attroscopy
的replaceCharactersRange:
,它将输出不同的
,addAttribute
工作,无论您是否使用mutableCopy
。你的问题基于错误的假设。因此,它没有答案
运行以下命令:
NSMutableAttributedString *attrS = [[NSMutableAttributedString alloc] initWithString:@"• Get Tested Son"];
NSMutableAttributedString *boldS = [[NSMutableAttributedString alloc] initWithString:@"Son"];
UIFont *someBoldFont = [UIFont fontWithName:@"Arial" size:23.0f];
[boldS addAttribute:NSFontAttributeName value:someBoldFont range:NSMakeRange(0, boldS.length)];
NSMutableAttributedString *attrSCopy = [attrS mutableCopy];
[attrS replaceCharactersInRange:[attrS.string rangeOfString:boldS.string]
withAttributedString:boldS];
[attrSCopy replaceCharactersInRange:[attrS.string rangeOfString:boldS.string]
withAttributedString:boldS];
NSLog(@"%@", [attrS isEqual:attrSCopy] ? @"equal" : @"different");
它将输出相等的。注释掉attrS
或attroscopy
的replaceCharactersRange:
,它将输出不同的
,addAttribute
工作,无论您是否使用mutableCopy
。你的问题基于错误的假设。因此,它没有答案
运行以下命令:
NSMutableAttributedString *attrS = [[NSMutableAttributedString alloc] initWithString:@"• Get Tested Son"];
NSMutableAttributedString *boldS = [[NSMutableAttributedString alloc] initWithString:@"Son"];
UIFont *someBoldFont = [UIFont fontWithName:@"Arial" size:23.0f];
[boldS addAttribute:NSFontAttributeName value:someBoldFont range:NSMakeRange(0, boldS.length)];
NSMutableAttributedString *attrSCopy = [attrS mutableCopy];
[attrS replaceCharactersInRange:[attrS.string rangeOfString:boldS.string]
withAttributedString:boldS];
[attrSCopy replaceCharactersInRange:[attrS.string rangeOfString:boldS.string]
withAttributedString:boldS];
NSLog(@"%@", [attrS isEqual:attrSCopy] ? @"equal" : @"different");
它将输出相等的。注释掉attrS
或attroscopy
的replaceCharactersRange:
,它将输出不同的
,addAttribute
工作,无论您是否使用mutableCopy
。你的问题基于错误的假设。因此,它没有答案
运行以下命令:
NSMutableAttributedString *attrS = [[NSMutableAttributedString alloc] initWithString:@"• Get Tested Son"];
NSMutableAttributedString *boldS = [[NSMutableAttributedString alloc] initWithString:@"Son"];
UIFont *someBoldFont = [UIFont fontWithName:@"Arial" size:23.0f];
[boldS addAttribute:NSFontAttributeName value:someBoldFont range:NSMakeRange(0, boldS.length)];
NSMutableAttributedString *attrSCopy = [attrS mutableCopy];
[attrS replaceCharactersInRange:[attrS.string rangeOfString:boldS.string]
withAttributedString:boldS];
[attrSCopy replaceCharactersInRange:[attrS.string rangeOfString:boldS.string]
withAttributedString:boldS];
NSLog(@"%@", [attrS isEqual:attrSCopy] ? @"equal" : @"different");
它将输出相等的。注释掉attrS
或attrSCopy
的replaceCharactersRange:
,它将输出不同的它是如何工作的?你在哪里调用addAttribute
?我看不出你在哪里加粗NSMutableAttributedString
:你所做的只是为同一个字符串更改一个字符串。是否有一个addAttributes
或类似的东西你忘了在这里发布?我不能复制这个。这是你运行过并遇到问题的真实代码吗?和Chuck一样,我运行了这段代码,它工作得非常完美。你在其他地方犯了一个错误。凭直觉,mutableCopy
创建了一个接收者的副本,即一个与接收者具有相同属性的新对象,它是可变的。它怎么不起作用?你在哪里调用addAttribute
?我看不出你在哪里加粗NSMutableAttributedString
:你所做的只是为同一个字符串更改一个字符串。是否有一个addAttributes
或类似的东西你忘了在这里发布?我不能复制这个。这是你运行过并遇到问题的真实代码吗?和Chuck一样,我运行了这段代码,它工作得非常完美。你在其他地方犯了一个错误。凭直觉,mutableCopy
创建了一个接收者的副本,即一个与接收者具有相同属性的新对象,它是可变的。它怎么不起作用?你在哪里调用addAttribute
?我看不出你在哪里加粗NSMutableAttributedString
:你所做的只是为同一个字符串更改一个字符串。是否有一个addAttributes
或类似的东西你忘了在这里发布?我不能复制这个。这是你运行过并遇到问题的真实代码吗?和Chuck一样,我运行了这段代码,它工作得非常完美。你在其他地方犯了一个错误。凭直觉,mutableCopy
创建了一个接收者的副本,即一个与接收者具有相同属性的新对象,它是可变的。它怎么不起作用?你在哪里调用addAttribute
?我看不出你在哪里加粗NSMutableAttributedString
:你所做的只是为同一个字符串更改一个字符串。是否有一个addAttributes
或类似的东西你忘了在这里发布?我不能复制这个。这是你运行过并遇到问题的真实代码吗?和Chuck一样,我运行了这段代码,它工作得非常完美。你在其他地方犯了一个错误。凭直觉,mutableCopy
创建了一个接收者的副本,也就是说,一个与接收者具有相同属性的新对象,它是可变的。