Skip to content

Transform original string through style (TextTransform for Style) #93

Description

@stefanomondino

Sometimes it can be useful to have a Style capable of transforming the string itself.
Our current use case scenario is this one:

  • we are in the middle of development and already have a lot of views with labels. Each label has its custom Style
  • Backend started to send us HTML entities (& instead of & is the most annoying example).
  • Not being able to display HTML entities should be an issue on the UILabel

Another use case scenario is this #91

I believe something like this could work:

Style {
 $0.color = UIColor.black
 $0.transform = { text in
       text.replacingOccurrences(of: "&amp:", with: "&")
     }
}

Or even something more complex like BonMot is doing here

I'll try to put together a PR.

Activity

  1. stefanomondino commented on Dec 12, 2019

    @stefanomondino
    ContributorAuthor

    Way more difficult than I expected.
    Transforming input string is quite easy for simple Style, but for StyleGroup is REALLY hard.
    Having the chance to manipulate the string by changing its length messes up with ranges used to detect tags.

  2. added a commit that references this issue on Dec 20, 2019
  3. self-assigned this
    on Dec 20, 2019
  4. added this to the 3.1.0 milestone on Dec 20, 2019
  5. added 3 commits that reference this issue on Dec 21, 2019
  6. malcommac commented on Dec 21, 2019

    @malcommac
    Owner

    It will be available inside the next release.

    Custom Text Transforms

    Sometimes you want to apply custom text transforms to your string; for example you may want to make some text with a given style uppercased with current locale.
    In order to provide custom text transform in Style instances just set one or more TextTransform to your Style's .textTransforms property:

    let allRedAndUppercaseStyle = Style({
    	$0.font = UIFont.boldSystemFont(ofSize: 16.0)
    	$0.color = UIColor.red
    	$0.textTransforms = [
    		.uppercaseWithLocale(Locale.current)
    	]
    })
    
    let text = "test".set(style: allRedAndUppercaseStyle) // will become red and uppercased (TEST)

    While TextTransform is an enum with a predefined set of transform you can also provide your own function which have a String as source and another String as destination:

    let markdownBold = Style({
    	$0.font = UIFont.boldSystemFont(ofSize: 16.0)
    	$0.color = UIColor.red
    	$0.textTransforms = [
    		.custom({
    			return "**\($0)**"
    		})
    	]
    })

    All text transforms are applied in the same ordered you set in textTransform property.

  7. changed the title [-]Transform original string through style[/-] [+]Transform original string through style (TextTransform for Style)[/+] on Dec 21, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions