Giter Club home page Giter Club logo

regeributedtextview's Introduction

RegeributedTextView

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.

regeributedtextview's People

Contributors

rinov avatar

Watchers

James Cloos avatar Shohei Yokoyama avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.