objective c - HTML to NSAttributedString - objective-c

I have written a function that converts HTML text to NSAttributedString. It is working fine. However, I have noticed that some tags when nested inside another tag, their fonts get overwritten.
Here's my code.
+(NSMutableAttributedString*) replaceHTMLTags : (NSString*) text : (NSString*) fontName : (CGFloat) fontSize
{
UIFont* font = [UIFont fontWithName:fontName size:fontSize];
NSMutableParagraphStyle* paragraphStyle = [[NSMutableParagraphStyle alloc]init];
paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
paragraphStyle.alignment = NSTextAlignmentJustified;
text = [text stringByReplacingOccurrencesOfString:#"<br>" withString:#"\n"];
NSMutableAttributedString* finalText = [[NSMutableAttributedString alloc]initWithString:text];
[finalText setAttributes:#{NSFontAttributeName:font} range:NSMakeRange(0, [finalText string].length)];
finalText = [self recurseFunc:finalText :#"" : font : paragraphStyle];
return finalText;
}
+(NSMutableAttributedString*) recurseFunc : (NSMutableAttributedString*) text : (NSString*) tag : (UIFont*) font : (NSMutableParagraphStyle*) paragraphStyle
{
NSMutableAttributedString* finalText = text;
NSRange newOpenTagRange;
//RECURSE IF THERE ARE MORE TAGS
while((newOpenTagRange = [[text string] rangeOfString:#"<[^>]+>" options:NSRegularExpressionSearch]).location != NSNotFound)
{
NSString* openTagName = [[text string] substringWithRange:newOpenTagRange];
NSString* closeTagName = [self getCloseTagName: openTagName];
NSRange newCloseTagRange = [[text string ]rangeOfString:closeTagName];
if(newCloseTagRange.location != NSNotFound)
{
NSString* textWithTags = [[text string] substringWithRange:NSMakeRange(newOpenTagRange.location, newCloseTagRange.location - newOpenTagRange.location + newCloseTagRange.length)];
NSString* newPlainText = [textWithTags stringByReplacingOccurrencesOfString:openTagName withString:#""];
newPlainText = [newPlainText stringByReplacingOccurrencesOfString:closeTagName withString:#""];
NSMutableAttributedString* newText = [[NSMutableAttributedString alloc]initWithString:newPlainText attributes:#{NSFontAttributeName:font, NSParagraphStyleAttributeName:paragraphStyle}];
newText = [self recurseFunc:newText :openTagName : font : paragraphStyle];
[finalText replaceCharactersInRange:NSMakeRange(newOpenTagRange.location, newCloseTagRange.location - newOpenTagRange.location + newCloseTagRange.length) withAttributedString:newText];
}
else
{
NSLog(#"Cannot find closing tag for tag %#", openTagName);
}
}
//FORMAT HTML TAGS
if([tag containsString:#"<p"])
{
[finalText.mutableString appendString:#"\n\n"];
}
else if ([tag isEqualToString:#"<i>"])
{
UIFont* italicFont = [UIFont fontWithName:#"Arial-ItalicMT" size:DEFAULT_FONT_SIZE];
[finalText addAttribute:NSFontAttributeName value:italicFont range:NSMakeRange(0, [finalText string].length)];
}
else if ([tag isEqualToString:#"<b>"])
{
UIFont* boldFont = [UIFont fontWithName:#"Arial-BoldMT" size:DEFAULT_FONT_SIZE];
[finalText addAttribute:NSFontAttributeName value:boldFont range:NSMakeRange(0, [finalText string].length)];
}
else if([tag isEqualToString:#"<ul>"])
{
NSMutableParagraphStyle* tempStyle = [[NSMutableParagraphStyle alloc]init];
tempStyle.headIndent = 30;
tempStyle.firstLineHeadIndent = 10;
tempStyle.lineBreakMode = NSLineBreakByWordWrapping;
tempStyle.alignment = NSTextAlignmentJustified;
NSString* temp = [[finalText string]stringByReplacingOccurrencesOfString:#"###" withString:#"•\t"];
temp = [NSString stringWithFormat:#"\n%#", temp];
[finalText setAttributedString:[[NSAttributedString alloc] initWithString:temp]];
[finalText addAttribute:NSParagraphStyleAttributeName value:tempStyle range:NSMakeRange(0, [finalText string].length)];
}
else if ([tag isEqualToString:#"<li>"])
{
NSMutableAttributedString* tempAS = [[NSMutableAttributedString alloc]initWithString:#"###$$$\n"];
NSRange r = [[tempAS string]rangeOfString:#"$$$"];
[tempAS replaceCharactersInRange:r withAttributedString:finalText];
[finalText setAttributedString:tempAS];
}
return finalText;
}
This does exactly what it is supposed to do, except for one specific case.
For instance, if I have a <b> or an <i> tag inside a <ul><li> tag, the <b> or <i> don't get rendered.

For converting HTML to NSAttributedString you can use the following code:
[[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUTF8StringEncoding]
options:#{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
NSCharacterEncodingDocumentAttribute: #(NSUTF8StringEncoding)}
documentAttributes:nil error:nil];

Related

How to format UITextField with NSNumberFormatter?

Hope this helps others, since I haven't seen any similar post on the web that shows how to format a text field using NSNumberFormatter but at the same time keep the UITextField cursor position to where it should naturally be. Those, because after formatting, the NSString from inside the UITextField and setting it back to the field you end up with the cursor placed an the end of the field.
Also it will be nice to convert it to Swift for those that needs it.
And here is my answer to the issue, I am using a UIKeyboardTypeNumberPad, but also it will work fine with a UIKeyboardTypeDecimalPad, if other keyboard types are used, feel free to add a regex before using the next code:
- (int)getCharOccurencies:(NSString *)character inString:(NSString *)string{
NSMutableArray *characters = [[NSMutableArray alloc] initWithCapacity:[string length]];
for (int i=0; i < [string length]; i++) {
NSString *ichar = [NSString stringWithFormat:#"%c", [string characterAtIndex:i]];
[characters addObject:ichar];
}
int count = 0;
for (NSString *ichar in characters) {
if ([ichar isEqualToString:character]) {
count++;
}
}
return count;
}
- (void)selectTextForInput:(UITextField *)input atRange:(NSRange)range {
UITextPosition *start = [input positionFromPosition:[input beginningOfDocument]
offset:range.location];
UITextPosition *end = [input positionFromPosition:start
offset:range.length];
[input setSelectedTextRange:[input textRangeFromPosition:start toPosition:end]];
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
NSString *charUsedToFormat = #",";
NSMutableString *textString = [NSMutableString stringWithString:[textField text]];
[textField setTintColor:[UIColor darkGrayColor]];
if ([string isEqualToString:charUsedToFormat]) {
return NO;
}
if (range.location == 0 && [string isEqualToString:#"0"]) {
return NO;
}
if ([[textString stringByReplacingCharactersInRange:range withString:string] length] == 0) {
textField.text = #"";
[self selectTextForInput:textField atRange:NSMakeRange(0, 0)];
return NO;
}
NSString *replacebleString = [textString substringWithRange:range];
if (string.length == 0 && [replacebleString isEqualToString:charUsedToFormat]) {
NSRange newRange = NSMakeRange( range.location - 1, range.length);
range = newRange;
textString = [NSMutableString stringWithString:[textString stringByReplacingCharactersInRange:newRange withString:string]];
}else{
textString = [NSMutableString stringWithString:[textString stringByReplacingCharactersInRange:range withString:string]];
}
int commmaCountBefore = [self getCharOccurencies:charUsedToFormat inString:textString];
textString = [NSMutableString stringWithString:[textString stringByReplacingOccurrencesOfString:charUsedToFormat withString:#""]];
NSNumber *firstNumber = [NSNumber numberWithDouble:[textString doubleValue]];
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
//just in case
[formatter setFormatterBehavior:NSNumberFormatterBehavior10_4];
[formatter setGroupingSeparator:charUsedToFormat];
[formatter setDecimalSeparator:#""];
[formatter setMaximumFractionDigits:0];
textString = [NSMutableString stringWithString:[formatter stringForObjectValue:firstNumber]];
textField.text = textString;
int commmaCountAfter = [self getCharOccurencies:charUsedToFormat inString:textString];
int commaDif = commmaCountAfter - commmaCountBefore;
int cursorPossition = (int)range.location + (int)string.length + commaDif;
//set cursor position
NSLog(#"cursorPossition: %d", cursorPossition);
[self selectTextForInput:textField atRange:NSMakeRange(cursorPossition, 0)];
return NO;
}

NSMutableAttributedString Append in UITextView

I need to append 2 NSMutableAttributedString for my UITextview when user selection different words like the example.
string = #"blabla1 blabla2 blabla3"
in first time the user select #"blabla1"
and the text looks like that #"blabla1 blabla2 blabla3"
and after I select #"blabla3" the result I want to get in My UITextview is #"blabla1 blabla2 blabla3"
now the result I get is #"blabla1 blabla2 blabla3 blabla1 blabla2 blabla3"
that my code :
-(NSMutableAttributedString*)getNSMutableAttributedString:(NSString*)string withRange:(NSRange)range withTextView:(UITextView*)textView
{
if (!str)
{
str = [[NSMutableAttributedString alloc] initWithString:string];
UIFont *font = [UIFont boldSystemFontOfSize:16];
[str addAttribute:NSFontAttributeName value:font range:NSMakeRange(range.location, range.length)];
}
else
{
NSMutableAttributedString *mutableAttString = [[NSMutableAttributedString alloc] initWithString:string];
UIFont *font = [UIFont boldSystemFontOfSize:16];
[mutableAttString addAttribute:NSFontAttributeName value:font range:NSMakeRange(range.location, range.length)];
NSMutableAttributedString *first = str;
NSMutableAttributedString *second = mutableAttString;
NSMutableAttributedString* result = [first mutableCopy];
[result appendAttributedString:second];
str = result;
}
return str;
}
Attributes can be added multiply times to one string.And you create a new attributedString from string, which don't have attributes.
In result you receive #"blabla1 blabla2 blabla3 blabla1 blabla2 blabla3"
-(NSMutableAttributedString*)getNSMutableAttributedString:(NSMutableAttributedString*)string withRange:(NSRange)range withTextView:(UITextView*)textView
{
if (!str)
{
str = [[NSMutableAttributedString alloc] initWithAttributedString:string];
UIFont *font = [UIFont boldSystemFontOfSize:16];
[str addAttribute:NSFontAttributeName value:font range:NSMakeRange(range.location, range.length)];
}
else
{
UIFont *font = [UIFont boldSystemFontOfSize:16];
[str addAttribute:NSFontAttributeName value:font range:NSMakeRange(range.location, range.length)];
}
return str;
}
What you should be doing is get str (the existing attributedText from the UITextView) and then add an attribute to the specific range
str = [textView attributedText];
UIFont *font = [UIFont boldSystemFontOfSize:16];
[str addAttribute:NSFontAttributeName value:font range:NSMakeRange(range.location, range.length)];
return str;
What you are doing is creating a new attributed string with the same content but different attributes and then appending to the existing attributedText. That is why you see the text repeated twice.

Changing the width of the space character in NSTextView

I’m trying to make a reader application to help a girl with reading difficulties. Some research shows that just changing the colors of the text, background and shadow can really help kids out so I’m trying to allow her to do that. It’s just a big NSTextView with buttons so she can change the font size, color, background color, shadow properties, letter spacing, line spacing and word spacing. I know you can do most of this just using Word but I’m trying to make it as intuitive/fun as possible for her.
The place where I could use a hand is in changing the size of the spacing between words. Currently I’m just searching for a string of spaces equal to the number of spaces I expect to be there and then replacing with more or less spaces it as follows:
- (IBAction)increaseSpacing:(id)sender{
NSInteger spacing = [[NSUserDefaults standardUserDefaults] integerForKey:#"wordSpacing"];
NSMutableString * oldString = [ NSMutableString stringWithCapacity:0];
NSMutableString * newString =[ NSMutableString stringWithCapacity:0];
for (int i = 0; i < spacing; i+=1) {
[oldString appendString:#" "];
}
[newString setString:oldString];
[newString appendString:#" "];
[[[textView textStorage] mutableString] replaceOccurrencesOfString:oldString
withString:newString options:0
range:NSMakeRange(0, [[textView textStorage] length])];
spacing += 1;
[[NSUserDefaults standardUserDefaults] setValue:[NSNumber numberWithInteger: spacing] forKey:#"wordSpacing"];
}
- (IBAction)reduceSpacing:(id)sender{
NSInteger spacing = [[NSUserDefaults standardUserDefaults] integerForKey:#"wordSpacing"];
if (spacing > 1) {
NSMutableString * oldString = [ NSMutableString stringWithCapacity:0];
NSMutableString * newString =[ NSMutableString stringWithCapacity:0];
for (int i = 0; i < spacing-1; i+=1) {
[newString appendString:#" "];
}
[oldString setString:newString];
[oldString appendString:#" "];
[[[textView textStorage] mutableString] replaceOccurrencesOfString:oldString
withString:newString options:0
range:NSMakeRange(0, [[textView textStorage] length])];
spacing -= 1;
[[NSUserDefaults standardUserDefaults] setValue:[NSNumber numberWithInteger: spacing] forKey:#"wordSpacing"];
}
}
This approach feels sloppy to me, especially when moving the cursor around with arrow keys. I could just change the font size of a space character when it’s typed, but that would also change the line height. Is there a way that I can just change the width of the space character? Thanks in advance for your help.
My eventual solution was to swap out spaces for blank images (blanks) that have the adjusted width.
Basic components:
a) Method to replace spaces with blanks
b) Method to replace blanks with spaces
c) NSValueTransformer for the NSTextView to do (a) for transformedValue and (b) for reverseTransformedValue
d) NSTextViewDelegate to do (a) when the text changes
e) Subclass NSTextView to do (b) on copied or cut text before sending to pasteboard
f) Action assigned to the stepper to make the size changes
Code for each part is below:
a) AppDelegate method to replace spaces with blanks
- (NSAttributedString * ) replaceSpacesWithBlanks:(NSString *)replaceString {
CGFloat imageWidth = [[NSUserDefaults standardUserDefaults] integerForKey:#"wordSpacing"];
NSImage * pic = [[NSImage alloc] initWithSize:NSMakeSize(imageWidth, 1.0f)];
NSTextAttachmentCell *attachmentCell = [[NSTextAttachmentCell alloc] initImageCell:pic];
NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
[attachment setAttachmentCell: attachmentCell ];
NSAttributedString *replacementString = [NSAttributedString attributedStringWithAttachment: attachment];
NSMutableAttributedString *mutableString = [[NSMutableAttributedString alloc] initWithString:replaceString];
NSRange range = [[mutableString string] rangeOfString:#" "];
while (range.location != NSNotFound) {
[mutableString replaceCharactersInRange:range withAttributedString:replacementString];
range = [[mutableString string] rangeOfString:#" "];
}
return [[NSAttributedString alloc] initWithAttributedString: mutableString];
}
b) AppDelegate method to replace blanks with spaces
- (NSString * ) replaceBlanksWithSpaces:(NSAttributedString *)replaceAttributedString {
NSMutableAttributedString * mutAttrString = [[NSMutableAttributedString alloc] initWithAttributedString:replaceAttributedString];
for (int index = 0; index < mutAttrString.length; index += 1) {
NSRange theRange;
NSDictionary * theAttributes = [mutAttrString attributesAtIndex:index effectiveRange:&theRange];
NSTextAttachment *theAttachment = [theAttributes objectForKey:NSAttachmentAttributeName];
if(theAttachment != NULL) {
[mutAttrString replaceCharactersInRange:theRange withString:#" "];
}
}
return mutAttrString.string;
}
c) NSValueTransformer for the NSTextView to replace spaces with blanks for transformedValue and replace blanks with spaces for reverseTransformedValue
#implementation DBAttributedStringTransformer
- (id)init
{
self = [super init];
if (self) {
appDelegate = (AppDelegate *)[[NSApplication sharedApplication] delegate];
}
return self;
}
+ (Class)transformedValueClass
{
return [NSAttributedString class];
}
+ (BOOL)allowsReverseTransformation
{
return YES;
}
- (id)transformedValue:(id)value
{
return [appDelegate replaceSpacesWithBlanks:value];
}
- (id)reverseTransformedValue:(id)value
{
return [appDelegate replaceBlanksWithSpaces:value];
}
d) NSTextViewDelegate to replace spaces with blanks when the text changes
#implementation DBTextViewDelegate
-(void)awakeFromNib {
appDelegate = (AppDelegate *)[[NSApplication sharedApplication] delegate];
}
- (void)textViewDidChangeSelection:(NSNotification *)aNotification{
// Need to keep track of where the cursor should be reinserted
textLength = myTextView.string.length;
insertionPoint = [[[myTextView selectedRanges] objectAtIndex:0] rangeValue].location;
}
//replaces spaces with blank image and puts cursor back in correct position
- (void)textDidChange:(NSNotification *)aNotification{
NSInteger newTextLength = myTextView.string.length;
NSInteger newInsertionPoint = insertionPoint + newTextLength - textLength;
NSString * stringValue = [[NSUserDefaults standardUserDefaults] stringForKey:#"textViewString"];
NSAttributedString * attrStringWithBlanks = [[ NSAttributedString alloc] initWithAttributedString:[appDelegate replaceSpacesWithBlanks:stringValue ]];
NSMutableAttributedString *mutableString = [[NSMutableAttributedString alloc] initWithAttributedString:attrStringWithBlanks];
[myTextView.textStorage setAttributedString: mutableString];
//Put the cursor back where it was
[myTextView setSelectedRange:NSMakeRange(newInsertionPoint, 0)];
}
e) Subclass NSTextView to replace blanks with spaces on copied or cut text before writing to pasteboard
#implementation DBTextView
-(void)awakeFromNib {
appDelegate = (AppDelegate *)[[NSApplication sharedApplication] delegate];
}
-(void) selectedTextToClipBoard{
NSRange selectedRange = [self selectedRange];
NSAttributedString * selectedText = [[self textStorage] attributedSubstringFromRange: selectedRange];
NSString * textWithoutBlanks = [appDelegate replaceBlanksWithSpaces:selectedText];
NSPasteboard *pasteboard = [NSPasteboard generalPasteboard];
[pasteboard clearContents];
NSArray *copiedObject = [NSArray arrayWithObject:textWithoutBlanks];
[pasteboard writeObjects:copiedObject];
}
-(void) copy:(id)sender{
[self selectedTextToClipBoard];
}
-(void) cut:(id)sender{
[self selectedTextToClipBoard];
// Delete selected text so it acts like a cut
NSRange selectedRange = [self selectedRange];
[[self textStorage] deleteCharactersInRange:selectedRange];
}
f) Action assigned to the stepper to make the size changes
- (IBAction)changeWordSpacing:(id)sender {
CGFloat imageWidth = [[NSUserDefaults standardUserDefaults] integerForKey:#"wordSpacing"];
NSImage * pic = [[NSImage alloc] initWithSize:NSMakeSize(imageWidth, 1.0f)];
NSTextAttachmentCell *attachmentCell = [[NSTextAttachmentCell alloc] initImageCell:pic];
NSMutableAttributedString * mutAttrString = [[NSMutableAttributedString alloc] initWithAttributedString:[textView textStorage]];
for (int index = 0; index < mutAttrString.length; index += 1) {
NSRange theRange;
NSDictionary * theAttributes = [mutAttrString attributesAtIndex:index effectiveRange:&theRange];
NSTextAttachment *theAttachment = [theAttributes objectForKey:NSAttachmentAttributeName];
if(theAttachment != NULL) {
[theAttachment setAttachmentCell: attachmentCell ];
}
}
[[textView textStorage] setAttributedString:mutAttrString];
}
Also, NSTextView should be set to “Continuously Updates Value”
It is possible to adjust the font kerning specifically for space characters. Here is a simple way to do that using the new AttributedString:
var searchRange = text.startIndex..<text.endIndex
while let range = text[searchRange].range(of: " ") {
text[range].mergeAttributes(AttributeContainer([.kern: 10]))
searchRange = range.upperBound..<text.endIndex
}
You may use text[range].kern = 10 if you are using SwiftUI's Text view, but as of Xcode 13.4 the SwiftUI.Kern attribute created in that way will not convert properly for NSAttributedStrings.

How to highlight search string in AutoComplete results for IOS5?

Google autocomplete would bolden what we search
For example: If we search for hell we'll see "hell o"
I think I need attributed string, so my code is:
- (NSMutableAttributedString*) highlightSearchString:(NSString*)substringToHighlight{
NSMutableAttributedString * mutableAttributedString = [[ NSMutableAttributedString alloc]initWithString:self];
NSUInteger count = 0, length = [mutableAttributedString length];
NSRange range = NSMakeRange(0, length);
count = 0,
length = [mutableAttributedString length];
range = NSMakeRange(0, length);
while(range.location != NSNotFound)
{
range = [[mutableAttributedString string] rangeOfString:substringToHighlight options:0 range:range];
if(range.location != NSNotFound) {
//[mutableAttributedString setTextColor:[UIColor blueColor] range:NSMakeRange(range.location, [word length])];
NSString *boldFontName = [[UIFont boldSystemFontOfSize:12] fontName];
NSDictionary * dict = #{NSFontAttributeName:boldFontName};
NSRange rangeHighlight = NSMakeRange(range.location, substringToHighlight.length);
[mutableAttributedString setAttributes:dict range:rangeHighlight];
range = NSMakeRange(range.location + range.length, length - (range.location + range.length));
count++;
}
}
return mutableAttributedString;
}
But it doesn't work because NSFontAttributeName is available only in iOS6.
After that I need to update the tableViewCell
cell.textLabel.text=text;
with something that take advantage of the atributed text.
Just use the CoreText definition for the font:
UIFont *font = [UIFont boldSystemFontOfSize:12];
CTFontRef ctFontRef = CTFontCreateWithName((__bridge CFStringRef)font.fontName, font.pointSize, NULL);
NSDictionary * dict = #{(NSString *)kCTFontAttributeName : (__bridge id) ctFontRef};
For the seconds problem:
The default UILabel in the iOS SDK only supports NSAttributedString from iOS 6. Thus in
iOS 5 you will either have to draw the NSAttributedString your self using CoreText of get some third part label that does support NSAttributedString like: TTTAttributedLabel.

Unable to add custom links to OHAttributeLabel

I am using OHAttributeLabel to add custom links to my label's text. The code that I am using is pasted below. It used to work with the older version of OHAttributed label (2010), however with the new version (recently updated), the text in my label are no longer clickable as links.
Can anyone advise what I am missing here?
// Set Question Label
Question *question = self._answerForCell.question;
NSString *questionText = [NSString stringWithFormat:#"Q: %#", question.text];
CustomOHAttributLabel *thisQuestionLabel = (CustomOHAttributLabel *)[self.contentView viewWithTag:QUESTIONLABEL_TAG];
//Set up dictionary for question
NSString *questionStr = [question.text stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
NSString *urlForQn = [NSString stringWithFormat:#"dailythingsfm://redirect_to/questions/%#/answers?text=%#&nickname=%#&question_id=%#&question_curious=%i&showEveryOneTab=%i", question.slug, questionStr, [[UserInfo sharedUserInfo] getNickname], question.qid, question.curious, 1];
NSString *qnStartIndex = #"0";
NSString *qnLength = [NSString stringWithFormat:#"%i", [questionText length]];
NSDictionary *qnDict = [NSDictionary dictionaryWithObjectsAndKeys:qnStartIndex, #"start", qnLength, #"length", urlForQn, #"url", nil];
NSArray *array = [NSArray arrayWithObject:qnDict];
[thisQuestionLabel setLabelwithText:questionText fontSize:QUESTION_FONT_SIZE andSubStringToURLArrayViaRange:array withHexColor:#"#555555"];
//Method to set the text in UILabel to a custom link
- (void)setLabelwithText:(NSString *)text fontSize:(CGFloat)fontSize andSubStringToURLArrayViaRange:(NSArray *)array withHexColor:(NSString *)textColor
{
NSMutableAttributedString *attrStr = [NSMutableAttributedString attributedStringWithString:text];
[attrStr setFont:[UIFont systemFontOfSize:fontSize]];
[attrStr setTextColor:[UIColor grayColor]];
[self removeAllCustomLinks];
for (NSDictionary *dict in array) {
NSString *start = [dict objectForKey:#"start"];
NSString *length = [dict objectForKey:#"length"];
NSString *url = [dict objectForKey:#"url"];
NSUInteger startIndex = [start intValue];
NSUInteger len = [length intValue];
NSRange range = NSMakeRange(startIndex, len);
[attrStr setFont:[UIFont boldSystemFontOfSize:fontSize] range:range];
[attrStr setTextColor:[UIColor colorWithHexString:textColor] range:range];
[self addCustomLink:[NSURL URLWithString:url] inRange:range];
}
self.attributedText = attrStr;
}
I have to use 'setLink' method instead of addCustomLink for the latest OHAttribute Library (3.2.1)