-->

我该如何申请UIAppearance代理属性的UILabel?(How do I apply UIA

2019-06-17 16:14发布

我已经越来越不可靠的结果,同时尝试应用UIAppearance代理样式UILabel类代理。 例如,下面的工程,我期望:

[[UILabel appearance] setFont:[UIFont fontWithName:SOME_FONT size:SOME_SIZE]];
[[UILabel appearance] setShadowColor:[UIColor blackColor]];

设置文字颜色不工作,但是,这样的:

[[UILabel appearance] setColor:[UIColor greenColor]];

确实工作。 种类 。 这有点不可靠,导致任何特定实例的调用setTextColor:被忽略。

什么是UIAppearance样式应用到一个UILabel正确的方法是什么?

Answer 1:

好了,事实证明,你不能使用样式任何性质的UILabel UIAppearance代理。

虽然UILabel类符合UIAppearanceContainer协议,UILabel.h的检查显示,没有它的性能都标有UI_APPEARANCE_SELECTOR ,对于使用的前提UIAppearance

开溜。



Answer 2:

我有子类的UILabel

@interface SmallLabel : UILabel

@end

@implementation SmallLabel

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
    }
    return self;
}

@end

然后我用appearanceWhenContainedIn:

UIFont *smallFont = [UIFont fontWithName:@"Arial" size:15];
[[SmallLabel appearanceWhenContainedIn:[UIView class], nil] setFont:smallFont];

这适用于使用所需的字体在我的应用SmallLabels。 我只需要自定义类设置为SmallLabel在Xcode身份检查。 它似乎并没有工作,标签编程创建,只有那些在发钞银行。

经过进一步测试这种方法并不总是可靠地工作。



Answer 3:

在斯威夫特你做下面的自定义包含在UITableViewHeaderFooterView当一个UILabel的外观属性:

UILabel.appearance(whenContainedInInstancesOf: [UITableViewHeaderFooterView.self]).font = UIFont.boldSystemFont(ofSize: 18)
UILabel.appearance(whenContainedInInstancesOf: [UITableViewHeaderFooterView.self]).textColor = .white

这将适用于为textLabel属性,当你在使用UITableViewHeaderFooterView:

 public func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    var headerView = tableView.dequeueReusableHeaderFooterView(withIdentifier: headerViewId)
    if headerView == nil {
        headerView = UITableViewHeaderFooterView(reuseIdentifier: headerViewId)
    }
    headerView?.textLabel?.text = "My Header".uppercased()
    return headerView
}

使用UITableViewStyle.grouped时,那些节头观点似乎与默认样式覆盖,即使你自定义UITableViewHeaderFooterView.textLabel在viewForHeaderInSection这确实有助于



Answer 4:

下面的代码为我工作迅速使用2.2和适用于iOS 9.0

    let textFieldInsideSearchBar = self.searchBar?.valueForKey("searchField") as? UITextField
    textFieldInsideSearchBar?.textColor = BCGConstants.Colors.darkBluishPurpleColor()

    let textFieldInsideSearchBarLabel = textFieldInsideSearchBar!.valueForKey("placeholderLabel") as? UILabel
    textFieldInsideSearchBarLabel?.textColor = UIColor(red: 220/255, green: 209/255, blue: 231/255, alpha: 1.0)`enter code here`


文章来源: How do I apply UIAppearance Proxy properties to UILabel?