Indenting on linewrap - iphone

I'm creating one long string from a number of strings in an array like this:
oneString = #"First Line\n";
for (int i = 0; i < itemsToAdd.count; i ++){
oneString = [NSString stringWithFormat:#"%# %#\n", oneString, stringToAdd];
}
This all works great until stringToAdd is long enough to wrap. If you notice the spaces between %# %#, this is acting as an indentation. Most of the strings I'm adding don't wrap, but I'm trying to figure out a way to add spaces before each line after the first.
The reason I'm building up this one long string is to calculate the height of it in order to adjust the size of a tableViewCell based on the length of this string. itemsToAdd is a variable size array. Sometimes it will be one line, sometimes it will be 15+ items.
This is sample output:
First Line
Item 1 is good
Item 2 is fine
Item 3 is long and it wraps so
it goes like this and it sucks
Item 4 is fine

Try like this for insert tab space:
oneString = [NSString stringWithFormat:#"%#%8s\n", oneString, [stringToAdd UTF8String]];
from how to add tab space into UILabel.text

You need to specify the indent in a way understood by the text rendering system, for example, by using NSParagraphStyle. You will need to make some changes to your cells depending on which versions of iOS your app supports. You may want to use a 3rd party label class or a framework like DTCoreText.

Related

Objective c - Text indentation

This question is about implementing text indentation ("The placement of text farther to the right to separate it from surrounding text") in iOS.
Take for example the following text:
This is the first section.
This is the second one,
with two lines.
This is the third.
Notice that the second row in section 2 begin farther to the right and just below the line above.
My code contains an array of NSString, each one should be display as a section with numeric bullet like above. for example:
NSArray *array = [NSArray arrayWithObjects:#"1. This is the first section.", #"2. This is the second one, with two lines.", #"3. This is the third.", nil];
I use UILable to display the text on screen.
To set the text from the array to the label, and to separate each string in a new line I use
myLabel.text = [array componentsJoinedByString:#"\n"];
Any ideas how to get this effect?
This is possible to some degree in iOS6 with - [UILabel setAttributedText:].
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.headIndent = 29;
myLabel.attributedText = [[NSAttributedString alloc] initWithString:
#"1.\tShort line.\n2.\tLong line with content that triggers wrapping.\n3.\tShort line."
attributes:#{NSParagraphStyleAttributeName: paragraphStyle}];
This adds indentation to the subsequent lines. It looks like iOS doesn't support tab stops in the same way as OSX so I'm not seeing a way to adjust the gap between the number and the text. This is probably possible in CoreText.
Of course, you could also just replace the label with a UIWebView and have full formatting control on all versions of iOS at the cost of performance.
Well I decided to implement it my self without using Core Text, I just created a view strcture that make all the indentation work by itself, and let you customize it as you want.
For all of you interested in the implementation, you can see the source code and an example project here:
ECListView Project
UILabel is not going to cut it if you have any kind of specific layout requirements. For that, you're going to need to dig into Core Text. The good news is that Core Text will let you do any kind of text layout you can imagine. The bad news is that all that power brings with it some complexity, so to use it you're going to have to invest some time learning how the framework works.
An alternative that's suitable in some situations is to use a web view to display your text. UIWebView will let you do whatever text layout you can manage using HTML and CSS.

NSString UITextView without replacing previous text

How would I go about adding text to a UITextView without replacing the previous text?
So far I have a UITextView and a UIButton that adds the text to the UITextView, but I would like the text field to append more text every time you hit the button instead of completely deleting the text and replacing it.
Here are some ways to overcome obstacles in iOS development:
Look at the documentation for the particular class you're trying to manipulate. In this case, UITextView documentation can be found within Xcode or online.
Command-Click on UITextView or any other object anywhere in your code, and it will bring you to the header file for that class. The header file will list every public method and property.
Look at your existing code. I'm assuming that since you have a button that adds text to a UITextView, you understand how to set its text. 99% of the time you'll find that any setter (mutator) methods will have a corresponding getter (accessor) method. In this case, UITextView has a method called setText: and a matching method just called text.
Finally, NSString has a convenience method called stringWithFormat: that you can use to concatenate (join) two strings, among other very useful things. %# is the format specifier for a string. For example, to combine two strings, stringOne and stringTwo, you could do the following:
NSString *string = [NSString stringWithFormat:#"%# %#", stringOne, stringTwo];
I will leave you to come up with the answer as to how to combine NSString stringWithFormat: and UITextField text and setText: to achieve what you'd like to accomplish.
Edit:
The OP was unable to figure out how to utilize the information above so a complete code sample has been provided below.
Assume you have synthesized property (possibly an IBOutlet) UITextView that you have initialized called myTextView. Assume also that we are currently in the method scope of the method that gets called (your IBAction, if you're using IB) when you tap your UIButton.
[myTextView setText:[NSString stringWithFormat:#"%# %#", myTextView.text, #"this is some new text"]];
Explanation: myTextView.text grabs the existing text inside of the UITextView and then you simply append whatever string you want to it. So if the text view is originally populated with the text "Hello world" and you clicked the button three times, you would end up with the following progression:
Initial String: #"Hello world"
Tap one: #"Hello world this is some new text"
Tap Two: #"Hello world this is some new text this is some new text"
Tap Three: #"Hello world this is some new text this is some new text text this is some new text"
If all you are doing is appending text, you might find this a little simpler:
myTextView.text = [myTextView stringByAppendingString:#"suffix\n"];
I found this on UITextView insert text in the textview text. Sadly, I have not found a way to append text directly without a wholesale replacement of the text in the UITextView. It bugs me that the effort involved is proportional to the total length of the existing string and the suffix, rather than just the suffix.
A more efficient way to append text is to use replace() at the end:
extension UITextInput {
func append(_ string : String) {
let endOfDocument = self.endOfDocument
if let atEnd = self.textRange(from: endOfDocument, to: endOfDocument) {
self.replace(atEnd, withText: string)
}
}
}
#Jack Lawrence: Your answer doesn't cover the question completely.
The example below will not scroll neatly while running off the bottom when called every second:
self.consoleView.text = [NSString stringWithFormat:#"%#%#%#", self.consoleView.text, data, #"\n"];
[self.consoleView scrollRangeToVisible:NSMakeRange(self.consoleView.text.length, 0)];
This is caused by setText replacing the original text every time thereby resetting associated contentOffsets etc.
This was possible prior to iOS 7, but since iOS 7 it seems that setText cannot be prevented from exhibiting jumpy behaviour. Appending does not seem to be an option for TextViews in this scenario?

Display a series of user selected integers, separated by a comma

I need to produce a list of integers in a label.
On the iPhone screen there are a series of 12 buttons and a label-
(0) (1) (2) (3) (4) (5) (6) (7) (8) (9) (10) (11)
[___________]
The user is to click a button, and the buttons's id should then be displayed in a label.
If the user clicks 0,4,7,3,10
Then I want the label to display- [0,4,7,3,10]
This is similar to a calculator app, but a calculator is base10 and its numbers combine to form a string. I would like to keep each integer separate so that other calculations may be performed on the user selected order.
in my #implementation i have tried to modify my calculator app's code, but have had no progress.
Any ideas?
Sounds like you need to keep an array (or some sort of stack) of the numbers pressed, and append to the label's text every time the user hits a button. What are you having issues with?
Your buttons could fire this IBAction:
- (IBAction)addNumberToLabel:(UIButton *)sender
{
[numbers addObject:[[sender titleLabel] text]];
[label setText:[NSString stringWithFormat:#"[%#]", [numbers componentsJoinedByString:#", "]];
}
Since you want the numbers to be displayed [1, 2, 3, 4], it is necessary to change the whole text of the label every time a new number is added to numbers, an NSMutableArray you initiate in your setup code.
Create a new string StrOldValue, and get the actual value of label, in the set to label use
NSString *strOldValue= yourLabel.text;
yourLabel.text= [NSString stringWithFormat:#"%#,%#",strOldValue, strNewValue)];
Keep your things in a Mutable array.
Then call componentsJoinedByString method like
NSLog(#"%#",[pathArray componentsJoinedByString:#","]);
You can use any string in place of 'Comma' as your delimiter.

iPhone: Phone Number UITextView vs UILabel

I have a UITableView. In row I need to setup text, so I need to use UILabel or UITextView. I need to detect phone number in text, so I use UITextView. BUT I need to set text only in 2 lines and if it's longer, setup ellipses in the end , because row height isn't big, so that work UILabel do perfect. So how to be ??? Can UILabel detect phone number ? or can i set UITextView in 2 lines and set ellipses in the end ?? Thanks...
I do not think that the UITextView has a property that let you set ellipses at the end if the text is to long. However that is simple to fix:
NSString documentation
You could use this method if all you want to do is to allow let say 120 characters, and starting from 0.
substringToIndex:
Returns a new string containing the
characters of the receiver up to, but
not including, the one at a given
index.
(NSString *)substringToIndex:(NSUInteger)anIndex
NSString *shortText =
[[someLongTextVariable substringToIndex: 120] stringWithString:#"..."];
Maybe you should use a mutable string for this, I do not know what performes the best. But if you do this, you could atleast detect phone numbers.

CoreText and strikethrough on iPhone

I'm currently struggling with the need to display strikethrough text in many UITableViewCells. Something that written in HTML would looke like
<strike>€99</strike> save 50% => now €49
I don't want to use a UIWebView just for a single line of text, especially that it's used in small UITableViewCells. I know there are reusable cells and all, but I'd like to keep things the more memory-efficient way possible.
So... I'm using NSAttributedStrings, with the help of AliSoftware's UILabel-replacement OHAttributedLabel. The fact that it's only available starting with iOS 4.0 is no problem, as we use all kinds of stuff only 4.0-compatible.
I can manage to create the attributed string, it displays text in the OHAttributedLabel, OK, that's cool. But what I can't achieve is setting the "strikeout", or "strikethrough" attribute.
Basically I go like this:
NSString *price = [NSString stringWithFormat:#"%01.2f €", product.price];
NSString *rate = [NSString stringWithFormat:#" -%01.0f%%", product.reductionRate];
NSMutableAttributedString *s = [NSMutableAttributedString attributedStringWithString:price];
[s addAttribute:NSStrikethroughStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlinePatternSolid | NSUnderlineStyleSingle] range:NSRangeFromString(price)];
[attributedLabel setAttributedText:s];
But here, the three NS* constants are undefined. I've imported CoreText.h, Foundation/NSAttributedString.h, to no avail. I've seen somewhere on the web that
NSStrikethroughStyleAttributeName = #"NSStrikethroughStyleAttributeName", and that NSUnderlinePatternSolid = 0 and NSUnderlineStyleSingle = 1, but hard-coding these values don't give anything.
One thing I got with auto-completion are the equivalent kCT...* constants, but there are kCTUnderlineStyleAttributeName, kCTStrokeWidthAttributeName, ... but no mention of kCTStrikethrough_anything.
What should I do to display that *$|#!# piece of strike-through text ?
With iOS 6 you can use NSStrikethroughStyleAttributeName
[attributedString addAttribute:NSStrikethroughStyleAttributeName value:[NSNumber numberWithInt:NSUnderlineStyleSingle] range:selectedRange];
While it may seem out of place, the numberWithInt value is correct as NSUnderlineStyleSingle.
A simpler approach might be two labels, using the answer to this question - Pixel Width of the text in a UILabel - to strikeout the text in one of the labels.