How do I display the character for 1/2 in an NSString in iOS? - objective-c

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 = #"½"

Related

How to save text from textview?

I have a textview where you can write multiple lines of text. How do you save that text in a file or variables?
I used the multiline text field but it doesn't let me go to next line unless I hit control enter.
What I'm thinking is like a text editor, after you type everything you save that in a file. Or I can get each line from the text view into a variable.
What's the best way to do this?
NSString can save text up to 4.2 billion characters. \n denotes a line break, so no need to save into multiple parameters.
NSString *text = textView.text;
OSX
NString *text = [[textView textStorage] string];
If you're looking for each individual line for whatever reason, you could use componentsSeparatedByString
NSArray *linesArray = [textView.text componentsSeparatedByString:#"\n"];
Each line will be available at linesArray[0], linesArray[1] etc...
[linesArray count] will give you the total number of lines... with linesArray[[linesArray count]-1] being the last line in the string.
The textView.text property is an NSString also... so when you say saved.. do you mean intra app session? If so you can use NSUserDefaults
Save object
[[NSUserDefaults standardUserDefaults]setObject:textView.text forKey:#"TheKeyForMyText"];
Get object
NSString *text = [[NSUserDefaults standardUserDefaults]objectForKey:#"TheKeyForMyText"];
Assign
In swift
let var_name = textfield.text
Or in objective C
NSString *string_name = textfield.text;
And use the variable where you want to.

Superscript cents in an attributed string

I'm trying to get my label to look like so:
But using attributed string, I managed to get this result:
My code:
NSString *string = [NSString stringWithFormat:#"%0.2f",ask];
NSMutableAttributedString *buyString = [[NSMutableAttributedString alloc] initWithString:string];
[buyString addAttribute:NSFontAttributeName
value:[UIFont systemFontOfSize:15.0]
range:NSMakeRange(2, buyString.length - 2)];
self.labelBuy.attributedText = buyString;
As you see, the numbers after the dot, stay below, and I would like to pop them to the top as the first example.
Is there any way to set attributed string frame?
You have to use NSBaselineOffsetAttributedName.
From the doc:
NSBaselineOffsetAttributeName
The value of this attribute is an
NSNumber object containing a floating point value indicating the
character’s offset from the baseline, in points. The default value is
0. Available in iOS 7.0 and later.
From your example:
[buyString addAttribute:NSBaselineOffsetAttributeName
value:#(10.0)
range:NSMakeRange(2, buyString.length - 2)];
You may have to change the value to fit your needs.
Why not actually use superscript? You must first #import "CoreText/CoreText.h"
[buyString addAttribute:(NSString *)kCTSuperscriptAttributeName
value:#1
range:NSMakeRange(2, buyString.length - 2)];

How do I get a cubed symbol in a text box?

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

How to make a NSString always use fixed font width

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.

How to find caret position in an NSTextView?

I've an NSTextView with with several semi-colon separated strings. I need to find on which of those strings the caret has been placed. How could I do that?
NSInteger insertionPoint = [[[myTextView selectedRanges] objectAtIndex:0] rangeValue].location;
For Swift 4
let insertionPointIndex = myTextView.selectedRanges.first?.rangeValue.location