【问题标题】:How to set RGB color from database如何从数据库中设置 RGB 颜色
【发布时间】:2013-05-04 18:50:31
【问题描述】:

我是 iphone 开发新手。我想从数据库中设置 uilabel 的背景颜色。我已经将 RGB 颜色存储到数据库中。但是我没有得到背景颜色。

const char *dbpath = [database_Path UTF8String];
sqlite3_stmt    *statement;
[category_array removeAllObjects];
[category_arrDictionary removeAllObjects];
if (sqlite3_open(dbpath, &Payer_DB) == SQLITE_OK)
{
    NSString *querySQL = [NSString stringWithFormat:@"SELECT * FROM Expense_Category"];

    const char *query_stmt = [querySQL UTF8String];

    if (sqlite3_prepare_v2(Payer_DB,query_stmt, -1, &statement, NULL) == SQLITE_OK)
    {
        while (sqlite3_step(statement) == SQLITE_ROW)
        {
            //              NSLog(@"%i",SQLITE_ROW);
            NSMutableString *Category_Id = [[NSMutableString alloc]initWithUTF8String:(const char *) sqlite3_column_text(statement, 0)];
            NSMutableString *Category_Name = [[NSMutableString alloc]initWithUTF8String:(const char *) sqlite3_column_text(statement, 1)];
            NSMutableString *Category_Image = [[NSMutableString alloc]initWithUTF8String:(const char *) sqlite3_column_text(statement, 2)];
            NSMutableString *Category_Color = [[NSMutableString alloc]initWithUTF8String:(const char *) sqlite3_column_text(statement, 3)];
            NSLog(@"%@",Category_Color);

            [category_arrDictionary setObject:[NSMutableArray arrayWithObjects:Category_Name,Category_Image,Category_Color,nil] forKey:Category_Id];
            [category_array addObject:Category_Id];
        }
    }
    else {
        NSLog(@"Value updated");
    }
    sqlite3_finalize(statement);
    sqlite3_close(Payer_DB);
}

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath  *)indexPath 
{
    UILabel *lblCatColor = [[UILabel alloc] initWithFrame:CGRectMake(230,10, 70, 40)];
    [lblCatColor setBackgroundColor:[[category_arrDictionary valueForKey:[category_array objectAtIndex:indexPath.row]] objectAtIndex:3]];
    //lblCatColor.text = [[category_arrDictionary valueForKey:[category_array objectAtIndex:indexPath.row]] objectAtIndex:3];
    [cell.contentView addSubview:lblCatColor];

    return cell;
}

【问题讨论】:

  • 如何存储 RGB 值?表示采用哪种格式。
  • 199.0,21.0,133.0 这是格式
  • 您在 category_arrDictionary 中得到了什么?
  • NSLog("%@",[[category_arrDictionary valueForKey:[category_array objectAtIndex:indexPath.row]] objectAtIndex:3]);打印哪个值?
  • 我正在获取行程名称和颜色..我可以在标签中显示行程名称..但我不知道显示 uilabel 的背景颜色。

标签: iphone ios ipad ios5 uilabel


【解决方案1】:

您需要编写一个小方法来解析您从数据库中提取的字符串并从中创建一个UIColor。您不能直接使用字符串设置标签的颜色。

【讨论】:

  • 但是,UIColor 符合 NSCoding,因此您应该能够将其存储为 BLOB。
【解决方案2】:

请尝试使用这个。 你的字典会给你一个字符串,然后你应该分离你的组件,然后把你的红色、绿色和蓝色值放在红色、绿色和蓝色变量的位置。

[lblCatColor setBackgroundColor:[UIColor colorWithRed:red/255.0f green:green/255.0f blue:blue/255.0f alpha:1.0f]];

【讨论】:

    【解决方案3】:

    试试这个

    NSString *strflg=[[category_arrDictionary valueForKey:[category_array objectAtIndex:indexPath.row]] objectAtIndex:3]];
    
    NSArray *colorArray=[strflg componentsSeparatedByString:@","];
    float red;
    float green;
    float blue;
    
    for (int j=0;j<[colorArray count]; j++) {
        if (j==0) {
            red=[[colorArray objectAtIndex:j] floatValue];
        }
        else if (j==1)
        {
            green=[[colorArray objectAtIndex:j] floatValue];
        }
        else{
            blue=[[colorArray objectAtIndex:j] floatValue];
        }
    }
    
    [lblCatColor setBackgroundColor:[UIColor colorWithRed:red/255.0f green:green/255.0f blue:blue/255.0f alpha:1.0]];
    

    【讨论】:

      【解决方案4】:

      它可以帮助你

      NSString *strflg=[[category_arrDictionary valueForKey:[category_array objectAtIndex:indexPath.row]] objectAtIndex:3]];
      
      NSArray *colorArray=[strflg componentsSeparatedByString:@","];
      
      [lblCatColor setBackgroundColor:[UIColor colorWithRed:[[colorArray objectAtIndex:0] floatValue]/255.0f green:[[colorArray objectAtIndex:1] floatValue]/255.0f blue:[[colorArray objectAtIndex:2] floatValue]/255.0f alpha:1.0]];
      

      【讨论】:

        【解决方案5】:
          [[category_arrDictionary valueForKey:[category_array objectAtIndex:indexPath.row]] objectAtIndex:3] returns NSString like 199.0,21.0,133.0.
        

        以下代码将帮助您...

         NSString *colorFormat  = [[category_arrDictionary valueForKey:[category_array      objectAtIndex:indexPath.row]] objectAtIndex:3];
         NSArray *colorArr      = [[colorFormat componentsSeparatedByString:@","];
         float red              = [[colorArr objectAtIndex:0]floatValue];
         float green            = [[colorArr objectAtIndex:1]floatValue];
         float blue         = [[colorArr objectAtIndex:2]floatValue];
        
         [lblCatColor setBackgroundColor:[UIColor colorWithRed:(red/255.0) green:(green/255.0) blue:(blue/255.0) alpha:1.0]];
        

        【讨论】:

          【解决方案6】:

          使用以下代码 sn-p 从NSString 创建UIColor

          NSString *rgbstr = @"123.0,104.0,238.0";
          NSArray *arrRGB = [rgbstr componentsSeparatedByString:@","];
          UIColor *color = [UIColor colorWithRed:[arrRGB[0] floatValue]/255.0f green:[arrRGB[1] floatValue]/255.0f blue:[arrRGB[2] floatValue]/255.0f alpha:1.0f];
          

          【讨论】:

            猜你喜欢
            • 2015-08-28
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2023-03-18
            • 2016-07-12
            相关资源
            最近更新 更多