I want to have an NSString in the format "hh:mm:ss" to use exactly the same amount of pixel space as the string "88:88:88". Is that possible?
Now I'm using:
// ...
NSMutableString * strS = [[NSMutableString alloc] initWithFormat:#"%d", Seconds];
if (Seconds<10){
[strS insertString:#"0" atIndex:0];
}
// Make the time to show
[ClocklLabel setText:[NSString stringWithFormat:#"%2#:%2#:%2#", strH,strM,strS]];
with no success!
I think you're looking for a monospaced font. Try using Courier for example:
UIFont *courier = [UIFont fontWithName:#"Courier" size:12.0f];
label.font = courier;
If you're trying to create a digital clock display, you could also simply use a separate label for each number and position the labels however you want.
You could use a monospace font. If you do not want this, use a UILabel and set adjustsFontSizeToFitWidth to YES. However, in this case the height will vary.
The title of your question hints to the fact that you are missing an important detail. NSString only contains the actual text.
It is not the NSString that dictates font, color or other attributes. As other answers have suggested you should set the font to the label/textfield/button or whatever visual gadget is supposed to display the string.
If you want to combine both text and text attributes, you may want to check NSAttributedString.
Related
I am trying to change text font size with using NSAttributedString. But it's size doesn't change.
NSDictionary *attrDict = #{NSFontAttributeName : [UIFont boldSystemFontOfSize:22], NSForegroundColorAttributeName : [UIColor orangeColor]};
NSAttributedString *newAttString = [[NSAttributedString alloc] initWithString:mytext attributes:attrDict];
[result appendAttributedString:newAttString];
Only text color changes. Size of result string is not 22 and also it is not bold.
Instead of applying the attributes with the alloc, init, try doing it after with something like (with a mutable NSAttributedString):
NSMutableAttributedString *newAtt = [[NSMutableAttributedString alloc] initWithString:mytext]; // Allocate a new NSMutableAttributedString with `mytext`
[newAtt addAttributes:#{NSFontAttributeName : [UIFont boldSystemFontOfSize:20],
NSForegroundColorAttributeName: [UIColor orangeColor]}
range:NSMakeRange(0, [result length])]; // add new attributes for the length of the string `mytext`
[result setAttributedText:newAtt];
This answer would vary depending on what result is, I tested it on a UITextView and it worked fine, there is also an attributedText
property on UILabels.
Hope this helps.
You didn't mention what result means at the end of your code. Are you sure you want to "append" it?
Besides, I use this code for setting fonts
[UIFont fontWithName:#"Arial-BoldMT" size:22.0f]
This can be used to set different fonts and sizes respectively.
Hope this helps:)
I have a request from a customer to use a certain font in a iOS 7 project because it has medieval numbers.
Is there any way to activate those numbers for a NSAttributedString? As default lining numbers are used, that are included in the font as-well.
Here is an example. Both lines have the same font with no variant (Regular), once with medieval numbers activated, the second wit the default lining numbers.
These are called lowercase numbers and can be turned on using UIFontDescriptor.
First, you need to import CoreText for some constants:
#import <CoreText/SFNTLayoutTypes.h>
or
#import CoreText.SFNTLayoutTypes;
Then create font using font descriptor. Here I use Georgia family:
NSDictionary *lowercaseNumbers = #{
UIFontFeatureTypeIdentifierKey: #(kNumberCaseType),
UIFontFeatureSelectorIdentifierKey: #(kLowerCaseNumbersSelector),
};
UIFontDescriptor *descriptor = [[UIFontDescriptor alloc] initWithFontAttributes:
#{
UIFontDescriptorFamilyAttribute: #"Georgia",
UIFontDescriptorFeatureSettingsAttribute:#[ lowercaseNumbers ],
}];
UIFont *font = [UIFont fontWithDescriptor:descriptor size:15];
Result:
Edit: As #Random832 pointed out, Georgia has only lowercase numbers, so the result is irrelevant. However, #vikingosegundo confirmed this code works on supported fonts. Thanks.
The top line was generated with
UIFont *font = [UIFont fontWithName:#"DIN Next LT Pro" size:12];
if (font)
label.font = font;
the second line with
NSDictionary *lowercaseNumbers = #{ UIFontFeatureTypeIdentifierKey:#(kNumberCaseType), UIFontFeatureSelectorIdentifierKey: #(kLowerCaseNumbersSelector)};
UIFontDescriptor *descriptor = [[UIFontDescriptor alloc] initWithFontAttributes:
#{UIFontDescriptorFamilyAttribute: #"DIN Next LT Pro",UIFontDescriptorFeatureSettingsAttribute:#[ lowercaseNumbers ]}];
UIFont *font = [UIFont fontWithDescriptor:descriptor size:12];
if (font)
label.font = font;
Another question has a pointer in the right direction, though the question mentions tabular figures [vs proportional] rather than text vs lining.
It looks like you can use CTFontDescriptorCreateCopyWithFeature with kNumberCaseType set to kLowerCaseNumbersSelector to display the digits this way.
Here's another related question, and here's the blog post provided in the answer.
I'm trying to change the kerning on a couple of SKLabelNodes. I tried to use some code from another answer:
NSMutableAttributedString *attributedString;
attributedString = [[NSMutableAttributedString alloc] initWithString:#"Please get wider"];
[attributedString addAttribute:NSKernAttributeName value:#5 range:NSMakeRange(10, 5)];
[self.label setAttributedText:attributedString];
This isn't allowed:
[myLabelNode setAttributedText:attributedString];
And this doesn't carry over the changes I made:
myLabelNode.text = attributedString.string;
Is it possible to change kerning on an SKLabelNode?
As of iOS 11, NSAttributedStrings are supported in SKLabelNode. So kerning and other options should now be available to you.
May seem like a silly question, but I simply want to have 'm'[cubed symbol] rather than 'cubic metres' in a text label in XCode.
How is this done?
If you only need ³, you can use the appropriate unicode character (Unicode: U+00B3, UTF-8: C2 B3). On the Mac you can use the "Character Viewer" (e.g. on "Edit" menu - "Special characters"), enter "3" in the search box, and then grab the appropriate "Related character". See the Apple help document entering unicode characters for more information.
If you need a more general use of subscripts (or you need more fine grained control over how the superscript is rendered), you can use attributed strings. Thus, the following will render something like "1,000 m3":
UIFont *font = [UIFont fontWithName:self.label.font.familyName size:self.label.font.pointSize * 0.75];
NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:#"1,000 m3"];
NSDictionary *attributes = #{(id)kCTSuperscriptAttributeName : #(1),
(id)NSFontAttributeName : font};
[string addAttributes:attributes range:NSMakeRange(7, 1)];
Simply insert the symbol in the string literal using Edit > Special Characters... (type "cubed" in the search box):
textField.text = [NSString stringWithFormat:#"Your table is %u㎤", tableVolume);
These are the unit-cubed symbols I found:
㎣ ㎤ ㎥ ㎦
If want a generic superscript '3' then that will be harder to arrange...
I am facing an issue in getting the correct frame for two different strings. I am running two strings through the below code:
UILabel *myLabel = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
myLabel.lineBreakMode = UILineBreakModeWordWrap;
myLabel.numberOfLines = 2;
myLabel.textAlignment = UITextAlignmentCenter;
myLabel.font = [UIFont boldSystemFontOfSize:11];
myLabel.text = #"About Stores"
myLabel.text = #"About Rivers"
CGSize myLabelSize = CGSizeMake(70,28);
[myLabel sizeWithFont:myString.font // <-- One of two strings
constrainedToSize:aLabelSize
lineBreakMode:UILineBreakModeWordWrap];
Result for myString1 => Width: 70, Height: 28
Result for myString2 => Width: 70, Height: 14
Why there is a difference here?
Issues I see:
1
NSString has no font property. You might want to replace "myString.font" with UILabel.font where the UILabel is to be replaced by the instance of UILabel you plan to use to display the string.
2
sizeWithFont is supposed to return a CGSize that you store in an instance of CGSize.
Seems like you're not very clear on using this. I've posted an answer on how to correctly use sizeWithFont. You should check that out here : Place two UILabels side by side left and right without knowing string length of text in left label
By the way, that's why you see a difference in the heights... I think a garbage value is being passed in as the font size argument. If you hardcode the font size argument, you'll see consistent results.
just replace the code n give a try.....i don kno if it works jus check :)
[myLabel sizeWithFont:[UIFont fontWithName:#"Helvetica-Bold" size:11]
constrainedToSize:myLabelSize lineBreakMode:UILineBreakModeWordWrap];
and also ";" is missing in label`
myLabel.text = #"About Stores";
myLabel.text = #"About Rivers";`
regards..
There's a difference because the system font is Helvetica, which is a variable-sized font. The text in myString1 is maginally wider than the text in myString2, so the string needs to wrap to a second line in order to fit. Since you specified 2 lines max and UILineBreakModeWordWrap, that's what you get. If you want it to fit in one line, make myLabelSize a little wider or else use a smaller font size.