简体   繁体   English

NSMutableAttributedString崩溃应用程序

[英]NSMutableAttributedString Crashing App

I'm doing something wrong with the range (I think) in setting this NSMutableAttributedString . 我在设置此NSMutableAttributedString的范围(我认为)上做错了。 Can anyone tell me why this is crashing my program? 谁能告诉我为什么这会使我的程序崩溃? It looks right to me, but I'm obviously wrong! 对我来说看起来不错,但我显然错了! Thanks. 谢谢。

NSString *placeHolderString = @"USERNAME";
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] init];
float spacing = 5.0f;

// crashes on this line
[attributedString addAttribute:NSKernAttributeName
                         value:@(spacing)
                         range:NSMakeRange(0, [placeHolderString length])];

self.userNameTextField.attributedPlaceholder = attributedString;

What I think was causing your issue is that you were never really accounting for your placeholderString in the first place. 我认为造成您的问题的原因是,您从未真正考虑过placeholderString In addition, your value parameter could simply use numberWithFloat as the application would then known what type you are using all the time. 另外,您的value参数可以简单地使用numberWithFloat因为应用程序随后将始终知道您使用的是哪种类型。

Once you account for the placeHolderString , you are then going to use the length for the attributeString , as it now just contains the contents of your placeholderString . 一旦考虑了placeHolderStringplaceHolderString使用attributeString的长度,因为它现在仅包含placeholderString的内容。 Then we just simply copy that string which contains your attribute using the UITextField property attributedText . 然后,我们只需使用UITextField属性attributedText复制包含您的属性的字符串。

NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:placeHolderString];
float spacing = 5.0f;
[attributeString addAttribute:NSKernAttributeName
                        value:[NSNumber numberWithFloat:spacing]
                        range:(NSRange){0,[attributeString length]}];
userNameTextField.attributedText = [attributeString copy];

For more context, attributes like NSUnderlineStyleAttributeName exist and you can do some really powerful things with NSMutableAttributedString . 对于更多上下文,存在诸如NSUnderlineStyleAttributeName属性,您可以使用NSMutableAttributedString进行一些真正强大的操作。 Refer to the documentation for options. 有关选项,请参阅文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 调用NSMutableAttributedString的append子类时,应用崩溃 - App is crashing when calling append insubclass of NSMutableAttributedString NSMutableAttributedString在更改字体时崩溃? - NSMutableAttributedString crashing on changing the font? 在NSMutableAttributedString初始化期间代码崩溃 - Code crashing during initialization of NSMutableAttributedString 带有CTStringAttributes和NSMutableAttributedString的CTFont在xamarin中崩溃了我的应用程序 - CTFont with CTStringAttributes and NSMutableAttributedString crashes my app in xamarin 如何使 NSMutableAttributedString 响应设置应用程序中的动态类型文本 - How to make NSMutableAttributedString responsive with dynamic type text from settings app 将具有字体和大小的NSMutableAttributedString放入UIPasteboard中以粘贴到另一个应用程序中 - Putting NSMutableAttributedString with font and size into UIPasteboard to paste into another app NSMutableAttributedString包含NSMutableAttributedString和NSString - NSMutableAttributedString contains NSMutableAttributedString and NSString Touch ID Crashing App - Touch ID Crashing App 在应用启动之前崩溃 - Crashing before the app starts Heroku应用程序不断崩溃 - Heroku App Crashing Constantly
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM