簡體   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];

應用程序在此行崩潰[attributedString addAttributes:linkAttributes range:policylinkRange];

嘗試這個

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];

用這個:

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM