UILabel Text line set as square shape arrange iOS - ios

I want to label Text arrange on square shape view format of label text line and label has some short word and some are Bigger size than other word of Label text so please see this Demo ImageSqaure shape Image

Try using NSAttributedString wish support multiple fonts and text sizes and use \n to return to the next line.
NSDictionary *italicAttrs = #{NSFontAttributeName : [UIFont fontWithName:#"HelveticaNeue-Bold" size:16], NSForegroundColorAttributeName : [UIColor colorWithRed:0.0 green:122.0/255.0 blue:1.0 alpha:1.0]};
NSDictionary *defaultAttrs = #{NSFontAttributeName : [UIFont fontWithName:#"HelveticaNeue-Medium" size:16], NSForegroundColorAttributeName : [UIColor blackColor]};
NSDictionary *arrowAttrs = #{NSFontAttributeName : [UIFont fontWithName:#"AlNile-Bold" size:20], NSForegroundColorAttributeName : [UIColor grayColor]};
NSMutableAttributedString *labelText = [[NSMutableAttributedString alloc] init];
NSAttributedString *from = [[NSAttributedString alloc] initWithString:#"From: " attributes:italicAttrs];
NSAttributedString *content = [[NSAttributedString alloc] initWithString:#"Me\n" attributes:defaultAttrs];
NSAttributedString *arrow = [[NSAttributedString alloc] initWithString:#"Ok" attributes:arrowAttrs];
[labelText appendAttributedString:from];
[labelText appendAttributedString:content];
[labelText appendAttributedString:arrow];

Related

Adding Outside Stroke to NSString in UITextView

I am trying to add a stroke to a text and showing in UITextView.
Here is what I want.
In the picture (CREATED FROM PHOTOSHOP) there are same font used, first one has Postion "OUTSIDE" and bottom text has position INSIDE.
I am using following code in Objective-C
NSShadow * shadow = [[NSShadow alloc] init];
shadow.shadowColor = [UIColor blackColor];
shadow.shadowOffset = CGSizeMake(0, 0);
NSDictionary * textAttributes =
#{ NSForegroundColorAttributeName : [UIColor whiteColor],
NSShadowAttributeName : shadow,
NSStrokeColorAttributeName : [UIColor blackColor],
NSStrokeWidthAttributeName : [NSNumber numberWithFloat:-3.0],
NSFontAttributeName : [UIFont fontWithName:#"UbuntuCondensed-Regular" size:40] };
textTitleResult.attributedText = [[NSAttributedString alloc] initWithString:textTitle.text
attributes:textAttributes];
I am achieving INSIDE effect. How can I set position to OUTSIDE
I don't think there's a property that can do exactly when you want. However, I have an idea which will possibly provide you with a way to "fake" it.
You can add another label at the exact position of the original one. The new label would have half the size of the stroke but the stroke color would be the same color as the text. Since the stroke "sits" on the border (half part inside the text and half part outside), it would block half of the original stroke, making it look as if it's on the outside.
Without making the code too pretty, it would look something like this:
UILabel *infoLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, 40)];
infoLabel.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:infoLabel];
UILabel *secondInfoLabel = [[UILabel alloc] initWithFrame:infoLabel.frame];
secondInfoLabel.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:secondInfoLabel];
NSDictionary * secondTextAttributes =
#{ NSForegroundColorAttributeName : [UIColor whiteColor],
NSStrokeColorAttributeName : [UIColor whiteColor],
NSStrokeWidthAttributeName : [NSNumber numberWithFloat:-3.0],
NSFontAttributeName : [UIFont systemFontOfSize:40] };
secondInfoLabel.attributedText = [[NSAttributedString alloc] initWithString:#"Welcome To" attributes:secondTextAttributes];
NSShadow * shadow = [[NSShadow alloc] init];
shadow.shadowColor = [UIColor blackColor];
shadow.shadowOffset = CGSizeMake(0, 0);
shadow.shadowBlurRadius = 2;
NSDictionary * textAttributes =
#{ NSForegroundColorAttributeName : [UIColor whiteColor],
NSShadowAttributeName : shadow,
NSStrokeColorAttributeName : [UIColor blackColor],
NSStrokeWidthAttributeName : [NSNumber numberWithFloat:-6.0],
NSFontAttributeName : [UIFont systemFontOfSize:40] };
infoLabel.attributedText = [[NSAttributedString alloc] initWithString:#"Welcome To" attributes:textAttributes];
Another option would be to create your own custom view and draw it yourself just the way you want it (in drawRect).

AttributedString in UINavigationBar not showing both lines of text

I am trying to set a two line label in my UINavigationBar. When I do the following, it only shows one line (the part before the line break).
NSString *title = #"First line of title is bigger\nSecond line of title is smaller";
NSDictionary *attribute1 = #{NSForegroundColorAttributeName: [UIColor colorWithRed:148/255.0f green:147/255.0f blue:147/255.0f alpha:1],
NSFontAttributeName: [UIFont boldSystemFontOfSize: 14.0f]};
NSDictionary *attribute2 = #{NSForegroundColorAttributeName: [UIColor colorWithRed:148/255.0f green:147/255.0f blue:147/255.0f alpha:1],
NSFontAttributeName: [UIFont boldSystemFontOfSize: 10.0f]};
const NSRange line1Range = {0, 29};
const NSRange line2Range = {31, 30};
NSMutableAttributedString *attributedText =[[NSMutableAttributedString alloc] initWithString:title];
[attributedText setAttributes: attribute1 range:line1Range];
[attributedText setAttributes: attribute2 range:line2Range];
UILabel *label = [[UILabel alloc] init];
label.attributedText=attributedText;
self.navigationItem.titleView = label;
[self.navigationItem.titleView sizeToFit];
For comparison, the following shows both line. The following is for comparison only. The top version is what I need.
UILabel *label = [[UILabel alloc] init];
label.backgroundColor = [UIColor clearColor];
label.numberOfLines = 2;
label.font = [UIFont boldSystemFontOfSize: 14.0f];
label.textColor = [UIColor colorWithRed:148/255.0f green:147/255.0f blue:147/255.0f alpha:1];
label.text = #"First line of title is bigger\nSecond line of title is smaller";
self.navigationItem.titleView = label;
[self.navigationItem.titleView sizeToFit];
Any help getting the top version to work correctly?
It appears that calling label.sizeToFit() before self.navigationItem.titleView = label displays the NSAttributedString title correctly.
(Swift, iOS8.1, XCode 6.1.1)
Easy problem, easy solution. Even if you are using attributedText instead of text, you still need to:
label.numberOfLines = 0;

NSAttributed String Won't change font [duplicate]

This question already has answers here:
UITextField attributedPlaceholder has no effect
(4 answers)
Closed 8 years ago.
I'm trying to format the placeholder text of a UITextField using an NSAttributedString. This code works successfully for the foreground color and kerning attributes. However, it won't change the font or the font size. What am I doing wrong? Thanks!
NSAttributedString *aString = [[NSAttributedString alloc] initWithString:
#"USER NAME"
attributes:#{
NSForegroundColorAttributeName: [UIColor redColor],
NSFontAttributeName : [UIFont fontWithName:#"Georgia" size:8.0f],
NSKernAttributeName : [NSNumber numberWithFloat:3.0f]
}];
self.userNameTextField.attributedPlaceholder = [aString copy];
For me it works when I set custom font for UITextField, and then set other attributes for placeholder. My example that works:
_searchField.font = [UIFont fontWithName:fontRokkitRegular size:20];
UIColor *color = [UIColor colorWithRed:150.0f/255 green:150.0f/255 blue:150.0f/255 alpha:1];
_searchField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:#"Keyword search" attributes:#{NSForegroundColorAttributeName:color}];
It looks like the placeholder font size is governed by the UITextField's font so I think you will have to subclass UITextField and add:
-(void)drawPlaceholderInRect:(CGRect)rect
{
NSString *aString = #"USER NAME";
[aString drawInRect:rect withAttributes:#{NSForegroundColorAttributeName: [UIColor redColor], NSFontAttributeName : [UIFont fontWithName:#"Georgia" size:8.0f], NSKernAttributeName : [NSNumber numberWithFloat:3.0f]}];
}

How to set correctly the line height of UILabel with an AttributedString?

I have a UILabel which uses an Attributed String. I want to have its line height to be exactly the size of the font size, not a pixel bigger. However, a top and bottom padding are being added. See image below:
This is my code to create the label.
NSDictionary *basicAttributes = #{NSForegroundColorAttributeName: [UIColor whiteColor], NSBackgroundColorAttributeName: [UIColor blueColor]};
NSMutableDictionary *attributes = [NSMutableDictionary dictionaryWithDictionary:basicAttributes];
UIFont *font = [UIFont fontWithName:#"AvenirNext-Regular" size:20.0];
[attributes setObject:font forKey:NSFontAttributeName];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineHeightMultiple = 1.0f;
[attributes setObject:paragraphStyle forKey:NSParagraphStyleAttributeName];
self.helloWorldLabel.attributedText = [[NSMutableAttributedString alloc] initWithString:#"Hola" attributes:attributes];
Attempts
I have tried to call sizeToFit after setting the attributedText without success.
[self.helloWorldLabel sizeToFit];
I have played with other attributes of NSMutableParagraphStyle such as lineSpacing without success.
paragraphStyle.lineSpacing = 0.0f;
What am I missing?
Thanks in advanced
It sounds like you want to use boundingRectWithSize: Here's a short example of how to use it. This code will dynamically determine your label size based on the text in your label. Setting the constraining sizes allows a max size if content needs to overflow to multiple lines.
NSString *text = [NSString stringWithFormat:#"Title Header:%#", value];
NSRange boldRange = [text rangeOfString:#"Title Header:"];
NSRange normalRange = [text rangeOfString:value];
NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithString:text];
//Add attributes for appropriate ranges
[attributedText setAttributes:#{ NSFontAttributeName:[UIFont fontWithName:#"HelveticaNeue-Bold" size:13.0f]} range:boldRange];
[attributedText setAttributes:#{ NSFontAttributeName:[UIFont fontWithName:#"HelveticaNeue" size:13.0f]} range:normalRange];
//Determine rect for attributed text constrained within max values
CGRect textAttributedSize = [attributedText boundingRectWithSize:CGSizeMake(CellTitleWidth, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading context:NULL];
[self.myLabel setText:attributedText];
[self.myLabel setFrame:textAttributedSize];
Check this link. There is no 100% way to correctly set the frame to fit the letters. You can calculate the attributed string with CoreText like using CTFramesetterRef CTFramesetterSuggestFrameSizeWithConstraints but it is more additional work.

How can I set the color and alignment of attributed text in a UITextView in iOS 7?

The formatting of my textViews worked fine in iOS 6, but no longer in iOS 7. I understand with Text Kit much of the under the hood stuff has changed. It's become really quite confusing, and I'm hoping someone can help straighten it out a bit by helping me with something as simple as this.
My static UITextView originally was assigned a value for it's textColor and textAlignment properties. Then I made a NSMutableAttributedString, assigned it an attributes, then assigned it to the textView's attributedText property. The alignment and color no longer take effect in iOS 7.
How can I fix this? If these properties take no effect, than why do they exist anymore? Here's the creation of the textView:
UITextView *titleView = [[UITextView alloc]initWithFrame:CGRectMake(0, 90, 1024, 150)];
titleView.textAlignment = NSTextAlignmentCenter;
titleView.textColor = [UIColor whiteColor];
NSMutableAttributedString *title = [[NSMutableAttributedString alloc]initWithString:#"Welcome"];
UIFont *font = [UIFont fontWithName:#"Avenir-Light" size:60];
[title addAttribute:NSParagraphStyleAttributeName value:font range:NSMakeRange(0, title.length)];
titleView.attributedText = title;
[self.view addSubview:titleView];
Curious, the properties are taken into account for UILabel but not for UITextView
Why don't you just add attributes for color and alignment to the attributed string similar to the way you are doing with the font?
Something like:
NSMutableAttributedString *title = [[NSMutableAttributedString alloc]initWithString:#"Welcome"];
UIFont *font = [UIFont fontWithName:#"Avenir-Light" size:60];
[title addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, title.length)];
//add color
[title addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:NSMakeRange(0, title.length)];
//add alignment
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setAlignment:NSTextAlignmentCenter];
[title addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, title.length)];
titleView.attributedText = title;
Edit: Assign the text first, then change the properties and this way it works.
UITextView *titleView = [[UITextView alloc]initWithFrame:CGRectMake(0, 90, 1024, 150)];
//create attributed string and change font
NSMutableAttributedString *title = [[NSMutableAttributedString alloc]initWithString:#"Welcome"];
UIFont *font = [UIFont fontWithName:#"Avenir-Light" size:60];
[title addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, title.length)];
//assign text first, then customize properties
titleView.attributedText = title;
titleView.textAlignment = NSTextAlignmentCenter;
titleView.textColor = [UIColor whiteColor];

Resources