【问题标题】:How to set UILabel properties like textcolor, font etc using KVC?如何使用 KVC 设置 UILabel 属性,如 textcolor、font 等?
【发布时间】:2013-05-30 06:53:58
【问题描述】:

我需要知道如何使用 KVC 设置 UILabel 属性,如文本颜色、字体、对齐方式等。在下面的链接中有一个关于如何设置UIButton 标题标签文本的示例。 How do I change a UIButton's title using KVC?

鉴于我已经尝试过这样的:

NSDictionary *labelProperties = @{@"textColor":@"redColor", @"font":@"Helvetica"},@"frame":@"50,50,50,50", @"text":@"Hi....", @"backgroundColor":@"yellowColor"};
UILabel*myLabel = [[UILabel alloc]init];
[myLabel setValuesForKeysWithDictionary:labelProperties];
[self.view addSubview:myLabel];

但它没有用。应用程序崩溃了。我希望这是由于键的值不正确。值的格式应该是什么,以便上面的代码起作用。我需要像这样设置所有的 UI 属性。

是否有任何教程可以让我了解如何设置值?

【问题讨论】:

    标签: iphone ios objective-c ipad kvc


    【解决方案1】:

    当所需的实际值显然不是字符串时,您正试图将所有值设置为字符串。

    尝试这部分并从那里开始工作:

    NSDictionary *labelProperties = @{@"textColor":[UIColor redColor]}
    

    【讨论】:

    • 我不能用那个。我从服务器获取所有值,它们是字符串类型的。我需要的是字符串值的正确格式,以便我可以直接使用它
    • @Pallavi:没有“字符串值的正确格式”。 keys 在 KVC 中总是字符串,但 values 不是。 “textColor”是 UIColor,“font”是 UIFont,“frame”是 CGRect,...
    猜你喜欢
    • 2011-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多