Apply attributes from one NSAttributedString to another - ios

Browsing Attributed String Programming Guide and the documentation for NSAttributedString and NSMutableAttributedString, I can't see a straightforward way to take all the attributes from one string and apply them to another.
The only possibility I've found so far is to to use -[NSAttributedString enumerateAttributesInRange:options:usingBlock:] but that seems like a very long way around.
Am I missing something? Surely there must be a cleaner solution?
What I'm ultimately aiming to achieve is to take an attributed string from a static label I've configured in a Storyboard, change it to contain dynamic info and apply certain attributes over certain ranges.

Use - (NSDictionary *)attributesAtIndex:(NSUInteger)index effectiveRange:(NSRangePointer)aRange to get the attributes at a specific index (e.g. 0 for the first character in string). Then use this attribute in - (void)setAttributes:(NSDictionary *)attributes range:(NSRange)aRange to set this attributes for all characters in range.

Related

Excluding place holder texts from localisations

When I design a cell layout I usually assign a sample text, e.g. 'John Appleseed' to a 'name' label so I can easily see where the field is on the layout and check the composition. Otherwise there is an empty label on a white background. Obviously this text does not need to be translated as it will be always replaced by another value at runtime.
Is there any property I can set in the Object Inspector to exclude this text from .strings / XLIFF file? Translators usually charge per word, so I don't want to send those texts for translation.
For the time being I use '~' prefix and then remove those texts using a Ruby script, but I was wondering whether there is an easier way to do it.
Unfortunately, if you are using ibtool (and you do not really have an alternative) you cannot exclude words directly.
What you can do, is to edit the XLIFF file after you export it and add the attribute translate="no" on the strings you want to exclude. You should make sure that your translators use a XLIFF-compatible tool to translate.
But, imho, this is not any better than your way.
Also see question 1, question 2 and ibtool's manual.

TTTAttributedLabel "Read More >" tail truncation with several attributes possible?

TTTAttributedLabel support a custom truncation string via truncationTokenString as well as truncationTokenStringAttributes.
However, I am wanting to go a bit further and set several string attributes on the truncation string, including different fonts and colours. This is what I am trying to achieve:
The arrow at the end can be achieved using a font icon, so I was thinking of the following string:
#"… Read More >"
'HORIZONTAL ELLIPSIS' (U+2026) + Read More + > character from a font.
Unfortunately TTTAttributedLabel doesn't allow me to set the ranges for various attributes.
Does anyone have a good solution for this or will have have to do it manually and basically calculate the string as it can be drawn on two lines including the #"… Read More >" string.
Thanks!
I'm just answering my own question in case anyone stumbled onto it needing the same functionality.
At the time of posting the question TTTAttributedLabel only supported setting attributes on the whole truncation token string.
I added functionality for NSAttributedString support (for which you can specify ranges, which was missing from the plan NSDictionary with attributes approach) and submitted a pull request. It hasn't yet been merged into the main branch for TTTAttributedLabel, but in case anyone's in need of the same functionality you can find the PR here:
https://github.com/runmad/TTTAttributedLabel/commit/628de3f9cb688eb58fffdbb6e7ff81fa0d8b7f4a

using nsattributedstring and nslocalizedstring

My old code uses NSLocalizedString to display the following where outputText was an NSMutableString that contained many such lines in a single output session:
[outputText appendFormat: NSLocalizedString(#"\n\n%# and %# are identical. No comparison required.", #"\n\n%# and %# are identical. No comparison required."), self.ipAddress, secAddress.ipAddress];
I'm trying to change the color of the various ipAddress strings, but can't find a similar method when using NSMutableAttributedString.
The biggest problem I'm facing is that since this entire string will be localized, I can't reliably set the NSRange without breaking up each part of the formatted output.
Do I need to dissect each part of this string, convert it to NSAttributedString and append each piece to the outputText??
The answer is: yes.
Yes, you need to localize sections with different attributes separately.

Increase or Decrease an Integer Within a NSString

My app is a simple blog app. I parse an XML, and build custom HTML code from the parsed parts, and load that HTML into a webview. Not everyone has the same quality of vision, so I am trying to add simple buttonsto add or decrease the font size. Currently, the HTML may look like:
font size=5 The Title.../font font size = 3 The body of text.../font
What I would like to do is take whatever number is listed, and increase or decrease by 1.
Any suggestions on how I can take just those 2 numbers (don't want to increase other numbers that may be in body of article) and increase or decrease incrementally?
I'd recommend stuffing the HTML you parsed out of the XML into a "NSMutableString" object and then hunt through the mutable string object looking for the ranges of all substrings that match the pattern "font size=".
Once you find those ranges, you should be able to get the font sizes and then put them into a "NSInteger" via something like [NSString integerValue] which you can then increment and put back into the mutable string object. Obviously there's some extra detail I'm not thinking too hard about, like how to replace a "9" in the mutable string with a "10" or to tell the difference between a font size of "10" and one that's "100". But at least this should be a good start for you.

Can the original string used to generate a CTLine variable be retrieved from it?

I want to test for a tap event within a Core Text CTLineRef variable. I have a CTLineRef variable, touchedLine, that I have retrieved by iterating over the CTLines in a frame. Once I have found the string with a hit within its bounds, I retrieve the index of the character in the original string from CTLineGetStringIndexForPosition.
Supposing now that the string data used to create this is not readily accessible without restructuring a portion of the code, can I retrieve the original NSAttributedString used to create the CTLineRef variable touchedLine from touchedLine itself---some sort of a CTLineGetAttributedString function?
Since it is an opaque type, CTLine should contain the data, but there appears to be no way to retrieve them beyond the listed functions in the Apple documentation. So, briefly, no, there isn't.
(One caveat to keep in mind, which helped me solve my initial problem, is that the coordinate system which you are using for the position with CTLineGetStringIndexForPosition may be y-flipped relative to the rest of your system---that whole Core Text thing again...)

Resources