How to have 2 alignments in a UILabel - ios

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;

Related

How can I truncate a part of NSAttributedString within a titleLabel property in UIButton?

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!

Swift : Remove empty white space in Aspect Fit ImageView

How to remove empty white space before and after some images that are small.
Some images are working well as the example below and some are with huge space above and below the image.
Am using Parse so I think I can't access imageView directly :
// upload Image To Parse as PFImageView
let imgFile = object?.objectForKey("ImgView") as? PFFile
cell.ImgView.file = imgFile
cell.questionImgView.loadInBackground()
// cell.questionImgView.clipsToBounds = true
cell. cell.ImgView.clipsToBounds.contentMode = UIViewContentMode.ScaleAspectFit
You can simply do the following to your UIButton to give it multiple lines and different fonts:
NSMutableParagraphStyle *style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
[style setAlignment:NSTextAlignmentCenter];
[style setLineBreakMode:NSLineBreakByWordWrapping];
UIFont *font1 = [UIFont fontWithName:#"HelveticaNeue-Medium" size:20.0f];
UIFont *font2 = [UIFont fontWithName:#"HelveticaNeue-Light" size:20.0f];
NSDictionary *dict1 = #{NSUnderlineStyleAttributeName:#(NSUnderlineStyleSingle),
NSFontAttributeName:font1};
NSDictionary *dict2 = #{NSUnderlineStyleAttributeName:#(NSUnderlineStyleNone),
NSFontAttributeName:font2};
NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] init];
[attString appendAttributedString:[[NSAttributedString alloc] initWithString:#"LINE 1\n" attributes:dict1]];
[attString appendAttributedString:[[NSAttributedString alloc] initWithString:#"line 2" attributes:dict2]];
[[self buttonToStyle] setAttributedTitle:attString forState:UIControlStateNormal];
[[[self buttonToStyle] titleLabel] setNumberOfLines:0];
[[[self buttonToStyle] titleLabel] setLineBreakMode:NSLineBreakByWordWrapping];
Source: iOS NSAttributedString on UIButton

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;

UIbutton title line spacing

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.

Equal spacing between lines of NSAttributedString with multiple font sizes for a UILabel in iOS 6

I have a multiple lines UILabel with attributed text.
All the lines in the text are of the same font, but each line is of a different font size.
I'm trying to achieve the exact same vertical space between each line.
However what is being displayed has variable spaces. It is as if something is adding a vertical margin to the font based on the font size.
CGFloat y = 0;
NSMutableAttributedString *attString=[[NSMutableAttributedString alloc] initWithString:#""];
NSArray *linesArray = [NSArray arrayWithObjects:#"One I\n",
#"Two I\n",
#"Three I\n",
#"Four I\n",
#"Five I\n", nil];
CGFloat fontSize = 10.0;
for(NSString *line in linesArray) {
NSMutableAttributedString *attributedLine = [[NSMutableAttributedString alloc] initWithString:line];
NSInteger stringLength=[line length];
[attributedLine addAttribute:NSFontAttributeName
value:[UIFont fontWithName:#"TimesNewRomanPSMT" size:fontSize]
range:NSMakeRange(0, stringLength)];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineSpacing = 0.0f;
paragraphStyle.alignment = NSTextAlignmentRight;
[attributedLine addAttributes:#{ NSParagraphStyleAttributeName : paragraphStyle} range:NSMakeRange(0, stringLength)];
[attString appendAttributedString:attributedLine];
fontSize += 10.0;
}
UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor blackColor];
label.numberOfLines = 0;
label.attributedText = attString;
[label sizeToFit];
CGRect newFrame = label.frame;
newFrame.size.width = self.view.frame.size.width - 40;
newFrame.origin.y = y;
newFrame.origin.x = 0;
label.frame = newFrame;
[self.view addSubview:label];
Any suggestions on the code I should use in order for it to display no space at all between each line of text?
I have been doing something similar, so maybe you could try something like this (typed in browser, watch out!):
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
[style setAlignment: NSTextAlignmentRight];
[style setLineSpacing:0];
for(NSString *line in linesArray) {
NSMutableParagraphStyle *subStyle = [style mutableCopy];
[subStyle setMaximumLineHeight:10]; // play around with this value <-----
NSDictionary *attributes =
#{
NSFontAttributeName : [UIFont fontWithName:#"TimesNewRomanPSMT" size:fontSize],
NSParagraphStyleAttributeName : paragraphStyle,
};
[attString appendAttributedString:[[NSAttributedString alloc] initWithString:line attributes: attributes]];
fontSize += 10.0;
}

Resources