Ios 可能有两种字体大小的NSString
是否可以有一个Ios 可能有两种字体大小的NSString,ios,objective-c,nsstring,Ios,Objective C,Nsstring,是否可以有一个NSString,它的第一个单词是一种字体大小/颜色,下一个单词是另一种字体大小和颜色 我在想可能是NSAttributedString——但不确定这是不是正确的方法 我尝试将两个NSString放入NSAttributedString——但没有成功 正在查找具有以下内容的UILabel: 大字符串(小字符串) 工作示例 到目前为止,我已经得出了以下结论: /* Set the Font Sizes */ UIFont *objectNameFont = [UIF
NSString
,它的第一个单词是一种字体大小/颜色,下一个单词是另一种字体大小和颜色
我在想可能是NSAttributedString
——但不确定这是不是正确的方法
我尝试将两个NSString
放入NSAttributedString
——但没有成功
正在查找具有以下内容的UILabel:
大字符串(小字符串)
工作示例
到目前为止,我已经得出了以下结论:
/* Set the Font Sizes */
UIFont *objectNameFont = [UIFont systemFontOfSize:14.f];
UIFont *itemsFont = [UIFont systemFontOfSize:12.f];
/* Create the attribute dictionaries */
NSDictionary *objectNameDict = [NSDictionary dictionaryWithObject:objectNameFont forKey:NSFontAttributeName];
NSDictionary *objectItemDict = [NSDictionary dictionaryWithObject:itemsFont forKey:NSFontAttributeName];
/* Create the string */
NSString *labelFullName = [NSString stringWithFormat:@"%@ (%@)", object.name, object.items];
/* Seperate the two strings */
NSArray *components = [labelFullName componentsSeparatedByString:@" ("];
NSRange objectNameRange = [labelFullName rangeOfString:[components objectAtIndex:0]];
NSRange objectItemRange = [labelFullName rangeOfString:[components objectAtIndex:1]];
/* Create the Attributed string */
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:labelFullName];
/* Start the editiong */
[attrString beginEditing];
[attrString addAttribute: NSForegroundColorAttributeName
value:[UIColor colorWithRed:0.667 green:0.667 blue:0.667 alpha:1] /*#aaaaaa*/
range:objectItemRange];
[attrString addAttribute: NSForegroundColorAttributeName
value:[UIColor colorWithRed:0.271 green:0.271 blue:0.271 alpha:1] /*#454545*/
range:objectNameRange];
[attrString addAttributes:objectNameDict range:objectNameRange];
[attrString addAttributes:objectItemDict range:objectItemRange];
[attrString endEditing];
cell.title.attributedText = attrString;
return cell;
这似乎是我所需要的。但是,两个字符串(”的分隔符是黑色的,我需要它与对象的颜色相同。此处设置的项目颜色:
[attrString addAttribute: NSForegroundColorAttributeName value:[UIColor colorWithRed:0.667 green:0.667 blue:0.667 alpha:1] /*#aaaaaa*/ range:objectItemRange];
找到解决方案
我找到了一个适合我的解决方案:
我取出字符串并将它们放入NSArray
中,然后使用objectAtIndex获取它们的NSRange
值来设置文本
有什么想法吗
谢谢大家。是的,你可以吃。
您还获得了该NSAttributedString
的正确类
以下代码中使用了两种字体大小12和15:
有关更多信息,请参见和。使用Attributedstring。谢谢。我正在创建一个NSString,如下所示:NSString*categoryString=[NSString stringWithFormat:@“%@(%@)”,object.text,object.text];我能用这一个nsstring并且仍然改变这两个词吗?你能看到我更新的问题吗?我就快到了。你的答案他帮了我很多。谢谢!
UIFont *font=[UIFont fontWithName:@"Arial" size:12.f];
NSDictionary *attrsDict=[NSDictionary dictionaryWithObject:font
forKey:NSFontAttributeName];
NSAttributedString *attribString=[[NSAttributedString alloc] initWithString:[words[0] substringFromIndex:1] attributes:attrsDict];
UIFont *fontFirst=[UIFont fontWithName:@"Arial" size:15.f];
NSDictionary *attrsDictFirst=[NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName];
NSAttributedString *firstString=[[NSAttributedString alloc] initWithString:[attribString subStringToIndex:1] attributes:attrsDictFirst];
[attribString replaceCharactersInRange:NSMakeRange(0, 1) withString:firstString];