ios - 自定义 UILabel 类 iOS

标签 ios iphone uilabel

在我的 iOS 应用程序中,我想要 UILabel,它可以在带有动态文本的大部分屏幕上使用。

因此,我在我的 Utility 类中编写了方法,它创建了具有关联 UIView 的 UILabel,并且我还将文本设置为我想要的 UILabel。 我使用这个 Utility 类方法在我想要的地方创建 UILabel

但是,我面临的问题是我无法从 View 中隐藏或删除此 UILabel。

所以请给我您宝贵的建议...

提前致谢......

+(void)drawLabel:(UIView *)view message:(NSString *)message
{
    UILabel *messageLabel = [[UILabel alloc]init];
    messageLabel.frame = CGRectMake(0, 0, 320, 30);
    [messageLabel setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin];
    messageLabel.text = message;
    [messageLabel setCenter:view.center];
    [messageLabel setTextAlignment:NSTextAlignmentCenter];
    [view addSubview:messageLabel];
}

这是我在 Utility 类中编写的用于创建 UILabel 的方法。

[CACCustomMessageLabel drawLabel:self.view message:@"MY LABEL2"];

我像上面一样使用它。

最佳答案

您可以在实用程序类中创建这样的方法。

+(void)drawLabel:(UIView *)view message:(NSString *)message isHide:(BOOL)isHide
{
    UILabel *messageLabel = [[UILabel alloc]init];
    messageLabel.frame = CGRectMake(0, 0, 320, 30);
    [messageLabel setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin];
    messageLabel.text = message;
    [messageLabel setCenter:view.center];
    [messageLabel setTextAlignment:NSTextAlignmentCenter];
    [view addSubview:messageLabel];
    messageLabel.hidden = isHide;
}

这是我在 Utility 类中编写的用于创建 UILabel 的方法。这是用于在 View 中隐藏和取消隐藏标签的 isHide 参数。

[uilabelUtility drawLabel:self.view message:@"Jaydip Godhani" isHide:YES];

这里的 YES 是隐藏标签,不让你看到。

关于ios - 自定义 UILabel 类 iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32782410/

相关文章:

ios - 设备旋转时的 Swift UIcollectionViewcell 网格布局 subview 未正确更新(以编程方式)

ios - 通过 CVOpenGLESTextureCacheCreate 获取屏幕截图

ios - 为什么我的自定义表格单元格中的标签在单击行之前无法显示

iphone - 如何在 UILabel 中实现 pic 中的背景图像?

ios - 横向 iPhone 6 Plus 的 UIModalPresentationPopover 不显示弹出窗口

ios - 需要有关将 UIDatePicker 时间转换为时间戳的帮助

iphone - 用于 iPhone 测试的通用 iPhone/iPad 应用程序调试编译错误

ios - 如何检查 NSDictionary 键值是否为空?

iphone - 比较 google place API mapPoints 与核心数据对象 mapPoints - mapView

ios - 如何在具有无限行的 UILabel 上自动换行?