
我当时想的可能是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值以设置文本
有任何想法吗?
谢谢大家.
解决方法 是的你可以拥有.你也得到了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];
更多,类似的问题是here和here.
总结以上是内存溢出为你收集整理的ios – 可能有两种字体大小的NSString全部内容,希望文章能够帮你解决ios – 可能有两种字体大小的NSString所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)