可能有两种字体大小的NSString

时间:2014-03-28 11:25:42

标签: ios objective-c nsstring

是否可以让一个NSString的第一个字体为一个字体大小/颜色,下一个字体是另一个字体大小和颜色?

我想的可能是NSAttributedString - 但不确定这是否可行?

我尝试将两个NSString放入NSAttributedString - 但这不起作用。

寻找具有类似内容的UILabel:

  

LARGEString(smallString)

工作示例

到目前为止,我已经想出了这个:

        /* 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;

这似乎可行,因为我需要它。但是,两个字符串的分隔符“(”是黑色的,我需要它与object.items颜色相同的颜色,在此处设置:

[attrString addAttribute: NSForegroundColorAttributeName value:[UIColor colorWithRed:0.667 green:0.667 blue:0.667 alpha:1] /*#aaaaaa*/ range:objectItemRange];

解决方案

我找到了一个适合我的解决方案:

我将字符串拉出并将它们放入NSArray并使用objectAtIndex获取其NSRange值以设置文本 有任何想法吗?

谢谢大家。

1 个答案:

答案 0 :(得分:7)

是的,你可以拥有。

你也得到了NSAttributedString的正确班级。

在以下代码中,使用了两种字体大小12和15:

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];

更多相似的问题是herehere