xcode iOS - UITableViewCell 使文本加粗
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20853433/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iOS - UITableViewCell make text bold
提问by Vanessa
I have a string:
我有一个字符串:
NSString *userInfo = @"James Johnson @james";
What i want to do is bold James Johnson
and keep @james
normal font.
我想要做的是加粗James Johnson
并保持@james
正常字体。
So what I have tried is using NSAttributedString
but somewhere I'm doing something wrong in order to complete the process.
所以我尝试过的是使用NSAttributedString
但在某处我做错了为了完成这个过程。
This is what I have tried:
这是我尝试过的:
NSString *user = @"James Johnson @james";
UIFont *fontLight = [UIFont fontWithName:@"HelveticaNeue-Light" size:14];
UIFont *fontBold = [UIFont fontWithName:@"HelveticaNeue-Bold" size:14];
NSMutableAttributedString *string = [[NSMutableAttributedString alloc]initWithString:userInfo];
//TESTING WITH RANDOM PARTS OF THE STRIN
[string addAttribute:NSForegroundColorAttributeName value:fontLight range:NSMakeRange(0, 3)];
[string addAttribute:NSForegroundColorAttributeName value:fontBold range:NSMakeRange(3, 5)];
NSString *str = [string string];
cell.textLabel.text = str;
Is there a way I can make this work even if I'm on the wrong direction?
即使我走错了方向,有没有办法让我完成这项工作?
What's not working
什么不起作用
For some reason, the characters from range 0 - 3 is not being a light font...instead the entire cell.textLabel.text
is bold somehow and is not font size 14 which i had specified in the UIFont.
出于某种原因,0 - 3 范围内的字符不是浅色字体……而是整个cell.textLabel.text
字体以某种方式加粗,而不是我在 UIFont 中指定的字体大小 14。
回答by Florian Mielke
Your last part of it is wrong. You must create your NSAttributedString
and finally trash the formatting by using
你的最后一部分是错误的。您必须NSAttributedString
使用
NSString *str = [string string];
As NSString
doesn't know anything about formatting you have to use the NSAttributedString
to assign it to the cell's textLabel
:
由于NSString
对格式一无所知,您必须使用 将NSAttributedString
其分配给单元格的textLabel
:
cell.textLabel.attributedText = string;
回答by Bhumeshwer katre
You should set attributedString
to attributed
text
你应该设置attributedString
为attributed
文本
Comment these lines
注释这些行
//NSString *str = [string string];
//cell.textLabel.text = str;
And write this
并写下这个
cell.textLabel.attributedText = string;//Set NSMutableAttributedString only not NSString
回答by Bamsworld
UILabel has a property attributedText. You should be assigning this property with your attributed string and not use text property.
UILabel 有一个属性attributedText。您应该使用属性字符串分配此属性,而不是使用文本属性。
回答by Mrigraj
Make changes in you code as follows :
对您的代码进行如下更改:
- You will require to report all the characters with in the NSMutableAttributedString, so specify them with in the range, while adding an attribute.
- Provide correct attribute name, else you will have an exception here, in this case you should use "NSFontAttribteName" in place of "NSForegroundColorAttributeName".
- 您将需要报告 NSMutableAttributedString 中的所有字符,因此在添加属性时在范围内指定它们。
- 提供正确的属性名称,否则这里会有一个例外,在这种情况下,您应该使用“NSFontAttribteName”代替“NSForegroundColorAttributeName”。
NSString *user = @"James Johnson @james";
UIFont *fontLight = [UIFont fontWithName:@"HelveticaNeue-Light" size:14];
UIFont *fontBold = [UIFont fontWithName:@"HelveticaNeue-Bold" size:14];
NSMutableAttributedString *string = [[NSMutableAttributedString alloc]initWithString:user];
[string addAttribute:NSFontAttributeName value:fontLight range:NSMakeRange(0, 20)];
[string addAttribute:NSFontAttributeName value:fontBold range:NSMakeRange(0, 5)];
cell.textLabel.attributedText = string;