How to highlight multiple text fragments on iOS? - ios

The past few days, I have been searching for a suitable solution to highlight multiple text fragments on iOS. The general idea is similar to how text highlighting works in Amazon's Kindle application.
I have experimented with UITextView as well as UIWebView, but I haven't find the right solution in terms of usability and performance.
The idea is simple. The user taps a sentence and the sentence is highlighted. When another sentence is tapped, that sentence is highlighted as well. The built-in solution for highlighting text is not suitable for this purpose. An important aspect of the solution is that the text needs to be styled, which is possible with UITextView since iOS 6.
My question is fairly general, that is, what are some viable approaches to implement this type of functionality? Are there any open source solutions that do what I describe?

I will suggest you to NSAttributedString on multiline UILabels. Starting from iOS 6 UILabels by default supports attributed strings without any modifications.
NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:#"Some text and some more %#", dynamicString]];
//Change background color to highlight a sentence, by giving sentence's range
[str addAttribute:NSBackgroundColorAttributeName value:[UIColor yellowColor] range:NSMakeRange(sentenceStartIndex, sentenceLength)];
[label setAttributedText:str];
EDIT: It turns out UITextViews also support NSAttributedString I didn't try that, so you may replace UILabels with UITextViews.

I recommend using TTTAttributedLabel from Matt Thompson. It supports attributed strings, works all the way back to iOS 4, and is MIT licensed.

Related

Why is UILabel with attributed string from a Storyboard localization not supported?

I'm puzzled why UILabels using attributed strings are not translated when using the Storyboard/Xib - strings localization approach. I know I can use code to set it at runtime but I would like to use the default mechanism like for all other controls which don't use attributed strings.
I found questions to my exact same problem like those: 1, 2, 3, 4
But in non of those questions and answer I see no official Apple statement/documentation link why this shouldn't work (Why shouldn't it?).
So, I'm asking why is UILabel with attributed string from a Storyboard localization not supported?
And if there is no hint I would still like to know:
How to localize an UILabel with attributed string from a Storyboard?
Update
I also created a question in the apple developer forum in the hope to get an official answer.
Apple responded on the developer forum with
This is not currently supported in Storyboard/XIB. I would encourage you to make a request on Feedback Assistant.
and ended up using this approach, see below. I still hope Apple adds the reason behind this.
- (void)localizeAttributedLabel:(UILabel*)label withText:(NSString*)text
{
NSMutableAttributedString* attributedString = [[NSMutableAttributedString alloc] initWithAttributedString:label.attributedText];
[attributedString.mutableString setString:text];
label.attributedText = attributedString;
}
[self localizeAttributedLabel:myLabel withText:NSLocalizedString(#"MyLabelTextKey", nil)];

UITextView with voiceover

Here is my very simple code for creating a UITextView.
UITextView *textView = [[UITextView alloc] initWithFrame:self.view.bounds];
textView.editable = NO;
textView.text = #"Using iOS 3.0 and later, VoiceOver is available to help users with visual impairments use their iOS-based devices. The UI Accessibility programming interface, introduced in iOS 3.0, helps developers make their applications accessible to VoiceOver users. Briefly, VoiceOver describes an application’s user interface and helps users navigate through the application’s views and controls, using speech and sound. Users familiar with VoiceOver in Mac OS X can leverage their experience to help them quickly come up to speed using VoiceOver on their devices.";
[self.view addSubview:textView];
Given that I could not possibly do anything wrong here I am just wondering if this is an expected behaviour or a bug perhaps somebody also faced:
With voiceover enabled I expect the entire text view to be “highlighted” on tap, then its accessibilityLabel to be read to a user and after they double tap, the entire text view’s text to be read.
But what is happening is that a small portion of the text view is highlighted (usually 2 lines), accessibilityLabel is not read, but the first “highlighted" line and the first letter (!) of the second line are read instead and only after a user double taps the entire text is read.
Especially reading the first letter in the second highlighted line confuses me. Plus shouldn’t accessibilityLabel be always read in the beginning?
This looks like a big to me but Apple has always paid so much attention to accessibility, so I’m having doubts if I should report it, may be the meant it to be this way.
Another question: is there a way to achieve the following behaviour (without subleasing UITextView) when voiceover is enabled: user taps UITextView -> accessibilityLabel and the entire text are read?
In case someone else has this problem here is the answer:
textView.accessibilityTraits = UIAccessibilityTraitStaticText;
Combining the other two answers from this post has the desired effect. i.e.
textView.isAccessibilityElement = true
textView.accessibilityTraits = .staticText
Also if you are setting the attributedText property on the UITextView make sure you DO NOT set the accessibilityLabel (on the UITextView). Doing so will cause VoiceOver (Xcode 12.5, iOS 14.4.2) to read the text twice.
textView.isAccessibilityElement = true
This Works

Circled Latin Capital Letter M on iOS [duplicate]

I would like to get a down arrow to display inside a UILabel. Specifically ⬇ Unicode: U+2B07. This is show sort order on a column header.
I have seen the code to get unicode characters to display and when I use it for the symbol above it doesn't display as expected but rather comes up with a blue down arrow with gloss.
Has anyone seen this?
Displaying some characters as "Emojis" is a feature which is e.g. (controversially) discussed here: https://devforums.apple.com/message/487463#487463 (requires Apple Developer login). This feature was introduced in iOS 6.
A solution (from https://stackoverflow.com/a/13836045/1187415) is to append the Unicode "Variation selector" U+FE0E, e.g.
self.myLabel.text = #"\u2B07\uFE0E";
// or:
self.myLabel.text = #"⬇\uFE0E";
Note that on iOS <= 5.x, the Variation selector is not necessary and must not be used, because iOS 5 would display it as a box.
In Swift it would be
myLabel.text = "⬇\u{FE0E}"
If you're seeing the blue arrow with gloss, you have the right character, but it's showing one of the emoji-style characters. Try changing the font of your UILabel to something like Arial Unicode MS.
Edit After a little testing, it looks like changing the font doesn't actually work. It keeps displaying the glossy arrow. It's probably better to go with the suggestion of the other answer and use a different glyph, like \u2193, which does work:
[nameLabel setText:#"\u2193"];

Changing font size of attributed text in UITextView takes a lot of time in iOS 6 using Xcode 5

I am developing an application that target iOS6+.
I have a text view with with large attributed text. Maybe 1000 lines odd lines red color and other lines green color.
I use below code for changing the font size of the textview:
self.doaTextView.editable= YES;
self.doaTextView.font = [UIFont systemFontOfSize:self.FontSilder.value];
self.doaTextView.editable= NO;
but it takes much time. It is about 2 second on iOS 7 and about 5-10 second on iOS 6!!!
(I enable and disable editable feature, becasue if I do not do this the changes not appear in iOS 6. Please see here)
What is the problem?
Edit
I found this topic related to this problem too. Really there is not any solution for this?
I finally fixed this problem by recreating my attributedtext again and setting it as a new attributedtext to my uitextview.

Ensuring the symbol th in iOS appears correctly

I need to have 19th century in my UILabel. But it should appear as 19th century where the th symbol is a superscript. I am entering the label using plist. How do I ensure that it appears correctly in UI?
Need some guidance on this.
Image attached:
Use Unicode characters ᵗ (U+1D57) and ʰ (U+02B0).
You can enter these literally as:
NSString *num = #"19 ᵗʰ";
or:
NSString *num = #"19\u1d57\u02b0";
If those don't look nice enough, using an NSAttributedString would be best.
NSAttributedString has a property for that on osx, so perhaps it will be available on ios7. for now you could either store html in your plist and display that, or store 2 strings '19' and 'th' and manually add two uilabels into the top level one and manually layout them to create the superscript effect.

Resources