获取属性字符串中的链接范围

时间:2019-03-21 19:39:47

标签: ios swift nsattributedstring

我想在属性文本中找到链接的范围,因此我只能将自定义下划线应用于相关词。

此刻,下划线位于所有文本的下方。

enter image description here

我希望它只在链接下。

由于所请求的下划线是超级定制的,因此代码有点复杂。

def generate_views(model_class, **kwargs):
    """
    For a given model, returns a dict of generic class-based views
    """
    ###
    # Forms
    #   Optionally generate form classes if not already provided
    ###

    # Append these fields with either "create_" or "update_" to have them only
    # apply to that specific type of form
    form_override_args = ['fields', 'exclude', 'form_method', 'form_class',
                          'form_layout', 'widgets', 'media_css', 'media_js']

    if 'form_class' not in kwargs and 'create_form_class' not in kwargs:
        create_form_kwargs = kwargs.copy()
        for arg in form_override_args:
            if f'create_{arg}' in kwargs:
                create_form_kwargs[arg] = kwargs[f'create_{arg}']
        kwargs['create_form_class'] = forms.FormFactory(model_class, **create_form_kwargs).form()

    if 'form_class' not in kwargs and 'update_form_class' not in kwargs:
        update_form_kwargs = kwargs.copy()
        for arg in form_override_args:
            if f'update_{arg}' in kwargs:
                update_form_kwargs[arg] = kwargs[f'update_{arg}']
        kwargs['update_form_class'] = forms.FormFactory(model_class, **update_form_kwargs).form()

    if 'form_class' not in kwargs:
        kwargs['form_class'] = forms.FormFactory(model_class, **kwargs).form()

    ###
    # Tables
    #   Optionally generate table classes if not already provided
    ###

    # Append these fields with "table_" to have them only
    # apply to the table view
    table_override_args = ['fields', 'exclude']

    if 'table_class' not in kwargs:
        update_table_kwargs = kwargs.copy()
        for arg in table_override_args:
            if f'table_{arg}' in kwargs:
                update_table_kwargs[arg] = kwargs[f'table_{arg}']
        kwargs['table_class'] = tables.TableFactory(model_class, **update_table_kwargs).table()

    ###
    # Views
    #   Generate 5 generic views based on the provided model
    ###
    view_factory = views.ViewFactory(model_class, **kwargs)

    return {
        'list_view': view_factory.list_view(),
        'detail_view': view_factory.detail_view(),
        'create_view': view_factory.create_view(),
        'update_view': view_factory.update_view(),
        'delete_view': view_factory.delete_view()
    }

1 个答案:

答案 0 :(得分:1)

使用:

let attributedText = htmlStyleAttributeText(text: text)!
...
textView.attributedText = attributedText

分隔属性:

let underlinesAttributes: [NSAttributedString.Key: Any] = [.underlineStyle: 0x15,
                                                           .underlineColor: underLineColor]

let attributes: [NSAttributedString.Key: Any] = [.font: UIFont.systemFont(ofSize: 25),
                                                 .baselineOffset: 0]

将“基本”应用于整个文本:

let wholeRange = NSRange(attributedText.string.startIndex..., in: attributedText.string)
storage.addAttributes(attributes, range: wholeRange)

我们现在枚举查找链接,并为找到的每个链接应用效果:

attributedText.enumerateAttribute(.link, in: wholeRange, options: []) { (value, range, pointee) in
    if value != nil {
        storage.addAttributes(underlinesAttributes, range: range)
    }
}