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...
Related
I have following string, and i want to show it in a UILabel properly with emojies and the new lines. Also I want to draw it using drawInRect method. How do I get them converted/Encoded/Decoded properly?
This string will change on runtime so should show any unicode character/ emoji or special characters such as \n or &
I'm sorry that I do not know proper terms to use to ask this question. Which makes it difficult for me to find an answer online. My knowledge about this topic is very low.
\ud83d\ude02\ud83d\ude02\ud83d\ude02\ud83d\ude02\ud83d\ude02\ud83d\ude05\ud83d\ude06
\u0db8\u0da0\u0d82
\u0d91\u0d9a\u0dca\u0d9a\n#set_with_machan\nkunuharapa na \n#Follow
#lankan_machan\nhttps://www.instagram.com/lankan_machan
after encoding the text should look like this with emojis, unicode characters & new lines.
I was able to find a solution and Edited it a bit. This un escapes the unicode characters perfectly and shows them properly. Shows the new lines too. Thanks #DanZimm for help.
- (NSString*) unescapeUnicodeString2:(NSString*)string
{
NSString* esc1 = [string stringByReplacingOccurrencesOfString:#"\\u" withString:#"\\U"];
NSString* esc2 = [esc1 stringByReplacingOccurrencesOfString:#"\"" withString:#"\\\""];
NSString* quoted = [[#"\"" stringByAppendingString:esc2] stringByAppendingString:#"\""];
NSData* data = [quoted dataUsingEncoding:NSUTF8StringEncoding];
NSString* unesc = [NSPropertyListSerialization propertyListWithData:data options:NSPropertyListImmutable format:NULL error:NULL];
assert([unesc isKindOfClass:[NSString class]]);
return unesc;
}
Solution found here I had to edit it because one of the methods were deprecated.
I'm making my own font dialogue, but it's embedded within WebKit. It's working pretty much how I want it to, except for one part. It's showing fonts that can not be written using standard ASCII characters. They appear in standard Latin characters, such as below. (note I'm setting the menu font using CSS)
This occurs with fonts such as Wingdings and fonts for languages such as Chinese. From what I've read, this is because there is no mapping between the ASCII characters to the font's characters.
I'd essentially like to filter out these fonts so only fonts that can actually be written with and are distinct appear. It should be possible, Pages manages to do it.
I've attempted to something like this, but it sometimes removes fonts that it shouldn't and leaves fonts that it should remove.
NSArray *availableFontFamilies = [[NSFontManager sharedFontManager] availableFontFamilies];
NSMutableArray *fontFamilies = [NSMutableArray array];
for (NSString *family in availableFontFamilies) {
NSFontDescriptor *fontDescriptor = [NSFontDescriptor fontDescriptorWithName: family size:16];
if ([fontDescriptor symbolicTraits] != 0 && ([fontDescriptor symbolicTraits] & NSFontSymbolicClass) == 0) {
[fontFamilies addObject: family];
}
}
NSLog(#"Families: %#", fontFamilies);
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.
I'm working on an application for iOS in which I would like to place the fractional value 1/2 as a single character within an NSString for use in an UILabel, is this possible?
Try setting the label's text property to the Unicode of 1/2:
label.text = [NSString stringWithFormat:#"%C",0x00bd];
You can simply use Mac OS's built-in character viewer: http://docs.info.apple.com/article.html?path=Mac/10.6/en/8164.html
So you can just do label.text = #"½"
I have a string in my cocoa GUI that needs to have special formatting (fonts, colors, etc.). Naturally, I'm using an attributed string. For convenience, I Init the string as an RTF:
NSString *inputString = #"This string has special characters";
NSString *rtfString = [NSString stringWithFormat:#"{#"***LENGTHY RTF FORMATTING STRING *** %#", inputString];
NSAttributedString *testString = [[NSAttributedString alloc] initWithRTF:[rtfString dataUsingEncoding:NSUTF8StringEncoding] documentAttributes:nil];
The problem is, the "inputString" might have special characters, which are not displayed properly due to the UTF8Encoding. They're replaced with other symbols.
é is left as Å©.
So, right now I'm doing this:
NSData* intermediateDataString=[inputString dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
inputString = [[[NSString alloc] initWithData:intermediateDataString encoding:NSUTF8StringEncoding] autorelease];
This does not display the unexpected characters, but it does remove all accents and leaves in their stead the unaccented letter - é is left as e.
This is an improvement since everything can be read, but it is far from ideal.
Thoughts?
I would do something like this. First, create a dummy attributed string:
NSString *dummyRTFString = #"***LENGTHY RTF FORMATTING STRING *** A";
NSAttributedString *dummyAS = [[NSAttributedString alloc]
initWithRTF:[rtfString dataUsingEncoding:NSUTF8StringEncoding]
documentAttributes:nil];
and obtain the attributes:
NSDictionary*attributes=[dummyAS attributesAtIndex:0 effectiveRange:NULL];
[dummyAS release];
Now I will use this attribute to create another attributed string:
NSAttributedString* as=[[NSAttributedString alloc] initWithString:inputString attributes:attributes];
Another approach is to use HTML instead of RTF; then you can include non-ascii characters as unicode in it.
In your first line of code, I assume that's really #"This string has special characters" otherwise you'd get a compile error. And it looks like your second line has an extra #".
If you know you're using UTF-8, why say NSASCIIStringEncoding?
Really, you should put the RTF including the string with special characters in a resource, not embedded in your code.