Set background color / highlight color for a given range of lines using body text

I have text written using Core Text in my iPhone app. I use NSAttributedString to set specific styles in the text for given ranges.

It seems I can not find the attribute to set the background / highlight, although it seems to be possible. I could not find an attribute name constant that sounds relevant, and only the documentation lists:

 kCTCharacterShapeAttributeName kCTFontAttributeName kCTKernAttributeName kCTLigatureAttributeName kCTForegroundColorAttributeName kCTForegroundColorFromContextAttributeName kCTParagraphStyleAttributeName kCTStrokeWidthAttributeName kCTStrokeColorAttributeName kCTSuperscriptAttributeName kCTUnderlineColorAttributeName kCTUnderlineStyleAttributeName kCTVerticalFormsAttributeName kCTGlyphInfoAttributeName kCTRunDelegateAttributeName 

Craig Hockenberry, the developer of Twitterrific, has publicly stated on Twitter that he uses Core Text to render tweets, and Twitterrific has this background / highlight, which I'm talking about when you touch the link.

alt text

Any help or pointers in the right direction would be fantastic, thanks.

Edit: Here, the link to the Craig tweet posted a mention of โ€œBody text, attribute strings and a lot of hard workโ€ and that are referenced using CTFrameSetter metrics to design if touches intersect with links.

+8
ios iphone highlight core-text nsattributedstring
source share
2 answers

In the end, I had to use Core Text metric methods (getting range, borders, line sources, etc.) to calculate the bounding box of the link. As soon as I had a rectangle for each line, the link was stretched, I turned the background into context before redrawing the text above it.

It seems like a lot of work, but it works.

+3
source share

With iOS 6, I think you can get the effect you are looking for with NSBackgroundColorAttributeName. For example:

 [attributedString addAttribute:NSBackgroundColorAttributeName value:[UIColor yellowColor] range:selectedRange]; 
+1
source share

Source: https://habr.com/ru/post/650341/


All Articles