2014-01-14 38 views
0

我想在下面使用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; 
} 
+0

低於該看看我的回答coveres你對你的文本的一部分設置爲黑體和覆蓋改變基於數字小值的顏色的能力的所有問題。這是關於如何實現所有這些事情的完整解釋:)退房。祝你好運 – Pavan

回答

1

創建一個NSAttributedString並將其分配給cell.textLabel.attributedText。例如:

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; 
+0

只是爲了將來的參考,你可以創建一個動態的粗體長度,通過添加後綴' - '3個字符,像這樣:'NSString * begginingString = [NSString stringWithFormat:@「%@ - 」,sl.name] ;'''''''''''''''''''''''''''''''''''''''''''''''''''''' – Pavan

+0

@Pavan,謝謝。我更新了我的示例以結合您的建議。 – jonahb

+0

多數民衆贊成在罰款,它不是一個問題!通過協作可以產生更好的代碼。說到這一點,我喜歡你的分隔符變量命名!我留白了,想不出分隔符的名字,'delimiter'這個詞是不合適的詞語:D我現在在我的答案中實現了分隔符變量,它使得它更容易編輯。乾杯。 – Pavan

1

更新的代碼 - 爲粗體文字的一部分,並根據數值改變顏色

請d您發佈在計算器的問題之前,向我們展示了一些初步研究你過去試過的,以便我們可以相應地幫助你。

您可以使用NSAttributedString將文本的一部分設置爲粗體: //如果要加粗字符串的一部分,必須讓它知道要在哪裏開始和結束粗體,因此您需要要知道你的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]; 

現在開始玩RGB va構成顏色值的顏色。

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

這將確保每個顯示的單元格的textColor不同。

+0

這是行不通的,因爲海報只需要格式化字符串的一部分。 – jonahb

+0

@jonahb噢謝謝你概述。那麼他將需要使用NSAttributedString。現在將更新我的答案。 – Pavan

+0

代碼已更新,以允許格式化字符串的部分 – Pavan

1

一個NSAttributedString應該做的伎倆。

像:

NSString *name = @"SampleName"; 
    NSUInteger asl = 2; 
    NSString *t_string = [NSString stringWithFormat:@"%@ - %d", name , asl]; 

    NSUInteger t_nameLength = name.length; 
    NSUInteger t_aslLength = 1; 

    UIColor *t_color = nil; 
    switch (asl) { 
     case 1: 
      t_color = [UIColor redColor]; 
      break; 
     case 2: 
      t_color = [UIColor greenColor]; 
      break; 
     default: 
      t_color = [UIColor blackColor]; 
      break; 
    } 

    NSMutableAttributedString *t_attributedString = [[NSMutableAttributedString alloc] initWithString:t_string]; 
    [t_attributedString addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Arial" size:14.0] range:NSMakeRange(0, t_nameLength)]; 
    [t_attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(0, t_nameLength)]; 
    [t_attributedString addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Arial-BoldMT" size:14.0] range:NSMakeRange(t_nameLength + 3, t_aslLength)]; 
    [t_attributedString addAttribute:NSForegroundColorAttributeName value:t_color range:NSMakeRange(t_nameLength + 3, t_aslLength)]; 

    cell.textLabel.attributedText = t_attributedString; 
+0

您的代碼的作用是什麼?請不要只粘貼代碼,解釋你的代碼的作用,以便OP可以從中學習,而不是僅僅複製和粘貼,希望能夠運行。 – Pavan