zoukankan      html  css  js  c++  java
  • 通过#色值方式设置颜色

     1 + (UIColor *)colorWithRGB:(NSString *)rgbstr{
     2     NSString *newrgbstr = [rgbstr uppercaseString];
     3     NSInteger strlen = newrgbstr.length;
     4     UIColor *color = nil;
     5     unichar first = [newrgbstr characterAtIndex:0];
     6     if (first == '#') {
     7         for (int i = 1; i < strlen; i++) {
     8             unichar u = [newrgbstr characterAtIndex:i];
     9             if (!(u >= '0' && u <= '9') && !(u >= 'A' && u <='F')) {
    10                 NSString *exName = [NSString stringWithFormat:@"使用%@类获取指定颜色错误", NSStringFromClass([self class])];
    11                 NSException *e = [NSException exceptionWithName:exName reason:@"请检查颜色数值越界" userInfo:nil];
    12                 @throw e;
    13             }
    14         }
    15         if (newrgbstr.length == 4) {
    16             unichar uarray[3];
    17             for(int i = 0;i < 3;i++){
    18                 uarray[i] = [newrgbstr characterAtIndex:(i+1)];
    19             }
    20             for (int i = 0;i < 3;i++) {
    21                 uarray[i] = (uarray[i] > '9' ? (uarray[i]-'A'+10) : (uarray[i]-'0')) * 17 % 256;
    22             }
    23             CGFloat r = uarray[0]/255.0f;
    24             CGFloat g = uarray[1]/255.0f;
    25             CGFloat b = uarray[2]/255.0f;
    26             color = [UIColor colorWithRed:r green:g blue:b alpha:1];
    27         }else if(newrgbstr.length == 7){
    28             unichar uarray[6];
    29             for(int i = 0;i < 6;i++){
    30                 uarray[i] = [newrgbstr characterAtIndex:(i+1)];
    31             }
    32             for (int i = 0;i < 3;i++) {
    33                 unichar l = uarray[2*i];
    34                 unichar r = uarray[2*i+1];
    35                 uarray[i] = ((l > '9' ? (l-'A'+10) : (l-'0')) * 16 +
    36                              (r > '9' ? (r-'A'+10) : (r-'0'))) % 256;
    37             }
    38             CGFloat r = uarray[0]/255.0f;
    39             CGFloat g = uarray[1]/255.0f;
    40             CGFloat b = uarray[2]/255.0f;
    41             color = [UIColor colorWithRed:r green:g blue:b alpha:1];
    42         }
    43     }
    44     return color;
    45 }

    可以通过#fff和#ffffff两种方式设置色值,和设计协调更方便

  • 相关阅读:
    在 Borland C++ 及 Visual C++ 环境中使用 STLport (作者:孟岩)
    设置JavaFX-CSS改变TreeView节点图标
    Using MS DataGrid control with ADO
    两个加载fxml文件的方法
    JavaFX中ObservableValue类型
    在 Eclipse 下利用 gradle 构建系统
    JavaFX初探
    深度剖析如何保证缓存与数据库的一致性
    ACID的实现原理
    一颗高度为3的B+树能存多少行数据?
  • 原文地址:https://www.cnblogs.com/gpengf/p/5209380.html
Copyright © 2011-2022 走看看