简体   繁体   English

在iOS中为特定范围的NSMutableAttributedString添加属性时崩溃

[英]Getting crash while adding attributes to NSMutableAttributedString for a particular range in iOS

NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]
                                                initWithString:@"I have read, understand and agree to the following terms and conditions and web usage policy." attributes:nil];
NSRange conditionlinkRange = NSMakeRange(50,70);
// for the word "terms and conditions" in the string above
NSRange policylinkRange = NSMakeRange(75,92);
NSDictionary *linkAttributes = @{ NSForegroundColorAttributeName : [UIColor colorWithRed:0.05 green:0.4 blue:0.65 alpha:1.0],
                                  NSUnderlineStyleAttributeName : @(NSUnderlineStyleSingle) };
[attributedString addAttributes:linkAttributes range:policylinkRange];
[attributedString addAttributes:linkAttributes range:conditionlinkRange];

Application gets crashed at this line [attributedString addAttributes:linkAttributes range:policylinkRange]; 应用程序在此行崩溃[attributedString addAttributes:linkAttributes range:policylinkRange];

Try This 尝试这个

NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"I have read, understand and agree to the following terms and conditions and web usage policy." attributes:nil];

NSRange conditionlinkRange = NSMakeRange(51,20);
NSRange policylinkRange = NSMakeRange(86,6);
NSDictionary *linkAttributes = @{ NSForegroundColorAttributeName : [UIColor colorWithRed:0.05 green:0.4 blue:0.65 alpha:1.0], NSUnderlineStyleAttributeName : @(NSUnderlineStyleSingle) };

[attributedString addAttributes:linkAttributes range:policylinkRange];
[attributedString addAttributes:linkAttributes range:conditionlinkRange];

Use this: 用这个:

NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"I have read, understand and agree to the following terms and conditions and web usage policy." attributes:nil];
    NSRange conditionlinkRange = [attributedString.string rangeOfString:@"terms and conditions"];

    // for the word "terms and conditions" in the string above
    NSRange policylinkRange = [attributedString.string rangeOfString:@"policy"];
    NSDictionary *linkAttributes = @{ NSForegroundColorAttributeName : [UIColor colorWithRed:0.05 green:0.4 blue:0.65 alpha:1.0],  NSUnderlineStyleAttributeName : @(NSUnderlineStyleSingle) };

    [attributedString addAttributes:linkAttributes range:policylinkRange];
    [attributedString addAttributes:linkAttributes range:conditionlinkRange];

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

相关问题 iOS崩溃@ NSMutableAttributedString - iOS crash @ NSMutableAttributedString iOS - 将所有NSMutableAttributedString属性复制到另一个NSMutableAttributedString - iOS - copy all NSMutableAttributedString attributes to another NSMutableAttributedString iOS NSMutableAttributedString使用无法识别的选择器崩溃 - iOS NSMutableAttributedString Crash With unrecognized selector 在特定范围内将属性添加到NSMutableAttributedString - Adding attribute to NSMutableAttributedString on a specific range iOS NSMutableAttributedString崩溃EXC_BAD_ACCESS - IOS NSMutableAttributedString crash EXC_BAD_ACCESS NSMutableAttributedString 添加多个隐藏文本的属性 - NSMutableAttributedString adding multiple attributes hiding the text ios - 在保持HTML属性的同时编辑NSMutableAttributedString - ios - edit NSMutableAttributedString while keeping HTML properties 在ios中获取当前的自动对焦系统时崩溃 - Crash while getting the current Autofocus system in ios iOS 10.3:如果将NSStrikethroughStyleAttributeName应用于NSMutableAttributedString的子范围,则不会渲染 - iOS 10.3: NSStrikethroughStyleAttributeName is not rendered if applied to a sub range of NSMutableAttributedString 在异步线程上在RLMArray中添加对象时,iOS应用程序崩溃 - iOS app getting crash when adding object in RLMArray on async thread
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM