【问题标题】:iOS7 - How to underline a UITextFieldiOS7 - 如何下划线 UITextField
【发布时间】:2013-12-31 21:12:22
【问题描述】:

我已经查看(并尝试了代码 sn-ps)this question,但想知道 iOS7 中是否出现了什么问题?更有可能是我做错了什么。

我的代码如下:

import <CoreText/CoreText.h>

    NSString *tempString1 = [currentMemory valueForKey:@"title"];

    NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] initWithString:tempString1];
        [attString addAttribute:(NSString*)kCTUnderlineStyleAttributeName
                    value:[NSNumber numberWithInt:kCTUnderlineStyleSingle]
                    range:(NSRange){0,[attString length]}];
        self.titleLabel.attributedText = attString;
        self.titleLabel.textColor = [UIColor whiteColor];

我得到这个编译错误:

*** 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“NSConcreteMutableAttributedString initWithString:: nil value”

如果我使用纯文本

self.titleLabel.text = [NSString stringWithFormat:@"%@", tempString1];

它工作正常。任何帮助表示赞赏。

【问题讨论】:

    标签: ios ios7 nsmutablestring


    【解决方案1】:

    我认为您的字典 currentMemory 正在为键“title”返回 nil,并且该 nil 导致 init 中的异常。

    【讨论】:

    • 谢谢斯蒂芬。当结果是一个普通的 NSString 时,为什么这会起作用?我正在记录当前的内存,当我使用 NSString 时它是满的,而在使用属性字符串时它是空的.. 很奇怪..
    猜你喜欢
    • 2022-01-21
    • 2015-08-22
    • 1970-01-01
    • 1970-01-01
    • 2017-04-10
    • 2018-05-19
    • 2014-10-20
    • 2020-12-11
    • 1970-01-01
    相关资源
    最近更新 更多