NSTextList in iOS - ios

Is there an equivalent for NSTextList on iOS?
On OSX i can do
NSTextList *list = [[NSTextList alloc] initWithMarkerFormat:#"square" options:0];
NSMutableParagraphStyle *paragraph = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
[paragraph setTextLists:[NSArray arrayWithObject:list]];
NSTextTab *t1 = [[NSTextTab alloc] initWithType:0 location:11.0f];
NSTextTab *t2 = [[NSTextTab alloc] initWithType:0 location:36.0f];
[paragraph setTabStops:[NSArray arrayWithObjects:t1, t2, nil]];
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:paragraph, NSParagraphStyleAttributeName, nil];
NSString *string = #"\t▪\tList item 1\n\t▪\tList item 2\n\t▪\tList item 3";
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:string attributes:attributes];
but there isn't an alternative for iOS, as all the solutions doesn't behave correctly

Related

How to justify label text?

My code to justify label text is given below:-
-(void)justifyToLabel:(UILabel*)label withStr:(NSString*)str{
NSMutableParagraphStyle *paragraphStyles = [[NSMutableParagraphStyle alloc] init];
paragraphStyles.alignment = NSTextAlignmentJustified;
NSDictionary *attributes = #{NSParagraphStyleAttributeName: paragraphStyles};
NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString: str attributes: attributes];
label.attributedText = attributedString;
}
I don't know that why its not working. Please give a best solution.
Thanks
Try the below code:
You have not assigned any value to firstLineHeadIndent property, that's why it is not working.
NSMutableParagraphStyle *paragraphStyles = [[NSMutableParagraphStyle alloc] init];
paragraphStyles.alignment = NSTextAlignmentJustified; //justified text
paragraphStyles.firstLineHeadIndent = 1.0; //must have a value to make it work
NSDictionary *attributes = #{NSParagraphStyleAttributeName: paragraphStyles};
NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString: string attributes: attributes];
label.attributedText = attributedString;

Highlighting particular text in UILabel

In my application I am displaying a text in my label like this
cell.lblUserDetails.text=[NSString stringWithFormat:#"%# and %# other likes your post", [dictionary valueForKey:#"username"], [dictionary valueForKey:#"CuriousCount"]];
Here I want to highlight Username and CuriousCount (in red color) and also username and CuriousCount should be clickable.
UIColor *color = [UIColor redColor];
NSDictionary *attrs = #{ NSForegroundColorAttributeName : color };
NSAttributedString *nameStr = [[NSAttributedString alloc] initWithString:[dictionary valueForKey:#"username"] attributes:attrs];
NSAttributedString *countStr = [[NSAttributedString alloc] initWithString:[dictionary valueForKey:#"CuriousCount"] attributes:attrs];
NSMutableAttributedString *string = [[NSMutableAttributedString alloc] init];
[string appendAttributedString:nameStr];
[string appendAttributedString:[[NSAttributedString alloc] initWithString:#" and "]];
[string appendAttributedString:countStr];
[string appendAttributedString:[[NSAttributedString alloc] initWithString:#" other likes your post"]];
cell.lblUserDetails.attributedText = string;

How to have 2 alignments in a UILabel

I am trying to create a UILabel where some of the text is aligned to the right and some of the text is aligned to the left. It is similar to the UITableViewCell with the small arrow:
I am trying to do it with NSAttributedString , but can't figure out what is the correct way to tackle this.
Here is some code which isn't working. It is aligned to the right.
NSMutableAttributedString *att = [[NSMutableAttributedString alloc] initWithString:#"Label >"];
NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];
paragraph.alignment = NSTextAlignmentLeft;
[att addAttribute:NSParagraphStyleAttributeName value:paragraph range:NSMakeRange(0, #"Label".length)];
NSMutableParagraphStyle *rightParagraph = [[NSMutableParagraphStyle alloc] init];
paragraph.alignment = NSTextAlignmentRight;
[att addAttribute:NSParagraphStyleAttributeName value:rightParagraph range:NSMakeRange(5, 1)];
You can use NSAttributedString to achieve your requirements, but it will be much better and cleaner approach to use two UILabels instead.
Use 2 labels.Assign the needed TextAlignment property to them. And after setting label text value, write this line :
[textLabel sizeToFit];
Though sizes of the labels varies it will set to minimum size. and will avoid text overlap.
I did it before with that code, Hope it also working for you.
NSString* alphaString = #“some text”;
NSMutableParagraphStyle* paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.alignment = NSTextAlignmentLeft;
NSMutableAttributedString * attributedString = [[NSMutableAttributedString alloc]
initWithString:alphaString
attributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIFont fontWithName:#"HelveticaNeue" size:13], NSFontAttributeName,
paragraphStyle, NSParagraphStyleAttributeName, nil]];
NSString * betaString = #“some other text”;
NSMutableParagraphStyle* paragraphStyle2 = [[NSMutableParagraphStyle alloc] init];
paragraphStyle2.alignment = NSTextAlignmentRight;
[attributedString appendAttributedString:[[NSAttributedString alloc] initWithString:betaString attributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:#"HelveticaNeue" size:13], NSFontAttributeName, paragraphStyle2, NSParagraphStyleAttributeName, nil]]];
yourLabel.attributedText = attributedString;

Replace occurrences of NSString with NSTextAttachment

I have a UITextView and I want to replace some strings with specific images using NSTextAttachment. I'm able to insert an image attachment to the UITextView and I have a method that, from the text view's attributed text, returns a NSString replacing attachments with specific NSString:
-(NSString*)getStringFromAttributedString{
__block NSString *str = self.attributedText.string; // Trivial String representation
__block NSMutableString *string = [NSMutableString new]; // To store customized text
[self.attributedText enumerateAttributesInRange:NSMakeRange(0, self.attributedText.length) options:0 usingBlock:^(NSDictionary *attributes, NSRange range, BOOL *stop){
// enumerate through the attributes
NSString *v;
NSObject *x = [attributes valueForKey:#"NSAttachment"];
if(x){ // YES= This is an attachment
v = [Fuctions getObjectTag:x];
if(v == nil){
v=#"";
}
}else{
v = [str substringWithRange:range]; // NO=This is a text block.
}
// append value to string
[string appendString:v];
}];
return string;
}
Now I need a method that returns a NSAttributedString from NSString, replacing some occurrences of string with image attachment, something like this but I'm not able to make it work.
-(NSMutableAttributedString*)getAttributedStringFromString:(NSString*)string{
// create NSMutableAttributedString from NSString in input
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:string];
// create the attachment with image
NSTextAttachment *textAttachment = [[NSTextAttachment alloc] init];
textAttachment.image = [UIImage imageNamed:#"myImageName.png"];
textAttachment.bounds = (CGRect) {0, -2, textAttachment.image.size};
// create NSMutableAttributedString with image attachment
NSMutableAttributedString *attrStringWithImage = [[NSAttributedString attributedStringWithAttachment:textAttachment] mutableCopy];
// here I'd like to replace all occurrences of a string (":D" this string for example) with my NSMutableAttributedString with image attachment).
return string;
}
How can I do?
Thanks, hope i explained myself.
This is my working solution:
-(NSMutableAttributedString*)getEmoticonStringFromString:(NSString*)string{
NSMutableAttributedString *final = [NSMutableAttributedString new]; //To store customized text
NSInteger len = [string length];
unichar buffer[len];
[string getCharacters:buffer range:NSMakeRange(0, len)];
for(int i = 1; i < len; i++) {
if(buffer[i-1] == ':' && buffer[i] == 'D'){
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:string];
NSTextAttachment *textAttachment = [[NSTextAttachment alloc] init];
textAttachment.image = [Functions scaleImage:[UIImage imageNamed:#"myImageName"] toSize:CGSizeMake(18.0f, 18.0f)];
textAttachment.bounds = (CGRect) {0, -2, textAttachment.image.size};
NSMutableAttributedString *attrStringWithImage = [[NSAttributedString attributedStringWithAttachment:textAttachment] mutableCopy];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init] ;
[paragraphStyle setAlignment:NSTextAlignmentLeft];
[paragraphStyle setLineSpacing:2.0f];
[attrString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [attrString length])];
[final insertAttributedString:attrStringWithImage atIndex:final.length];
i++;
}else{
[final insertAttributedString:[[NSAttributedString alloc] initWithString:[NSString stringWithFormat:#"%c", buffer[i-1]]] atIndex:final.length];
if(i == len-1){
[final insertAttributedString:[[NSAttributedString alloc] initWithString:[NSString stringWithFormat:#"%c", buffer[i]]] atIndex:final.length];
}
}
}
return final;
}

UITextView can't be selectable with textContainer

I am using UITextView with textContainer property and attributed string...
UITextView *textView = [[UITextView alloc] initWithFrame:textViewFrame
textContainer:textContainer];
But this textview became unSelectable.
I tried to subClass UITextView and return YES to canBecomeFirstResponder method but it still unSelectable..
how to fix this problem ?
This is Part of my code
NSString * decodedHtmlString = [[contentString stringByDecodingHTMLEntities] stringWithNewLinesAsBRs];
NSString * plainText = [decodedHtmlString stringByConvertingHTMLToPlainText];
NSData * dataString = [decodedHtmlString dataUsingEncoding:NSUnicodeStringEncoding allowLossyConversion:NO];
NSDictionary * attributedOptions = [NSDictionary dictionaryWithObjectsAndKeys:
NSHTMLTextDocumentType, NSDocumentTypeDocumentAttribute
, nil];
NSAttributedString * attributesString = [[NSAttributedString alloc] initWithData:dataString
options:attributedOptions
documentAttributes:nil
error:nil];
/////direction
NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.alignment = NSTextAlignmentRight;
NSMutableAttributedString * mutableAttrString = [[NSMutableAttributedString alloc] initWithAttributedString:attributesString];
[mutableAttrString addAttribute:NSFontAttributeName value:[UIFont fontWithName:#"Arial" size:29.0] range:NSMakeRange(0, plainText.length)];
[mutableAttrString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, plainText.length)];
NSTextStorage * textStorage = [[NSTextStorage alloc] initWithAttributedString:mutableAttrString];
NSLayoutManager * layoutManager = [[NSLayoutManager alloc] init];
[textStorage addLayoutManager:layoutManager];
NSUInteger lastRenderedGlyph = 0;
CGFloat currentXOffset = 0;
while (lastRenderedGlyph < layoutManager.numberOfGlyphs) {
CGRect textViewFrame = CGRectMake(currentXOffset, 0, 500, 700);
CGSize columnSize = textViewFrame.size;
NSTextContainer *textContainer = [[NSTextContainer alloc] initWithSize:columnSize];
[layoutManager addTextContainer:textContainer];
UITextView *textView = [[UITextView alloc] initWithFrame:textViewFrame
textContainer:textContainer];
[textView setSelectable:YES];
textView.scrollEnabled = NO;
currentXOffset += CGRectGetWidth(textViewFrame);
[self.scrollView addSubView:textView];
lastRenderedGlyph = NSMaxRange([layoutManager glyphRangeForTextContainer:textContainer]);
}
It seems that UITextView can't be selectable with using textContainer. So, as workaround we can retrieve attributedString from range of textContainer and then set it to the attributedText property of our UITextView.
NSAttributedString * subAttributedString = [mutableAttrString attributedSubstringFromRange:[layoutManager glyphRangeForTextContainer:textContainer]];
[textView setAttributedText: subAttributedString];
then the selection is working.
Try this
NSTextContainer *textContainer = [[NSTextContainer alloc] initWithSize:Size];
// create the UITextView for this column
UITextView *textView = [[UITextView alloc] initWithFrame:textViewFrame
textContainer:textContainer];

Resources