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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 04:29:13  来源:igfitidea点击:

iOS - UITableViewCell make text bold

iosxcodeuitableviewxcode5

提问by Vanessa

I have a string:

我有一个字符串:

NSString *userInfo = @"James Johnson @james";

What i want to do is bold James Johnsonand keep @jamesnormal font.

我想要做的是加粗James Johnson并保持@james正常字体。

So what I have tried is using NSAttributedStringbut 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.textis 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 NSAttributedStringand finally trash the formatting by using

你的最后一部分是错误的。您必须NSAttributedString使用

NSString *str = [string string];

As NSStringdoesn't know anything about formatting you have to use the NSAttributedStringto assign it to the cell's textLabel:

由于NSString对格式一无所知,您必须使用 将NSAttributedString其分配给单元格的textLabel

cell.textLabel.attributedText = string;

回答by Bhumeshwer katre

You should set attributedStringto attributedtext

你应该设置attributedStringattributed文本

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 :

对您的代码进行如下更改:

  1. You will require to report all the characters with in the NSMutableAttributedString, so specify them with in the range, while adding an attribute.
  2. Provide correct attribute name, else you will have an exception here, in this case you should use "NSFontAttribteName" in place of "NSForegroundColorAttributeName".
  1. 您将需要报告 NSMutableAttributedString 中的所有字符,因此在添加属性时在范围内指定它们。
  2. 提供正确的属性名称,否则这里会有一个例外,在这种情况下,您应该使用“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;