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;
Related
As question title, I want to truncate a part of NSAttriutedString.
Here's my code.
UIButton *btnWelcome = [UIButton buttonWithType:UIButtonTypeCustom];
[btnWelcome setContentMode:UIViewContentModeCenter];
[btnWelcome.titleLabel setNumberOfLines:1];
NSMutableParagraphStyle *titleTextParagrahStyle = [[NSMutableParagraphStyle alloc] init];
NSMutableParagraphStyle *specificNameParagrahStyle = [[NSMutableParagraphStyle alloc] init];
[titleTextParagrahStyle setAlignment:NSTextAlignmentCenter];
[specificNameParagrahStyle setAlignment:NSTextAlignmentCenter];
[specificNameParagrahStyle setLineBreakMode:NSLineBreakByTruncatingTail];
NSDictionary *titleTextAttributes = #{NSForegroundColorAttributeName:[UIColor whiteColor], NSFontAttributeName:[UIFont boldSystemFontOfSize:17.0f], NSParagraphStyleAttributeName:titleTextParagrahStyle};
NSDictionary *specificNameAttributes = #{NSForegroundColorAttributeName:[UIColor blueColor], NSFontAttributeName:[UIFont boldSystemFontOfSize:17.0f], NSParagraphStyleAttributeName:specificNameParagrahStyle};
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:#("Welcome To '%#' World!, 'VeryVeryLongSpeicificName'] attributes:titleTextAttributes];
NSRange specificNameRange = [attrString.string rangeOfString:[NSString stringWithFormat:#"'%#'", #"VeryVeryLongSpecificName"]];
specificNameRange.location++;
specificNameRange.length -= 2;
[attrString setAttributes:specificNameAttributes range:specificNameRange];
[btnWelcome setAttributedTitle:attrString forState:UIControlStateNormal];
The btnWelcome width is set to width of UIViewController.view
The Color Attribute of specificName is very well done.
But, setLineBreakMode:NSLineBreakByTruncatingTail doesn't apply.
Just shows below.
Welcome To 'VeryVeryLongSpec
The contents is just clipped.
The expectation result is below.
Welcome To 'VeryVeryLong...' World!
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;
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;
I have builed a button with two titles line by this code:
rootBntUI.titleLabel.font = [UIFont fontWithName:#"Avenir-Black" size:UserListFontSize];
[rootBntUI.layer setBorderWidth:0];
rootBntUI.titleLabel.textColor = [UIColor whiteColor];
rootBntUI.titleLabel.lineBreakMode = NSLineBreakByTruncatingTail;
rootBntUI.titleLabel.textAlignment = NSTextAlignmentCenter;
rootBntUI.titleLabel.numberOfLines = 2;
Everything is working fine but how can I control line spacing of button title?
You can do the styling from the xib . Use button title attributed in attribute inspector and you can set all the styling parameter along with spacing .
I have resolved my problem, and this solution for anyone who have similar question.
NSMutableParagraphStyle *style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
[style setAlignment:NSTextAlignmentCenter];
[style setLineBreakMode:NSLineBreakByWordWrapping];
[style setLineSpacing:-50];
UIFont *font1 = [UIFont fontWithName:#"Avenir-Black" size:UserListFontSize];
NSDictionary *dict1 = #{NSUnderlineStyleAttributeName:#(NSUnderlineStyleSingle),
NSFontAttributeName:font1,
NSParagraphStyleAttributeName:style};
NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] init];
[attString appendAttributedString:[[NSAttributedString alloc] initWithString:[NSString stringWithFormat:#"%#", obj] attributes:dict1]];
[FriendBnt setAttributedTitle:attString forState:UIControlStateNormal];
[[FriendBnt titleLabel] setNumberOfLines:0];
[[FriendBnt titleLabel] setLineBreakMode:NSLineBreakByWordWrapping];
Happy coding.
This works in Swift 2 using .lineHeightMultiple to compress the title text on a button.
let style = NSMutableParagraphStyle()
style.lineHeightMultiple = 0.8
style.alignment = .Center
style.lineBreakMode = .ByWordWrapping
let dict1:[String:AnyObject] = [
NSParagraphStyleAttributeName: style,
NSUnderlineStyleAttributeName: NSUnderlineStyle.StyleSingle.rawValue
]
let attrString = NSMutableAttributedString()
attrString.appendAttributedString(NSAttributedString(string: "Button Text here over two lines", attributes: dict1))
myButton.setAttributedTitle(attrString, forState: .Normal)
myButton.titleLabel?.numberOfLines = 0
What really worked for me to change line height of the UIButton title label, was this:
NSMutableParagraphStyle *style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
style.maximumLineHeight = 12.0;
style.minimumLineHeight = 12.0;
UIColor *colorO = [UIColor whiteColor];
UIColor *colorD = [UIColor redColor];
NSDictionary *firstAttributes = #{NSFontAttributeName : [UIFont fontWithName:#"HelveticaNeue-CondensedBold" size:getFloatScaledFactor(13.0)],
NSForegroundColorAttributeName : colorO,
NSParagraphStyleAttributeName:style
};
NSDictionary *secondAttributes = #{NSFontAttributeName : [UIFont fontWithName:#"HelveticaNeue-CondensedBold" size:getFloatScaledFactor(13.0)],
NSForegroundColorAttributeName : colorD,
NSParagraphStyleAttributeName:style
};
NSArray *textArray = [title componentsSeparatedByString:#"\n"];
NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] init];
[attString appendAttributedString:[[NSAttributedString alloc] initWithString:[NSString stringWithFormat:#"%#", textArray[0]] attributes:firstAttributes]];
[attString appendAttributedString:[[NSAttributedString alloc] initWithString:[NSString stringWithFormat:#"%#", textArray[1]] attributes:secondAttributes]];
[self.btnRight setAttributedTitle:attString forState:UIControlStateNormal];
As a alternative solution.
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];