Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding custom attributes? #9

Open
ghost opened this issue Dec 27, 2016 · 5 comments
Open

Adding custom attributes? #9

ghost opened this issue Dec 27, 2016 · 5 comments

Comments

@ghost
Copy link

ghost commented Dec 27, 2016

I'm having a bit of trouble adding my own attributes to Highlightr.

When not using Highlightr, I can add attributes to a UITextView using addAttribute(). What's a good way to add my own (e.g. for increasing line-height and changing the alignment programmatically)?

This is the code which doesn't work (creating and adding an attribute). Normally "attempt 2" is all that's needed to add an attribute to a text view.

let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.minimumLineHeight = 15
paragraphStyle.alignment = .center

// Attempt 1:  add attribute to text storage
textStorage.addAttribute(NSParagraphStyleAttributeName, value: paragraphStyle, range: NSMakeRange(0, attributedString.length))

// Attempt 2: Add attribute to text view
let attributedString = NSMutableAttributedString(attributedString: textView.attributedText)

attributedString.addAttribute(NSParagraphStyleAttributeName, value: paragraphStyle, range: NSMakeRange(0, attributedString.length))

textView.attributedText = attributedString
@ghost
Copy link
Author

ghost commented Dec 27, 2016

Update

Subclassing to provide an attribute works, but seems very hacky.

Is there a better way?

class CustomCodeAttributedString: CodeAttributedString {
    override func attributes(at location: Int, effectiveRange range: NSRangePointer?) -> [String : Any]
    {
        var attributes = super.attributes(at: location, effectiveRange: range)
        
        let paragraphStyle = NSMutableParagraphStyle()
        paragraphStyle.minimumLineHeight = 30
        paragraphStyle.alignment = .center
        attributes[NSParagraphStyleAttributeName] = paragraphStyle
        
        return attributes
    }
}

@raspu
Copy link
Owner

raspu commented Dec 28, 2016

Hi @droidery, there's no support for that right now, but I can totally add this on a future release. CodeAttributedString will dynamically add attributes to the text, and this as a side effect will remove the current attributes of the line being processed.

I will need to take some time to think of a proper solution, but for now I think your approach is the best one.

@ivancantarino
Copy link

ivancantarino commented Feb 1, 2018

@raspu I would definitely love a solution for this!
We could use your on the fly parser (I'm using it for Markdown) but with our default attributes, such as a custom font being processed with your engine.

But it's been so long since this was requested.

Is there at least a way for us to hardcode the font we want throughout the whole themes and parsing? If there's that option we can force a font there.

@raspu
Copy link
Owner

raspu commented Feb 6, 2018

@ivan-cantarino You will need to call setCodeFont on the Theme instance every time you change a theme, there's no other way right now.

@geberl
Copy link

geberl commented Feb 23, 2019

2019 / Swift 4.2 version of ghost's code snipplet from above:

class CustomCodeAttributedString: CodeAttributedString {
    override func attributes(at location: Int, effectiveRange range: NSRangePointer?) -> [AttributedStringKey : Any] {
        var attributes = super.attributes(at: location, effectiveRange: range)
        
        let paragraphStyle = NSMutableParagraphStyle()
        paragraphStyle.minimumLineHeight = 30
        paragraphStyle.alignment = .center
        attributes[NSAttributedString.Key.paragraphStyle] = paragraphStyle

        return attributes
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants