Adding Outside Stroke to NSString in UITextView - ios

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).

Related

Navigation Title Chopped with external Font 'FuturaStd-Book'

Title chopped in Navigation with external Font 'FuturaStd-Book'.
Tried to decreased Font size with following code but still title chopped.
[[UINavigationBar appearance] setTitleTextAttributes:#{NSFontAttributeName:[UIFont fontWithName:#"FuturaStd-Book" size:16]}];
Kindly help me on this...
Following code to Align the navigationItem title. (Only Works from iOS 11 - but the Navigation back and Title y position differs )
NSMutableParagraphStyle *style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
style.alignment = NSTextAlignmentCenter;
style.firstLineHeadIndent = 1.0f;
style.lineSpacing = 2; //Change spacing between lines
style.paragraphSpacing = 2; //Change space between paragraphs
[[UINavigationBar appearance] setTitleTextAttributes:#{NSForegroundColorAttributeName : [UIColor blackColor],
NSBackgroundColorAttributeName:[UIColor clearColor],
NSFontAttributeName:[UIFont fontWithName:#"FuturaStd-Book" size:18],
NSParagraphStyleAttributeName: style }];
You've to use the titleView of the navigationItem. Try this in your VC:
UILabel * headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 400.0, 44.0)];
headerLabel.textAlignment = NSTextAlignmentCenter;
headerLabel.backgroundColor = [UIColor clearColor];
headerLabel.text = #"Register";
headerLabel.font = [UIFont fontWithName:#"FuturaStd-Book" size:16];
headerLabel.textColor = [UIColor blackColor];
self.navigationItem.titleView = headerLabel;
Output:

UILabel Text line set as square shape arrange 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];

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;

How to get text for in self drawn view from viewcontroller

I've got self drawn view with text:
NSString* text = #"text";
UIFont* font = [UIFont systemFontOfSize:14.f];
NSShadow* shadow = [[NSShadow alloc] init];
shadow.shadowOffset = CGSizeMake(1, 1);
shadow.shadowColor = [UIColor whiteColor];
shadow.shadowBlurRadius = 0.5;
NSDictionary* attirbutes =
[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor grayColor], NSForegroundColorAttributeName,
font, NSFontAttributeName,
shadow, NSShadowAttributeName, nil];
CGSize textSize = [text sizeWithAttributes:attirbutes];
CGRect textRect = CGRectMake(0,
0,
textSize.width, textSize.height);
textRect = CGRectIntegral(textRect);
[text drawInRect:textRect withAttributes:attirbutes];
In my ViewController I want to get it from another class and draw it in my selfDrawnView. How Can I perform it?
Not really clear what exactly are you asking. If you subclassed UIView with your new view class and overridden drawRect method there you need to add a property to hold the text you're drawing.
Then in your view controller you just pass the text to that property and call [myCustomView setNeedDisplay] to force redrawing.

Effect in Text - objective-c

I have the sources of these letters and I am trying to create the gray effect around in objective-c.
But I can not, does anyone have an idea how to do this?
Take a look at GSBorderLabel.
It's good because it adds outer border and not inner border on characters and it's very easy to customise it.
For example:
GSBorderLabel *myLabel = [[GSBorderLabel alloc] initWithFrame:CGRectMake(0, 50, 300, 100)];
myLabel.textColor = [UIColor yellowColor];
myLabel.textAlignment = NSTextAlignmentCenter;
myLabel.borderColor = [UIColor grayColor];
myLabel.borderWidth = 20;
myLabel.text = #"Review";
myLabel.font = [UIFont fontWithName:#"Party LET" size:60.0];
If you're targeting iOS 6 and up, there isn't much over head for this. You can simply use NSAttributedString to add a stroke to an existing font. Mind you, this will not add the stroke to the font, just render it on screen.
NSDictionary *attributes = #{NSStrokeColorAttributeName: [UIColor grayColor], NSStrokeWidthAttributeName : #3};
NSString *inputText = #"Some text";
NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:inputText attributes:attributes];
[self.someLabel setAttributedText:attributedString];
You can try this:
#import <QuartzCore/QuartzCore.h>
....
titleLabel.layer.shadowColor = [[UIColor grayColor] CGColor];
titleLabel.layer.shadowRadius = 3;
titleLabel.layer.shadowOpacity = 1;
titleLabel.layer.shadowOffset = CGSizeMake(0, 0);

Resources