美文网首页
开发日常

开发日常

作者: iOS_tree | 来源:发表于2023-04-18 19:44 被阅读0次

    设置导航栏返回键

    let navigationitom = UIBarButtonItem(image: UIImage(named: "main_navi_left"), style: .plain, target: self, action: #selector(didClickBackButton))
    self.navigationItem.leftBarButtonItem = navigationitom
    

    暗黑模式的判断

    UIUserInterfaceStyle userInterfaceStyle = [UITraitCollection currentTraitCollection].userInterfaceStyle;
    if(userInterfaceStyle == UIUserInterfaceStyleLight){
    
    }else{
    
    }
    
    - (void) traitCollectionDidChange:(UITraitCollection *)previousTraitCollection
    {
        [super traitCollectionDidChange:previousTraitCollection];
        UIUserInterfaceStyle userInterfaceStyle = previousTraitCollection.userInterfaceStyle;
        if(userInterfaceStyle == UIUserInterfaceStyleLight){
    
        }else{
    
        }
    }
    

    屏蔽暗黑模式
    plist文件设置

    <key>UIUserInterfaceStyle</key>
        <string>Light</string>
    

    把字符串“#ff00ff00”转换为UIColor对象,前面两位代表透明度,后面分别代表RGB颜色

    func colorFromHexString(_ hexString: String) -> UIColor? {
        // 去掉字符串中的空格和换行符
        let trimmedString = hexString.trimmingCharacters(in: .whitespacesAndNewlines)
        
        // 判断字符串是否以 # 符号开头
        guard trimmedString.hasPrefix("#") else {
            return nil
        }
        
        // 截取字符串中的透明度、红、绿、蓝四种颜色的数值
        let startIndex = trimmedString.index(trimmedString.startIndex, offsetBy: 1)
        let endIndex = trimmedString.index(startIndex, offsetBy: 2)
        let alphaIndex = trimmedString.index(startIndex, offsetBy: 2)
        let redIndex = trimmedString.index(alphaIndex, offsetBy: 2)
        let greenIndex = trimmedString.index(redIndex, offsetBy: 2)
        let blueIndex = trimmedString.index(greenIndex, offsetBy: 2)
        
        let alphaHex = String(trimmedString[startIndex..<endIndex])
        let redHex = String(trimmedString[alphaIndex..<redIndex])
        let greenHex = String(trimmedString[redIndex..<greenIndex])
        let blueHex = String(trimmedString[greenIndex..<blueIndex])
        
        // 将十六进制数值转换为 UIColor 对象
        if let alpha = UInt8(alphaHex, radix: 16),
           let red = UInt8(redHex, radix: 16),
           let green = UInt8(greenHex, radix: 16),
           let blue = UInt8(blueHex, radix: 16) {
            return UIColor(red: CGFloat(red) / 255.0, green: CGFloat(green) / 255.0, blue: CGFloat(blue) / 255.0, alpha: CGFloat(alpha) / 255.0)
        }
        
        return nil
    }
    

    oc代码

    + (UIColor *)colorFromHexString:(NSString *)hexString {
        // 去掉字符串中的空格和换行符
        NSString *trimmedString = [hexString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
        
        // 判断字符串是否以 # 符号开头
        if (![trimmedString hasPrefix:@"#"]) {
            return nil;
        }
        
        // 截取字符串中的透明度、红、绿、蓝四种颜色的数值
        NSRange alphaRange = NSMakeRange(1, 2);
        NSRange redRange = NSMakeRange(3, 2);
        NSRange greenRange = NSMakeRange(5, 2);
        NSRange blueRange = NSMakeRange(7, 2);
        
        NSString *alphaHex = [trimmedString substringWithRange:alphaRange];
        NSString *redHex = [trimmedString substringWithRange:redRange];
        NSString *greenHex = [trimmedString substringWithRange:greenRange];
        NSString *blueHex = [trimmedString substringWithRange:blueRange];
        
        // 将十六进制数值转换为 UIColor 对象
        unsigned int alpha, red, green, blue;
        NSScanner *scanner = [NSScanner scannerWithString:alphaHex];
        [scanner scanHexInt:&alpha];
        scanner = [NSScanner scannerWithString:redHex];
        [scanner scanHexInt:&red];
        scanner = [NSScanner scannerWithString:greenHex];
        [scanner scanHexInt:&green];
        scanner = [NSScanner scannerWithString:blueHex];
        [scanner scanHexInt:&blue];
        
        return [UIColor colorWithRed:(CGFloat)red / 255.0 green:(CGFloat)green / 255.0 blue:(CGFloat)blue / 255.0 alpha:(CGFloat)alpha / 255.0];
    }
    

    相关文章

      网友评论

          本文标题:开发日常

          本文链接:https://www.haomeiwen.com/subject/wzckjdtx.html