How to display superscript % character as string in UIlabel? - ios

How to display superscript % character as string in UIlabel? I know % does not exist in unicode as a superscript but is there is any way we can display % as a superscript instead of using html tags??

I found this post on Stackoverflow on superscript styling text using attributed string:
NSAttributedString superscript styling
So using that, I hacked up this demo:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIFont *font = [UIFont fontWithName:#"Helvetica" size:20];
UILabel *textBlock1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height / 2.0)];
textBlock1.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1.0];
textBlock1.textAlignment = NSTextAlignmentCenter;
textBlock1.font = font;
textBlock1.text = #"57%";
UILabel *textBlock2 = [[UILabel alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height / 2.0, self.view.bounds.size.width, self.view.bounds.size.height / 2.0)];
textBlock2.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1.0];
textBlock2.textAlignment = NSTextAlignmentCenter;
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:#"57%"
attributes:#{NSFontAttributeName: font}];
[attributedString setAttributes:#{NSFontAttributeName : [UIFont fontWithName:#"Helvetica" size:10]
, NSBaselineOffsetAttributeName : #10} range:NSMakeRange(2, 1)];
textBlock2.attributedText = attributedString;
[self.view addSubview:textBlock1];
[self.view addSubview:textBlock2];
}
The result:

For a simple to use Swift solution, you might want to checkout HandyUIKit. After importing it into your project (e.g. via Carthage – see instructions in README) you can do something like this:
import HandyUIKit
"57^{%}".superscripted(font: UIFont.systemFont(ofSize: 20, weight: .medium))
This line will return an NSAttributedString which will look exactly like what you're looking for. Just assign it to a UILabels attributedText property and that's it!
If you're looking for subscripting a text, simply use subscripted(font:) instead. It will recognize structures like CO_{2}. There's also superAndSubscripted(font:) if you want to combine both.
See the docs for more information and additional examples.

Related

NSAttributedString with sub/superscript on iOS 13 [duplicate]

Trying to display super/subscript text using NSAttributedString in a UITextView seems broken in iOS13 - unless anyone knows otherwise?
Curiously if I use the UIFont systemFont then it works - but if I use any other font it doesn't.
See below for my code to setup a UITextView in my test app.
- (void)viewDidLoad
{
[super viewDidLoad];
UIFont* font = [UIFont systemFontOfSize:32];
//UIFont* font = [UIFont fontWithName:#"Helvetica Neue" size:32];
//UIFont* font = [UIFont fontWithName:#"Courier" size:32];
//UIFont* font = [UIFont fontWithName:#"Arial" size:32];
NSMutableAttributedString* as = [[NSMutableAttributedString alloc] initWithString:#"Super2Script" attributes:#{NSFontAttributeName : font}];
[as addAttribute:(NSString*)kCTSuperscriptAttributeName value:#(1) range:NSMakeRange(5, 1)];
UITextView* tv = [[UITextView alloc] initWithFrame:CGRectZero];
tv.attributedText = as;
[tv sizeToFit];
[self.view addSubview:tv];
tv.center = CGPointMake(self.view.bounds.size.width / 2, self.view.bounds.size.height / 2);
}
Simple 'fix' for this one.
It appears kCTSuperscriptAttributeName no longer works in iOS13 (for non-system fonts.) You need to use NSSuperscriptAttributeName instead. No idea where the definition for this lives (which header) so the actual string value required is "NSSuperScript"

UITextView text give small gap between each word in middle of text

Team,
I have UITextView added content of text, with font family avinar roma, I noticed there is small gap in between each word which was not consistent. How to avoid the small gap in textview text for each word.
descriptionTextView = [[UITextView alloc] initWithFrame:CGRectMake(20, titlelabel.frame.origin.y + titlelabel.frame.size.height + 5, view.frame.size.width - 40, view.frame.size.height - 150)];
descriptionTextView.backgroundColor = [UIColor clearColor];
descriptionTextView.textColor = MH_PANTONE_444;
descriptionTextView.textAlignment = NSTextAlignmentCenter;
descriptionTextView.font = [UIFont fontWithName:MH_FONT_AVENIRROMAN size:MH_SCREEN_HEIGHT/48];
descriptionTextView.text = [descriptionArray objectAtIndex:index];
descriptionTextView.editable = NO;
descriptionTextView.scrollEnabled = NO;
descriptionTextView.selectable = YES;
descriptionTextView.dataDetectorTypes = UIDataDetectorTypeAll;
Please have image attached above
University Medical Center, is not same gap with remaining text.
Setting the hyphenationFactor should fix your problem. But you have to use NSAttributedString instead of plain text:
UIFont *font = [UIFont fontWithName: MH_FONT_AVENIRROMAN size: MH_SCREEN_HEIGHT/48];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.hyphenationFactor = 1.0;
NSDictionary *attributes = #{ NSFontAttributeName:font,
NSForegroundColorAttributeName: MH_PANTONE_444;
NSParagraphStyleAttributeName: paragraphStyle };
NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithString: [descriptionArray objectAtIndex:index]
attributes: attributes];
descriptionTextView.attributedText = attributedText;
https://developer.apple.com/reference/uikit/nsmutableparagraphstyle/1535553-hyphenationfactor
You may have to adjust the hyphenationFactor until you get the result you want. (0.0-1.0)
I solved the same issue by setting the leftView property of the UITextField to be an empty view with the size of the padding as desired:
UIView *paddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 20)];
textView.leftView = paddingView;
textView.leftViewMode = UITextFieldViewModeAlways;
I had also face an issue of text alignment with custom fonts in iOS application, and that was with vertical alignment. I guess this is because of the custom font you are using in your application. But luckily there is a solution.
Every font file have some configurations about display the letter. One of the property is minLeftSideBearing for left spacing and minRightSideBearing for right spacing. I guess by changing them you can solve this spacing issue.
Follow the following link to check how to change this properties.
http://www.andyyardley.com/2012/04/24/custom-ios-fonts-and-how-to-fix-the-vertical-position-problem/
And as mention in above blog you need to install font tool. That you can download from following link.
https://developer.apple.com/download/more/?=font
Hope this might help you.
Thanks, Jay.

I have a text view created dynamically no outlet ,I need to change the textview text colour.How could i do this?

I have a text view created dynamically no outlet ,I need to change the textview text colour.i have set dynamically the selectable "True",But the text is always showing in black colour don't know why. I have reached every where in google but not lucky to find the answer.
UITextView *view = [[UITextView alloc]initWithFrame:CGRectMake(50, 100, 100, 100)];
view.textColor = [UIColor redColor];
view.userInteractionEnabled = YES;
[self.view addSubview:view];
Please set text color for each UITextView which you created dynamically :
SomeTextView.textColor = [UIColor redColor];
Hope This will help you !!!
UITextView *txtView = [[UITextView alloc]initWithFrame:CGRectMake(10, 100, 100, 100)];
txtView.textColor = [UIColor blueColor];
[self.view addSubview:txtView];
If u want to display the colour for particular words in textview u can try
NSMutableAttributedString *stringText = [[NSMutableAttributedString alloc] initWithString:#"This is awesome"];
//Green color for the first four characters.
[stringText addAttribute: NSForegroundColorAttributeName value: [UIColor greenColor] range: NSMakeRange(0, 4)];
// Sets the font color of last four characters to yellow.
[stringText addAttribute: NSForegroundColorAttributeName value: [UIColor yellowColor] range: NSMakeRange(14, 10)];
You can achieve this by doing it programatically. Use NSMutableAttributedString
NSMutableAttributedString *stringText = [[NSMutableAttributedString alloc] initWithString:#"This is awesome"];
//Green color for the first four characters.
[stringText addAttribute: NSForegroundColorAttributeName value: [UIColor greenColor] range: NSMakeRange(0, 4)];
// Sets the font color of last four characters to yellow.
[stringText addAttribute: NSForegroundColorAttributeName value: [UIColor yellowColor] range: NSMakeRange(14, 10)];
Set this text to your UITextView. It should work. Also make sure if text changes dynamically you need to care full with NSMakeRange.
Simplest way for changing textview text color is
UITextView *txtviewTextColor = [[UITextView alloc]init];
txtviewTextColor.frame = CGRectMake(10, 30, 200, 100);
txtviewTextColor.scrollEnabled = YES;
txtviewTextColor.textColor =[UIColor greenColor];
txtviewTextColor.userInteractionEnabled = YES;
[self.view addSubview:txtviewTextColor];

UILabel size incorrect for single line of text with lineSpacing and multiple colors

I'm pretty sure this is actually a UIKit bug but want to get some input to see if I'm missing something silly here.
Here is the code I have:
// single line with modified line spacing and 2 colors - broken, line spacing is added to the bottom!
UILabel *brokenLabel = [[UILabel alloc] init];
brokenLabel.backgroundColor = [UIColor greenColor];
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:#"Just some text"];
[attributedString addAttributes:#{NSForegroundColorAttributeName : [UIColor redColor]} range:[attributedString.string rangeOfString:#"text"]];
attributedString = attributedStringFromAttributedStringWithLineSpacing(attributedString, 20, NSTextAlignmentCenter);
brokenLabel.attributedText = attributedString;
[brokenLabel sizeToFit];
brokenLabel.frame = CGRectOffset(brokenLabel.frame, 50, 100);
[self.view addSubview:brokenLabel];
// end
// single line with modified line spacing and 1 color - correct
UILabel *workingLabel = [[UILabel alloc] init];
workingLabel.backgroundColor = [UIColor greenColor];
attributedString = [[NSMutableAttributedString alloc] initWithString:#"Just some text"];
attributedString = attributedStringFromAttributedStringWithLineSpacing(attributedString, 20, NSTextAlignmentCenter);
workingLabel.attributedText = attributedString;
[workingLabel sizeToFit];
workingLabel.frame = CGRectOffset(workingLabel.frame, 200, 100);
[self.view addSubview:workingLabel];
//end
// multiple lines with modified line spacing and 1 color - correct
UILabel *workingLabel2 = [[UILabel alloc] init];
workingLabel2.frame = CGRectMake(0, 0, 100, 0);
workingLabel2.numberOfLines = 0;
workingLabel2.backgroundColor = [UIColor greenColor];
attributedString = [[NSMutableAttributedString alloc] initWithString:#"Just some text"];
attributedString = attributedStringFromAttributedStringWithLineSpacing(attributedString, 20, NSTextAlignmentCenter);
workingLabel2.attributedText = attributedString;
[workingLabel2 sizeToFit];
workingLabel2.frame = CGRectOffset(workingLabel2.frame, 50, 300);
[self.view addSubview:workingLabel2];
//end
// multiple lines with modified line spacing and 2 color - correct
UILabel *workingLabel3 = [[UILabel alloc] init];
workingLabel3.frame = CGRectMake(0, 0, 100, 0);
workingLabel3.numberOfLines = 0;
workingLabel3.backgroundColor = [UIColor greenColor];
attributedString = [[NSMutableAttributedString alloc] initWithString:#"Just some text"];
[attributedString addAttributes:#{NSForegroundColorAttributeName : [UIColor redColor]} range:[attributedString.string rangeOfString:#"text"]];
attributedString = attributedStringFromAttributedStringWithLineSpacing(attributedString, 20, NSTextAlignmentCenter);
workingLabel3.attributedText = attributedString;
[workingLabel3 sizeToFit];
workingLabel3.frame = CGRectOffset(workingLabel3.frame, 200, 300);
[self.view addSubview:workingLabel3];
along with a simple convenience function to change the lineSpacing of an attributed string:
NSMutableAttributedString *attributedStringFromAttributedStringWithLineSpacing(NSAttributedString *string, CGFloat lineSpacing, NSTextAlignment textAlignment)
{
NSMutableAttributedString *mutable = string.mutableCopy;
NSMutableParagraphStyle *par = [NSMutableParagraphStyle new];
par.alignment = textAlignment;
par.lineSpacing = lineSpacing;
[mutable addAttributes:#{NSParagraphStyleAttributeName : par} range:NSMakeRange(0, mutable.length)];
return mutable;
}
However, this is what it looks like
As you can see, the height of the first label is way too big (or the height that it should be + my custom line spacing, to be precise). When simply adding another color to the first attributed string, it causes the sizeToFit size to increase by adding the lineSpacing below it. I also tried using the boundingRectWithSize: methods on the strings directly and the same issue is visible. So this is not specific to the label sizing code but is an issue with the strings themselves. I don't see any possible reason why this should be happening. Does anyone have any insight?
In your Attributes Dictionary add
[attrDic setObject:#0 forKey:NSBaselineOffsetAttributeName];

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