Den*_*Vog 6 compare nsattributedstring uicolor ios ios6
比较或测试NSAttributed字符串的特定颜色属性的正确方法是什么?
作为一个例子,我想知道文本选择是否有红色文本.我已经尝试了几种方法,如下所示,但它们都没有产生匹配.我看到文本在屏幕上变为红色,并且记录该属性返回:UIDeviceRGBColorSpace 1 0 0 1
- (BOOL)isRedWithAttributes:(NSDictionary *)attributes
{
BOOL isRedWithAttributes = NO;
if (attributes != nil)
{
// if ( [attributes objectForKey:NSForegroundColorAttributeName] == [UIColor redColor] )
// if ( [attributes objectForKey:NSForegroundColorAttributeName] == @"UIDeviceRGBColorSpace 1 0 0 1" )
if ( [attributes objectForKey:NSForegroundColorAttributeName] == [UIColor colorWithRed:1 green:0 blue:0 alpha:1] )
{
isRedWithAttributes = YES;
}
else
{
isRedWithAttributes = NO;
}
}
return isRedWithAttributes;
}
Run Code Online (Sandbox Code Playgroud)
以下是我为测试传递属性的方法:
NSAttributedString *selectedString = [attributedString attributedSubstringFromRange:selectedRange];
[selectedString enumerateAttributesInRange:NSMakeRange(0, [selectedString length])
options:NSAttributedStringEnumerationLongestEffectiveRangeNotRequired
usingBlock:^(NSDictionary *attributes, NSRange range, BOOL *stop)
{
UIFont *fontFace = [attributes objectForKey:NSFontAttributeName];
BOOL isRedWithAttributes = [fontFace isRedWithAttributes:attributes];
if (isRedWithAttributes)
{
NSLog(@"detected red. set selected");
[self.redButton setSelected:YES];
}
else
{
NSLog(@"detected not red. do not set selected");
[self.redButton setSelected:NO];
}
}
Run Code Online (Sandbox Code Playgroud)
我认为这不重要,但为了完整起见,我将如何将文本设置为红色.
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithAttributedString:self.synopsisTextView.attributedText];
[attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:selectedRange];
self.synopsisTextView.attributedText = attributedString;
Run Code Online (Sandbox Code Playgroud)
Objective-C是C的严格超集.Objective-C对象存在于堆上并且跟踪via指针.这样的净效果就是测试:
[attributes objectForKey:NSForegroundColorAttributeName] ==
[UIColor colorWithRed:1 green:0 blue:0 alpha:1]
Run Code Online (Sandbox Code Playgroud)
测试身份而不是平等.也就是说,不是测试两个对象是否具有相同的值,而是测试它们是否是相同的物理对象,位于内存中的同一地址.C不允许运算符重载,因此==运算符在Objective-C中具有与C中完全相同的含义.您将比较两个指针.他们碰巧指向Objective-C对象既不在这里也不在那里.
你可能想要:
[[attributes objectForKey:NSForegroundColorAttributeName] isEqual:
[UIColor colorWithRed:1 green:0 blue:0 alpha:1]]
Run Code Online (Sandbox Code Playgroud)
这将允许UIColor对象应用它认为将建立相等的任何测试.
唯一可能的警告是,UIColor只有当颜色相对于相同的颜色空间定义并且具有相同的系数时才认为颜色是相等的.假设你总是在RGBA中定义所有颜色,这些颜色应该没有什么区别.
尝试NSForegroundColorAttributeName在属性字典中测试对象,而不是NSFontAttributeName像这样:
NSAttributedString *selectedString = [attributedString attributedSubstringFromRange:selectedRange];
[selectedString enumerateAttributesInRange:NSMakeRange(0, [selectedString length])
options:NSAttributedStringEnumerationLongestEffectiveRangeNotRequired
usingBlock:^(NSDictionary *attributes, NSRange range, BOOL *stop) {
UIColor *fgColor = [attributes objectForKey:NSForegroundColorAttributeName];
if ([fgColor isEqual:[UIColor redColor]]) {
NSLog(@"detected red. set selected");
[self.redButton setSelected:YES];
} else {
NSLog(@"detected not red. do not set selected");
[self.redButton setSelected:NO];
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10225 次 |
| 最近记录: |