UILabel 优于 UIProgressView,具有多种颜色

2023-12-20

所以我还没有这样做,我正在尝试弄清楚如何做到这一点。所以我制作了一个 UITableView,每个单元格都有一个关联的 NSTimer。现在,在每个自定义 UITableViewCell 中,我都有一个 UIProgressView 作为背景,拉伸以填充单元格。现在我想将带有剩余时间的 UILabel 添加到 UIProgressView 上。但由于进度条填充颜色和背景颜色显着不同(海军蓝色进度填充和白色背景/未填充区域),我想知道如何在进度条填充时动态更改文本颜色。与 UILabel 的海军蓝色填充部分一样,文本颜色应为白色。白底的部分,文字应该是黑色的。类似这样的东西,但是在 Objective-c 中。 https://stackoverflow.com/questions/17987469/how-to-change-the-color-of-the-text-of-a-qprogressbar-with-its-value


刚刚为你解决了这个问题:)

结果

这是 ZWProgressView 的模拟器结果:

视图控制器文件

这是一个用法示例:

#import "ViewController.h"
#import "ZWProgressView.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    ZWProgressView *progressView = [[ZWProgressView alloc] init];
    progressView.frame = CGRectMake((self.view.bounds.size.width - 200) / 2.0, self.view.bounds.size.height / 2.0 - 25.0, 200, 50);

    progressView.progress = 0.47;

    [self.view addSubview:progressView];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

ZWProgressView 类

注意:屏蔽代码取自:

只需用矩形掩盖 UIView https://stackoverflow.com/questions/11391058/simply-mask-a-uiview-with-a-rectangle

头文件

#import <UIKit/UIKit.h>

@interface ZWProgressView : UIView

@property (nonatomic, assign) CGFloat progress;
@property (nonatomic, strong) UIColor *normalTextColor;
@property (nonatomic, strong) UIColor *maskedTextColor;

@property (nonatomic, strong) UIView *container;
@property (nonatomic, strong) UIView *progressBar;
@property (nonatomic, strong) UILabel *progressLabel;
@property (nonatomic, strong) UILabel *maskedProgressLabel;
@property (nonatomic, strong) UIView *mask;

@end

实施文件

#import "ZWProgressView.h"

@interface ZWProgressView()
{
    NSLayoutConstraint *progressBarWidthConstraint;
    NSLayoutConstraint *progressBarMaskWidthConstraint;
}

@end

@implementation ZWProgressView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code

        self.frame = frame;

        [self initView];
        [self addAllConstraints];
    }
    return self;
}

-(void)initView
{
    self.layer.cornerRadius = 2.0;
    self.backgroundColor = [UIColor colorWithRed:0.85 green:0.85 blue:0.85 alpha:1.0];

    self.normalTextColor = [UIColor colorWithRed:0.2 green:0.2 blue:0.2 alpha:1.0];
    self.maskedTextColor = [UIColor whiteColor];

    self.container = [[UIView alloc] init];
    self.container.layer.borderWidth = 1.0;
    self.container.layer.borderColor = [UIColor grayColor].CGColor;
    self.container.backgroundColor = [UIColor whiteColor];
    self.container.layer.cornerRadius = 3.0;
    self.container.clipsToBounds = YES;

    self.progressBar = [[UIView alloc] init];
    self.progressBar.backgroundColor = [UIColor colorWithRed:0.2 green:0.3 blue:0.8 alpha:1.0];

    self.progressLabel = [[UILabel alloc] init];
    self.progressLabel.font = [UIFont fontWithName:@"Arial-BoldMT" size:30];
    self.progressLabel.textAlignment = NSTextAlignmentCenter;
    self.progressLabel.textColor = self.normalTextColor;
    self.progressLabel.clipsToBounds = YES;

    self.maskedProgressLabel = [[UILabel alloc] init];
    self.maskedProgressLabel.font = self.progressLabel.font;
    self.maskedProgressLabel.textAlignment = self.progressLabel.textAlignment;
    self.maskedProgressLabel.textColor = self.maskedTextColor;
    self.maskedProgressLabel.clipsToBounds = YES;

    self.mask = [[UIView alloc] init];

    [self.container addSubview:self.progressBar];
    [self.container addSubview:self.progressLabel];
    [self.container addSubview:self.maskedProgressLabel];
    [self.container addSubview:self.mask];

    [self addSubview:self.container];
}

-(void)addAllConstraints
{
    self.container.translatesAutoresizingMaskIntoConstraints = NO;
    self.progressBar.translatesAutoresizingMaskIntoConstraints = NO;
    self.progressLabel.translatesAutoresizingMaskIntoConstraints = NO;
    self.maskedProgressLabel.translatesAutoresizingMaskIntoConstraints = NO;
    self.mask.translatesAutoresizingMaskIntoConstraints = NO;

    id views = @{@"container": self.container, @"progressBar": self.progressBar, @"progressLabel": self.progressLabel, @"maskedProgressLabel": self.maskedProgressLabel, @"mask": self.mask};

    // container constraint

    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-5-[container]-5-|" options:0 metrics:nil views:views]];
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-5-[container]-5-|" options:0 metrics:nil views:views]];

    // progressBar constraint

    [self.container addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[progressBar]" options:0 metrics:nil views:views]];

    progressBarWidthConstraint = [NSLayoutConstraint constraintWithItem:self.progressBar attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeWidth multiplier:1.0 constant:0];

    [self.container addConstraint:progressBarWidthConstraint];

    [self.container addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[progressBar]|" options:0 metrics:nil views:views]];

    // progressLabel constraint

    [self.container addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[progressLabel]|" options:0 metrics:nil views:views]];
    [self.container addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[progressLabel]|" options:0 metrics:nil views:views]];

    // maskedProgressLabel constraint
    [self.container addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[maskedProgressLabel]|" options:0 metrics:nil views:views]];
    [self.container addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[maskedProgressLabel]|" options:0 metrics:nil views:views]];

    // mask constraint
    [self.container addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[mask]" options:0 metrics:nil views:views]];
    [self.container addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[mask]|" options:0 metrics:nil views:views]];

    progressBarMaskWidthConstraint = [NSLayoutConstraint constraintWithItem:self.mask attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeWidth multiplier:1.0 constant:0];

    [self.container addConstraint:progressBarMaskWidthConstraint];
}

-(void)setProgress:(CGFloat)progress
{
    int percentage = progress * 100;

    NSString *strProgress = [[NSString alloc] initWithFormat:@"%d%%", percentage];

    self.progressLabel.text = strProgress;
    self.maskedProgressLabel.text = strProgress;

    // ------------------------------------------------------------------
    // subtracting 10 pixel for the |-5-[progressBar]-5-| padding in
    // the constraint for the progresBar
    // ------------------------------------------------------------------
    progressBarWidthConstraint.constant = progress * (self.bounds.size.width - 10.0);
    progressBarMaskWidthConstraint.constant = progressBarWidthConstraint.constant;

    [self layoutIfNeeded];

    [self updateMask];
}

-(void)updateMask
{
    // ------------------------------------------------------------------------
    // Masking code taken from:
    //
    // https://stackoverflow.com/questions/11391058/simply-mask-a-uiview-with-a-rectangle
    // ------------------------------------------------------------------------

    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    CGRect maskRect = CGRectMake(0, 0, progressBarMaskWidthConstraint.constant, self.mask.bounds.size.height);

    CGPathRef path = CGPathCreateWithRect(maskRect, NULL);

    maskLayer.path = path;

    CGPathRelease(path);

    self.maskedProgressLabel.layer.mask = maskLayer;
}

@end
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

UILabel 优于 UIProgressView,具有多种颜色 的相关文章

  • iOS 上的 UIBezierPath 操作

    我从一条直线开始 我希望用户能够触摸并拖动该线 使其弯曲 实际上 他们有能力将线条操纵成波浪形状 我不确定从技术上实现这一目标的最简单方法 我首先创建了三次曲线的 UIBezierPaths 数组 目的是操纵控制点 但似乎一旦绘制了 UIB
  • AVCaptureSession 具有多个方向问题

    我正在尝试实现条形码扫描仪 我有一个 AVCaptureSession 它从 AVCaptureDevice 接收视频 我想支持所有方向 使用以下代码 当我运行应用程序时 纵向一切正常 然而 在横向方向上 视图会旋转 但视频输入不会旋转 所
  • 在 iOS 上使用 Web 服务的最佳方式?

    我想构建一个 iOS 应用程序 让您登录到网络服务 之后 应用程序将 当用户选择时 通过 https 发送登录名 密码以及请求的变量 例如 在请求 新闻更新 后 它将收到 XML 格式的请求信息 类似于
  • UIDocumentInteractionController 阻止“打开方式”表中的空投

    在我的应用程序中 我允许用户通过 Instagram 分享照片 这需要使用 UIDocumentInteractionController 如果手机支持 则会自动检测空投 如何将其从 打开方式 操作表中删除 即使我使用 UIActivity
  • Objective-c 中的块递归

    当执行涉及 Objective C 块的递归时 我在 iOS 应用程序中收到 EXC BAD ACCESS 信号 这是简化的代码 void problematicMethod FriendInfo friendInfo onComplete
  • iPhone UINavigationBar 使用 [UINavigationBar 外观] 更改所有控制器的字体样式

    我知道我可以单独更改导航栏的字体 如本答案所述 更改导航栏的字体 https stackoverflow com questions 5832036 change the navigation bars font 目前我正在使用一种更全局的
  • 在iOS上,“添加到主页”缓存保存在哪里,如何清除它?

    我正在 iPad iOS v7 上制作一个 html5 游戏 当我将其添加到主页时 它非常顽固地释放缓存 如果我在 Safari 中查看它 这会按照您所期望的方式工作 如果我刷新一次或两次 页面就会以最新状态缓存 但在主页上却是另一回事 它
  • iOS 中的构建对象文件扩展名是什么?

    当我在项目中构建java对象类时 将创建带有 class扩展名的构建文件 并且人类不可读 快速构建文件怎么样 example car java gt build gt car class 构建后会是什么 car swift gt build
  • 个人帐户开发者之间的 Apple 开发/分发证书

    我一直在到处寻找有关处理证书的正确答案 想象一下以下帐户 Joe拥有个人 Apple 帐户 但他根本不会编码 他只是发布了该应用程序并将其称为自己的 Bob还有一个个人 Apple 帐户 Bob 是一位编码专家 Joe 付费让他开发他的第一
  • SiriKit 错误:此应用程序不支持捐赠意图

    我在 Xcode 10 iOS 12 Beta 中捐赠自定义意图时遇到问题 我创建了一个在我的主应用程序目标和 OrderIntent 目标之间共享的自定义框架 我创建了一个 intentdefinition 文件 并将目标成员资格设置为我
  • 架构armv7的重复符号

    尝试在我现有的应用程序中使用 Layar SDK 时出现以下错误 我该如何解决这个问题 Ld Users pnawale Library Developer Xcode DerivedData hub afxxzaqisdfliwbzxbi
  • 我怎样才能勾勒出文本字体?

    我想在边框 轮廓 中显示另一种颜色的文本 我正在尝试使用在 MapOverlayView 中显示文本 text drawAtPoint CGPointMake 0 30 withFont UIFont fontWithName Helvet
  • 来自 iPhone/iPad 的 json Web 服务

    有人可以帮助我解决如何从 iphone 或 ipad 使用 json Web 服务的问题吗 这里我的要求是使用 API 密钥实现 json webservice 如果可能的话发布一些教程或示例链接 谢谢 规范的 JSON 处理库是here
  • 在 UISearchController 文本字段中输入内容时导航栏消失

    我试图找出为什么当我开始在 UISearchController searchBar 中输入时我的整个导航栏消失 它正确加载并正确动画 但是当我开始输入时我丢失了活动的导航栏 下面是从 viewDidLoad 加载 searchContro
  • NVActivityIndi​​catorView 仅适用于特定视图

    我正在使用这个库https github com ninjaprox NVActivityIndi catorView https github com ninjaprox NVActivityIndicatorView用于显示加载指示器
  • 拖动时获取MKAnnotation的坐标

    我正在根据用户添加的注释的位置创建一条路径 MKPolyline 我想允许用户通过拖动引脚来更改路径 我目前可以做到这一点 但 MKPolyline 不会更新 直到引脚被放下 我实施了 void mapView MKMapView mapV
  • 为什么我的视图仍然以横向呈现?

    我的视图是由导航控制器控制的 因此我将导航控制器支持的方向设置为明确的纵向和纵向UpSideDown 这可以工作 但是如果调用视图时前一个视图处于横向状态 它将以横向方式呈现并保持横向状态 直到设备旋转 如何防止这种情况发生 这是我的代码
  • UIWebView Bug:-[UIWebView cut:]:无法识别的选择器发送到实例

    In the UIWebView 如果包含文本的输入元素具有焦点 并且按下按钮导致输入失去焦点 则随后双击输入以重新获得焦点并从出现的弹出栏中选择 剪切 或 复制 或 粘贴 会导致这UIWebView因错误而崩溃 UIWebView cut
  • Swift C 回调 - Swift 类指针的 takeUnretainedValue 或 takeRetainedValue

    我有一些UIView or UITableViewCell 里面我有 C 回调 例如 CCallback bridge self observer data gt Void in let mySelf Unmanaged
  • Swift 中的 UIAlert 自动消失?

    我有以下代码 Creates Alerts on screen for user func notifyUser title String message String gt Void let alert UIAlertController

随机推荐

  • 如何急切加载与 current_user 的关联?

    我在 Rails 应用程序中使用 Devise 进行身份验证 我想在我的一些控制器中加载一些与用户相关的模型 像这样的东西 class TeamsController lt ApplicationController def show te
  • 无法在 Firefox 中打开 Blob 文件

    我想在 Firefox 上打开从服务器发送的文件 实际上它是在 IE 上运行的 以下是我将如何进行 openFile path fileName this creditPoliciesService openFile path toProm
  • Rails 新建与创建

    为什么需要在 RESTful 控制器中定义一个新方法 并在其后面添加一个 create 方法 谷歌搜索没有为我提供我正在寻找的答案 我理解其中的区别 但需要知道为什么要这样使用它们 Rails 的 REST 实现new and create
  • 将多个域名映射到 Rails 应用程序中的不同资源

    我的 Rails 应用程序允许用户管理度假屋 每个属性都有自己的属性 我的应用程序中的 网站 主页 用户可以调整内容 效果很好 到目前为止很高兴 典型的 Rails 资源方法 因此特定属性的 URL 类似于特定属性的 主页 localhos
  • 具体来说,编译器会做什么来积极优化生成的字节码?

    我一直在阅读各种编译器的功能 并且遇到了许多编译器报告执行的术语 积极优化 例如 LLVM 引用了以下编译时优化功能 内存 指针特定 循环变换 数据流 算术 消除死代码 Inlining 这具体是什么意思呢 假设您有以下代码片段 如何优化生
  • Indexeddb:使用通配符搜索

    我想知道是否可以使用通配符对 indexeddb 对象存储执行搜索 例如 查找键以 555 开头的所有对象会很方便 使用复合键或键片段可以开箱即用地实现这一点 键在 IndexedDB 中的工作方式是生成一个 keyRange 对象并将其传
  • 当任何线程完成任务时终止多个线程

    我对 python 和线程都很陌生 我编写了 python 代码 它充当网络爬虫并在网站中搜索特定关键字 我的问题是 如何使用线程同时运行类的三个不同实例 当其中一个实例找到关键字时 所有三个实例都必须关闭并停止抓取网络 这是一些代码 cl
  • 为什么我的 Visual Studio Win32 项目需要安装 .NET 3.5 SP1?

    使用 Visual Studio 2008 我创建了一个 C Win32 http en wikipedia org wiki Windows API项目 为了发布该程序 我在同一解决方案中创建了一个 Visual Studio 安装项目
  • 删除 FASTA 文件中的换行符

    我有一个 fasta 文件 其中序列用换行符分隔 我想删除换行符 这是我的文件的示例 gt accession1 ATGGCCCATG GGATCCTAGC gt accession2 GATATCCATG AAACGGCTTA 我想把它转
  • PHP - 访客在线计数器

    我有以下代码来统计我的 PHP 网站上的访问者数量 它在使用 WampServer 的本地开发计算机上运行良好 但当我将文件上传到我的托管帐户进行测试时 我意识到它无法正常工作 我得到的数字非常高 并且还注意到会话永远不会被删除 因此它们只
  • Asp.net MVC Razor 页面上有多个表单

    Yo 我的网站上有一个注册页面 页面顶部是现有用户的登录表单 主区域有登记表 登录区域是部分视图 model ViewModels LoginViewModel注册区域也是部分的 model ViewModels RegViewModel
  • StreamProvider 与 RiverPod 无法正常工作(尝试从 Provider 迁移)

    我试图通过将简单的 FireStore auth Provider 示例迁移到 RiverPod 来了解 RiverPod 这是我的身份验证服务 import package firebase auth firebase auth dart
  • JSONP 长轮询始终加载

    我正在使用 JSONP 进行长轮询 而 Firefox 不断弹出 正在加载 微调器 使页面看起来像是尚未完成加载 有办法抑制这种情况吗 我被告知 Orbited 团队有一些技巧可以抑制这种情况 但浏览 Orbited js 代码我无法弄清楚
  • Pymongo 批量插入不起作用

    我正在按照教程进行操作http api mongodb org python current tutorial html http api mongodb org python current tutorial html用于批量插入 但是
  • 更改 ggplot 中的线宽,而不是大小

    我看到几篇关于改变线宽 https stackoverflow com questions 14794599 how to change line width in ggplot在 ggplot 中 这些答案虽然对OP来说内容丰富且有效 但
  • 使用引用字段值进行聚合中的 Mongodb 正则表达式

    注意 我使用的是 Mongodb 4 我必须使用聚合 因为这是更大聚合的一步 Problem 如何在集合文档中查找包含以同一文档中另一个字段的值开头的字段 让我们从这个集合开始 db regextest insert first Pizza
  • VBA Excel 中的弹出图表

    我想知道是否有一种方法可以根据特定工作表中找到的值 通过按按钮在 Excel 中创建弹出图表 最好的方法是能够在 VBA 中完成它 我一直在研究但找不到任何真正的解决方案 有什么建议么 你 你这个幸运儿 p 由于我有空 我为您创建了一个基本
  • 字符串文字:它们去了哪里?

    我对字符串文字的分配 存储位置感兴趣 我确实找到了一个有趣的答案here https stackoverflow com questions 51592 is there a need to destroy char string or c
  • 自托管 Azure DevOps Agents 卷映射

    在执行容器化任务时 在 K8s 中运行自托管 docker 构建代理时 出现以下错误 我已按照文档进行操作here https learn microsoft com en us azure devops pipelines agents
  • UILabel 优于 UIProgressView,具有多种颜色

    所以我还没有这样做 我正在尝试弄清楚如何做到这一点 所以我制作了一个 UITableView 每个单元格都有一个关联的 NSTimer 现在 在每个自定义 UITableViewCell 中 我都有一个 UIProgressView 作为背