Cha*_*lis 19 nsattributedstring uilabel nsrange nsrangeexception
我正在设置标签的属性文本,我收到这个奇怪的错误:由于未捕获的异常'NSRangeException'而终止应用程序,原因:'NSMutableRLEArray replaceObjectsInRange:withObject:length :: Out of bounds'.我之前从未见过这个错误,所以我不确定如何修复它.以下是导致崩溃的代码:
if ([cell.waveTextLabel respondsToSelector:@selector(setAttributedText:)])
{
const CGFloat fontSize = 16.0f;
//define attributes
UIFont *boldFont = [UIFont fontWithName:@"HelveticaNeue-Medium" size:fontSize];
// Create the attributes
NSDictionary *attrs = [NSDictionary dictionaryWithObjectsAndKeys:boldFont, NSFontAttributeName, nil];
NSRange rangeOfIs = [cell.cellWaveObject.waveString rangeOfString:@" is"];
NSLog(@"The range of is: {%lu, %lu}", (unsigned long)rangeOfIs.location, (unsigned long)rangeOfIs.length);
NSRange nameRange = NSMakeRange(0, rangeOfIs.location);
NSLog(@"The range of the name: {%lu, %lu}", (unsigned long)nameRange.location, (unsigned long)nameRange.length);
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:cell.cellWaveObject.waveString];
[attributedString setAttributes:attrs range:nameRange];
//set the label text
[cell.waveTextLabel setAttributedText:attributedString];
}
Run Code Online (Sandbox Code Playgroud)
我设置了一个异常断点,并且在线[attributedString setAttributes:attrs range:nameRange];
上崩溃有没有人知道如何解决这个问题?
小智 0
该错误明确意味着范围超出了字符串的范围,您可以尝试将您要应用的字符串存储在局部变量中吗?您也可以尝试 substringWithRange 来查看您是否使用了正确的范围。如果范围正确,则不应显示该错误。
归档时间: |
|
查看次数: |
2182 次 |
最近记录: |