【问题标题】:Attributed UILabel does not respect IB归因的 UILabel 不尊重 IB
【发布时间】:2013-09-10 20:23:08
【问题描述】:

我第一次决定为我的 UILabel 使用 NSAttributed 字符串,

尽管如此,当我在运行代码时在 IB(界面构建器界面)中设置标签的 UILabel 属性(字体、颜色、对齐方式)时,这些属性永远不会受到尊重!我必须手动为我的标签添加属性:

_myLabel.font = [UIFont fontWithName:@"Arial-BoldMT" size:25];

有人可以帮我吗?

【问题讨论】:

    标签: iphone ios objective-c uilabel nsattributedstring


    【解决方案1】:

    您正在做的是将字体字体应用于标签的文本属性。如果您想为属性文本分配字体,您必须将该字体分配为属性字符串的属性之一。

    NSString *inputString = @"fsdfdsf";
    
    NSDictionary *attributes = @{NSFontAttributeName: [UIFont fontWithName:@"Arial-BoldMT" size:25]};
    
    NSAttributedString *attributecString = [[NSAttributedString alloc] initWithString:inputString attributes:attributes];
    
    [_myLabel setAttributedText:attributecString];
    

    【讨论】:

    • 这是我做的,但我的 IB 属性没有得到尊重,所以我在代码中设置了它们。 . .
    • @JackoJack 如果为标签设置属性文本,之前的属性将丢失。您必须将属性重新应用于字符串。
    • 谢谢!所以我必须在 IB 中将属性文本设置为标签并在我的代码中使用 .text ?
    • @JackoJack 不,您可以根据需要设置 _lable.attributedText,您只需确保将相同的属性应用于您正在应用的新字符串。
    猜你喜欢
    • 1970-01-01
    • 2016-09-26
    • 1970-01-01
    • 1970-01-01
    • 2012-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多