Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/109.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 字符串和属性问题_Ios_Nsstring_Nsattributedstring - Fatal编程技术网

Ios 字符串和属性问题

Ios 字符串和属性问题,ios,nsstring,nsattributedstring,Ios,Nsstring,Nsattributedstring,我一直在胡闹,没有得到应有的结果。我的代码如下 NSMutableAttributedString *nameString = [[NSMutableAttributedString alloc]initWithString:((User *)appDelegate.users[self.currentUserIndex]).name]; [nameString addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Helve

我一直在胡闹,没有得到应有的结果。我的代码如下

NSMutableAttributedString *nameString = [[NSMutableAttributedString alloc]initWithString:((User *)appDelegate.users[self.currentUserIndex]).name];
[nameString addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Helvetica-Bold" size:12.0] range:NSMakeRange(0, nameString.length)];

NSString *adviceString = [[NSString alloc] initWithFormat:@"%@, remember be extra aware of the \n cat today. The dog index is 4, dogcatcher \n suggests you should at minimum wear \n a dog and apply cat...", [nameString string]];

因此,它不会根据需要加粗用户名。我尝试对adviceString使用
NSAttributedString
,但无法使用
initWithFormat:
方法并在字符串后列出变量,并且我没有看到任何
NSAttributedString
等效项。我想让您
NSAttributedString
而不是
NSMutableAttributedString
,但它似乎无法识别
addAttribute:value:fontWithName:range
方法。有人能帮我吗?非常感谢您的帮助。

这是肯定的,您还需要为
adviceString
使用NSAttributedString来获得属性

如果需要使用
stringWithFormat:

NSString *fullName = @"Anoop Kumar Vaidya";
NSMutableAttributedString *attributedName = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"Hello %@", fullName]
                                       attributes:@{NSFontAttributeName : [UIFont boldSystemFontOfSize:[UIFont systemFontSize]]}];
在属性中,您可以再添加几个所需/所需的属性

编辑2:

您可能希望使用以下方法:

-(NSMutableAttributedString *)normalString:(NSString *)string{
    return [[NSMutableAttributedString alloc] initWithString:string
                                                  attributes:@{}];
}

-(NSMutableAttributedString *)boldString:(NSString *)string{
    return [[NSMutableAttributedString alloc] initWithString:string
                                                  attributes:@{NSFontAttributeName : [UIFont boldSystemFontOfSize:[UIFont systemFontSize]]
                                                               }];
}
然后根据您的要求使用:

NSMutableAttributedString *attributedName = [self boldString:fullName];
[attributedName appendAttributedString:[self normalString:@" is creating one"]];
[attributedName appendAttributedString:[self boldString:@" bold"]];
[attributedName appendAttributedString:[self normalString:@" string."]];

为什么不将属性化字符串插入您的
adviceString
,它也应该是属性化的。否则它将不保存属性。你到底想在这里实现什么?我想要4个变量,我可以加粗并将它们插入到我的标准字符串中,因为你不能使用
NSString
NSAttributedString
是一个不错的选择。是的,我发现我甚至不能追加byattributedstring:basicNSString。谢谢请更正您的答案,使其包含(比如3个)可以采用粗体大小的变量,然后将其添加到普通字符串中。你的例子帮助了我,但它使整个字符串变粗了,这似乎没有意义,并给了我一些想法(尽管做一件简单的事情似乎很难),谢谢!我认为您应该用anNSAttributedString替换上一个代码部分中的“string”,这只是为了澄清和避免误解。