zoukankan      html  css  js  c++  java
  • Swift 由十六进制转为RGB的颜色设置

    如果美工给你的颜色值或者后台取到的颜色值为十六进制时,如:

    #3492e9

     

    UIColor中没有直接以十六进制色值数为参数的方法,如下可以转为RGB,alpha也可以设定:

     

     1 func colorConversion (Color_Value:NSString, alpha: CGFloat)->UIColor{
     2         var  Str :NSString = Color_Value.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet()).uppercaseString
     3         if Color_Value.hasPrefix("#"){
     4             Str=(Color_Value as NSString).substringFromIndex(1)
     5         }
     6         let redStr = (Str as NSString ).substringToIndex(2)
     7         let greenStr = ((Str as NSString).substringFromIndex(2) as NSString).substringToIndex(2)
     8         let blueStr = ((Str as NSString).substringFromIndex(4) as NSString).substringToIndex(2)
     9         var r:CUnsignedInt = 0, g:CUnsignedInt = 0, b:CUnsignedInt = 0;
    10         NSScanner(string:redStr).scanHexInt(&r)
    11         NSScanner(string: greenStr).scanHexInt(&g)
    12         NSScanner(string: blueStr).scanHexInt(&b)
    13         return UIColor(red: CGFloat(r)/255.0, green: CGFloat(g)/255.0, blue: CGFloat(b)/255.0, alpha: alpha)
    14     }

    然后调用这个方法来设置颜色就ok了:

    self.backgroundColor = colorConversion("#3492e9", alpha: 1.0)
  • 相关阅读:
    Ionic移动应用框架
    JavaScript为iphone添加到主屏幕
    移动前端:在手机上隐藏地址工具栏
    Swipe.js支持网页点击和数字导航
    String类的概述和常用方法
    今天的学习
    集合的区别
    面向对象总结
    面向对象的封装
    方法的多态中的重写
  • 原文地址:https://www.cnblogs.com/Walking-Jin/p/6398425.html
Copyright © 2011-2022 走看看