Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/107.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 新手需要帮助:粗体字体&;基于值的文本颜色_Ios_Fonts_Colors - Fatal编程技术网

Ios 新手需要帮助:粗体字体&;基于值的文本颜色

Ios 新手需要帮助:粗体字体&;基于值的文本颜色,ios,fonts,colors,Ios,Fonts,Colors,我想让sl.asl在下面加粗,并根据下面的数值给它上色。有什么想法吗 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; [date

我想让sl.asl在下面加粗,并根据下面的数值给它上色。有什么想法吗

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateStyle:NSDateFormatterShortStyle];

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}


// Configure the cell...
ServiceLevel *sl = (ServiceLevel *)[self.importedRows objectAtIndex:indexPath.row];
cell.textLabel.text = [NSString stringWithFormat:@"%@ - %@", sl.name , sl.asl];

return cell;
}
创建并将其分配给
单元格.textLabel.AttributeText
。例如:

NSDictionary *attributes = @{
    NSForegroundColorAttributeName:[UIColor redColor],
    NSFontAttributeName:[UIFont fontWithName:@"Helvetica-Bold" size:16]
};

NSString *delimiter = @" - ";
NSString *text = [NSString stringWithFormat:@"%@%@%@", sl.name, delimiter, sl.asl];
NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithString:text];
[attributedText addAttributes:attributes range:NSMakeRange(sl.name.length + delimiter.length, sl.asl.length)];

cell.textLabel.attributedText = attributedText;
创建并将其分配给
单元格.textLabel.AttributeText
。例如:

NSDictionary *attributes = @{
    NSForegroundColorAttributeName:[UIColor redColor],
    NSFontAttributeName:[UIFont fontWithName:@"Helvetica-Bold" size:16]
};

NSString *delimiter = @" - ";
NSString *text = [NSString stringWithFormat:@"%@%@%@", sl.name, delimiter, sl.asl];
NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithString:text];
[attributedText addAttributes:attributes range:NSMakeRange(sl.name.length + delimiter.length, sl.asl.length)];

cell.textLabel.attributedText = attributedText;
创建并将其分配给
单元格.textLabel.AttributeText
。例如:

NSDictionary *attributes = @{
    NSForegroundColorAttributeName:[UIColor redColor],
    NSFontAttributeName:[UIFont fontWithName:@"Helvetica-Bold" size:16]
};

NSString *delimiter = @" - ";
NSString *text = [NSString stringWithFormat:@"%@%@%@", sl.name, delimiter, sl.asl];
NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithString:text];
[attributedText addAttributes:attributes range:NSMakeRange(sl.name.length + delimiter.length, sl.asl.length)];

cell.textLabel.attributedText = attributedText;
创建并将其分配给
单元格.textLabel.AttributeText
。例如:

NSDictionary *attributes = @{
    NSForegroundColorAttributeName:[UIColor redColor],
    NSFontAttributeName:[UIFont fontWithName:@"Helvetica-Bold" size:16]
};

NSString *delimiter = @" - ";
NSString *text = [NSString stringWithFormat:@"%@%@%@", sl.name, delimiter, sl.asl];
NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithString:text];
[attributedText addAttributes:attributes range:NSMakeRange(sl.name.length + delimiter.length, sl.asl.length)];

cell.textLabel.attributedText = attributedText;

更新的代码-用于加粗文本的一部分并根据数值更改颜色

在您发布有关stackoverflow的问题之前,请进行一些初步研究,并向我们展示您过去的尝试,以便我们能够相应地帮助您

您可以使用NSAttributed字符串将部分文本设置为粗体: //要加粗字符串的一部分,必须让它知道加粗的起始位置和结束位置,因此需要知道
sl.asl
变量的长度

NSMutableAttributedString * attributedAsl = [[NSMutableAttributedString alloc] initWithAttributedString:sl.asl];

NSString *delimiter = @" - "; //This is what will be written to separate sl.name and sl.asl 
NSString *begginingString = [NSString stringWithFormat:@"%@%@", delimiter, sl.name];
//What we dont want bolded
NSInteger startBoldFromEndOfBeginningString = [begginingString length]; 
//The length of text we want bolded - your sl.asl variable
NSInteger slAslLength = ((NSString)sl.asl).length;


UIFont *font= [UIFont fontWithName:@"Helvetica-Bold" size:25.0f];
[attributedAsl addAttribute:NSFontAttributeName value:font range:NSMakeRange(startBoldFromEndOfBeginningString, slAslLength)];

cell.textLabel.attributedText = attributedAsl;
然后你可以像这样设置文本颜色

cell.textLabel.textColor = [UIColor redColor];
现在,如果您想根据某个内容的数值更改textColor,则必须开始使用组成颜色值的RGB值

[UIColor colorWithRed:1.0 green:0.0 blue:1.0 alpha:1.0];
要确保颜色均匀分布到所需的颜色范围,有助于了解该数字的最高值

假设您在tableView中显示英语字母表,单元格总数为26,并且您要更改显示的每个单元格的文本颜色,一种解决方案是:

CGFloat maxNumber = 26; //total number of letters
CGFloat dynamicColor = 1.0/maxNumber * indexPath.row;

[UIColor colorWithRed:dynamicColor green:0.0 blue:1.0 alpha:1.0];

这将确保显示的每个单元格的每个文本颜色都不同。

更新的代码-用于加粗文本的一部分并根据数值更改颜色

在您发布有关stackoverflow的问题之前,请进行一些初步研究,并向我们展示您过去的尝试,以便我们能够相应地帮助您

您可以使用NSAttributed字符串将部分文本设置为粗体: //要加粗字符串的一部分,必须让它知道加粗的起始位置和结束位置,因此需要知道
sl.asl
变量的长度

NSMutableAttributedString * attributedAsl = [[NSMutableAttributedString alloc] initWithAttributedString:sl.asl];

NSString *delimiter = @" - "; //This is what will be written to separate sl.name and sl.asl 
NSString *begginingString = [NSString stringWithFormat:@"%@%@", delimiter, sl.name];
//What we dont want bolded
NSInteger startBoldFromEndOfBeginningString = [begginingString length]; 
//The length of text we want bolded - your sl.asl variable
NSInteger slAslLength = ((NSString)sl.asl).length;


UIFont *font= [UIFont fontWithName:@"Helvetica-Bold" size:25.0f];
[attributedAsl addAttribute:NSFontAttributeName value:font range:NSMakeRange(startBoldFromEndOfBeginningString, slAslLength)];

cell.textLabel.attributedText = attributedAsl;
然后你可以像这样设置文本颜色

cell.textLabel.textColor = [UIColor redColor];
现在,如果您想根据某个内容的数值更改textColor,则必须开始使用组成颜色值的RGB值

[UIColor colorWithRed:1.0 green:0.0 blue:1.0 alpha:1.0];
要确保颜色均匀分布到所需的颜色范围,有助于了解该数字的最高值

假设您在tableView中显示英语字母表,单元格总数为26,并且您要更改显示的每个单元格的文本颜色,一种解决方案是:

CGFloat maxNumber = 26; //total number of letters
CGFloat dynamicColor = 1.0/maxNumber * indexPath.row;

[UIColor colorWithRed:dynamicColor green:0.0 blue:1.0 alpha:1.0];

这将确保显示的每个单元格的每个文本颜色都不同。

更新的代码-用于加粗文本的一部分并根据数值更改颜色

在您发布有关stackoverflow的问题之前,请进行一些初步研究,并向我们展示您过去的尝试,以便我们能够相应地帮助您

您可以使用NSAttributed字符串将部分文本设置为粗体: //要加粗字符串的一部分,必须让它知道加粗的起始位置和结束位置,因此需要知道
sl.asl
变量的长度

NSMutableAttributedString * attributedAsl = [[NSMutableAttributedString alloc] initWithAttributedString:sl.asl];

NSString *delimiter = @" - "; //This is what will be written to separate sl.name and sl.asl 
NSString *begginingString = [NSString stringWithFormat:@"%@%@", delimiter, sl.name];
//What we dont want bolded
NSInteger startBoldFromEndOfBeginningString = [begginingString length]; 
//The length of text we want bolded - your sl.asl variable
NSInteger slAslLength = ((NSString)sl.asl).length;


UIFont *font= [UIFont fontWithName:@"Helvetica-Bold" size:25.0f];
[attributedAsl addAttribute:NSFontAttributeName value:font range:NSMakeRange(startBoldFromEndOfBeginningString, slAslLength)];

cell.textLabel.attributedText = attributedAsl;
然后你可以像这样设置文本颜色

cell.textLabel.textColor = [UIColor redColor];
现在,如果您想根据某个内容的数值更改textColor,则必须开始使用组成颜色值的RGB值

[UIColor colorWithRed:1.0 green:0.0 blue:1.0 alpha:1.0];
要确保颜色均匀分布到所需的颜色范围,有助于了解该数字的最高值

假设您在tableView中显示英语字母表,单元格总数为26,并且您要更改显示的每个单元格的文本颜色,一种解决方案是:

CGFloat maxNumber = 26; //total number of letters
CGFloat dynamicColor = 1.0/maxNumber * indexPath.row;

[UIColor colorWithRed:dynamicColor green:0.0 blue:1.0 alpha:1.0];

这将确保显示的每个单元格的每个文本颜色都不同。

更新的代码-用于加粗文本的一部分并根据数值更改颜色

在您发布有关stackoverflow的问题之前,请进行一些初步研究,并向我们展示您过去的尝试,以便我们能够相应地帮助您

您可以使用NSAttributed字符串将部分文本设置为粗体: //要加粗字符串的一部分,必须让它知道加粗的起始位置和结束位置,因此需要知道
sl.asl
变量的长度

NSMutableAttributedString * attributedAsl = [[NSMutableAttributedString alloc] initWithAttributedString:sl.asl];

NSString *delimiter = @" - "; //This is what will be written to separate sl.name and sl.asl 
NSString *begginingString = [NSString stringWithFormat:@"%@%@", delimiter, sl.name];
//What we dont want bolded
NSInteger startBoldFromEndOfBeginningString = [begginingString length]; 
//The length of text we want bolded - your sl.asl variable
NSInteger slAslLength = ((NSString)sl.asl).length;


UIFont *font= [UIFont fontWithName:@"Helvetica-Bold" size:25.0f];
[attributedAsl addAttribute:NSFontAttributeName value:font range:NSMakeRange(startBoldFromEndOfBeginningString, slAslLength)];

cell.textLabel.attributedText = attributedAsl;
然后你可以像这样设置文本颜色

cell.textLabel.textColor = [UIColor redColor];
现在,如果您想根据某个内容的数值更改textColor,则必须开始使用组成颜色值的RGB值

[UIColor colorWithRed:1.0 green:0.0 blue:1.0 alpha:1.0];
要确保颜色均匀分布到所需的颜色范围,有助于了解该数字的最高值

假设您在tableView中显示英语字母表,单元格总数为26,并且您要更改显示的每个单元格的文本颜色,一种解决方案是:

CGFloat maxNumber = 26; //total number of letters
CGFloat dynamicColor = 1.0/maxNumber * indexPath.row;

[UIColor colorWithRed:dynamicColor green:0.0 blue:1.0 alpha:1.0];

这将确保显示的每个单元格的每个文本颜色都不同。

NSAttributed字符串应该起作用

比如:


NSAttributedString应该可以做到这一点

比如:


NSAttributedString应该可以做到这一点

比如:


NSAttributedString应该可以做到这一点

比如:


这是行不通的,因为海报只需要格式化字符串的一部分。@jonahb噢,谢谢你概述了这一点。他将需要使用NSAttributedString。“现在将更新我的答案。代码已更新,以允许对字符串的某些部分进行格式化,但这些部分不起作用,因为poster只希望格式化字符串的一部分。@jon”