【问题标题】:iOS 7 UITextView select word or imageiOS 7 UITextView 选择文字或图片
【发布时间】:2013-10-18 15:55:00
【问题描述】:

我在 iOS 7 中有一个使用属性文本的 UITextView。在放入 UITextView 之前解析的原始文本看起来像这样。

“欢迎来到我的示例@(John Doe)(johndoeid) @(Jane Doe)(janedoeid)”

当我解析此文本并将其放入 UITextView 时,它看起来像这样。

“欢迎来到我的示例 John Doe Jane Doe

我的问题是这样的。

当我在文本视图中单击 John Doe 或 Jane Doe 时,如何获取用户“johndoe”或“janedoe”的 ID,以便对此采取操作?我正在考虑存储原始位置和新位置并使用它,但这似乎很笨重。

【问题讨论】:

    标签: ios uitextview nsdatadetector


    【解决方案1】:

    假设您使用NSAttributedString,您可以向文本添加自定义属性。请查看下面的此链接以查看有关如何搜索与文本关联的特定属性的示例。

    这些示例都不会真正创建自定义属性,但您可以看到我们如何搜索每个字体属性并更改字体大小。附加自定义属性后,您将能够进行类似的操作。如果您遇到困难,请告诉我,我可以尝试为您破解更具体的内容。

    http://ossh.com.au/design-and-technology/software-development/implementing-rich-text-with-images-on-os-x-and-ios/

    具体查看OSTextView.m 源列表文件中的resizeText 方法。下面是一些搜索NSFont属性的代码

    [self.textStorage enumerateAttributesInRange:rangeAll options:NSAttributedStringEnumerationLongestEffectiveRangeNotRequired usingBlock:
     ^(NSDictionary *attributes, NSRange range, BOOL *stop) {
    
         // Iterate over each attribute and look for a Font Size
         [attributes enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
             if ([[key description] isEqualToString:@"NSFont"]) {
                 UIFont *font = obj;
                 float fontSize = font.pointSize + bySize;
                 smallestFontSize = MIN(smallestFontSize, fontSize);
                 largestFontSize = MAX(largestFontSize, fontSize);
             }
    
         }];
     }];
    

    该方法的更下方是替换 NSFont 属性的代码,在您的情况下,您可以添加自定义属性 - 请注意,我们首先复制现有属性,然后添加到它们,因为您可能不想删除任何现有属性属性。

    [self.textStorage enumerateAttributesInRange:rangeAll options:NSAttributedStringEnumerationLongestEffectiveRangeNotRequired usingBlock:
     ^(NSDictionary *attributes, NSRange range, BOOL *stop) {
    
         NSMutableDictionary *mutableAttributes = [NSMutableDictionary dictionaryWithDictionary:attributes];
    
         // Iterate over each attribute and look for a Font Size
         [mutableAttributes enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
    
             if ([[key description] isEqualToString:@"NSFont"]) {
    
                 UIFont *font = obj;
                 float fontSize = font.pointSize;
                 fontSize += bySize;
                 fontSize = MIN(fontSize, MAXFONTSIZE);
    
                 // Get a new font with the same attributes and the new size
                 UIFont *newFont = [font fontWithSize:fontSize];
    
                 // Replace the attributes, this overrides whatever is already there
                 [mutableAttributes setObject:newFont forKey:key];
             }
    
         }];
    
         // Now replace the attributes in ourself (UITextView subclass)
         [self.textStorage setAttributes:mutableAttributes range:range];
     }];
    

    现在您的自定义属性已整齐地嵌入到属性字符串中,您应该能够对其进行归档和取消归档而不会丢失它。

    【讨论】:

      【解决方案2】:

      是的,将所有位置存储在 NSDictionary 中并在该数据结构中进行查找是最有意义的。

      我知道这看起来很笨拙,但这确实是您唯一的选择。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-07-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-05-21
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多