Latest 0.1.0
Homepage https://github.com/rinov/RegeributedTextView
License MIT
Platforms ios 8.0
Authors

Build Status
Cocoapods
Carthage
License
Platform
Language

RegeributedTextView is a subclass of UITextView that supports fully attribute string based on regular expression.

DEMO

Usage

import RegeributedTextView

and you can use RegeributedTextView as subclass of UITextView in Interface Builder.

InterfaceBuilder

It is simple to use an attribute string.

Chage the text color:

textView.addAttribute(.all, attribute: .textColor(.red)))

Instead of .all, you can use regular expression .

textView.addAttribute(".*", attribute: .textColor(.red)))

If you want highlight a mention and hash tag like a SNS, you can use following parameters.

textView.addAttribute(.mention, attribute: .bold))
textView.addAttribute(.hashTag, attribute: .textColor(.blue)))

Available attribute type

Attribute Type
backgroundColor UIColor
bold
boldWithFontSize CGFloat
font UIFont
fontName String
fontSize CGFloat
italic CGFloat
linkColor UIColor
shadow Shadow
strikeColor UIColor
strikeWithThickness CGFloat
strokeWidth CGFloat
textColor UIColor
underline UnderlineStyle
underlineColor UIColor

Link text behavior

In swift 3, The property linkTextAttributes of UITextView can designate the link text behavior but it is not possible to coloring a few text separately in the same text.
In this case, you can use attributes text based on regular expression like this.

e.g. Set text color separately.

textView.addAttribute("@[a-zA-Z0-9]+", attributes: [.textColor(.black), .bold], values: ["Type": "Mention"])
textView.addAttribute("#[a-zA-Z0-9]+", attribute: .textColor(.blue), values: ["Type": "HashTag"])

In RegeributedTextView, All attributed text can be detected for each word by tapping.
and you can detect a tap event of link text by RegeributedTextViewDelegate.
The arguments of values can embbed any values.

func regeributedTextView(_ textView: RegeributedTextView, didSelect text: String, values: [String : Any]) {
    print("Selected word: (text)")
    if let url = values["Type"] as? String {
        // Do something
    }

Advanced settings

public func addAttribute(_ regexString: String, attribute: TextAttribute, values: [String: Any] = [:], priority: Priority = .medium, applyingIndex: ApplyingIndex = .all)

To control an attribute text, you can use Prioriry and ApplyingIndex.
Priority represents that attribute string priority.
Attribute text range is sometime overlaped, so this property enable to control attribute string priority like AutoLayout.

Rules:

  • The new attribute priority is greater than the current attribute, it’s overwriten.
  • The new attribute priority is less than the current attribute, it’s ignored.

Applying Index represents which text should be applied an attribute because it is difficult to control the attribute text order using only regular expression.

e.g. Applying only first element

let userName = "rinov"
textView.addAttribute(userName, attribute: .bold, applyingIndex: .first)

ApplyingIndex is available following patterns.

ApplyingIndex Description
all All matched text is applyed
first Only first element is applyed
firstFrom(Int) It’s applyed for specified number of times from the start index of the text
ignoreFirstFrom(Int) It’s ignore for specified number of times from the start index of the text
last Only last element is applyed
lastFrom(Int) It’s applyed for specified number of times from the end index of the text
ignoreLastFrom(Int) It’s ignore for specified number of times from the end index of the text
indexOf(Int) Applyed only specified index
ignoreIndexOf(Int) Ignore only specified index

Requirements

XCode 8+
Swift 3+

Installation

Cocoapods:

$: pod repo update

pod "RegeributedTextView"

and

$: pod install

or

Carthage:

github "rinov/RegeributedTextView"

and

$: carthage update --platform iOS

Author

rinov, [email protected]

License

RegeributedTextView is available under the MIT license. See the LICENSE file for more info.

Latest podspec

{
    "name": "RegeributedTextView",
    "version": "0.1.0",
    "summary": "RegeributedTextView is a subclass of UITextView that supports attribute string based on regular expression.",
    "description": "`RegeributedTextView` is a subclass of `UITextView` and very easy to use attribute string.",
    "homepage": "https://github.com/rinov/RegeributedTextView",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "rinov": "[email protected]"
    },
    "source": {
        "git": "https://github.com/rinov/RegeributedTextView.git",
        "tag": "0.1.0"
    },
    "platforms": {
        "ios": "8.0"
    },
    "source_files": "RegeributedTextView/Classes/**/*",
    "pushed_with_swift_version": "3.0"
}

Pin It on Pinterest

Share This