nsstring

Detect moment when newline starts in UITextView

有些话、适合烂在心里 提交于 2019-11-27 01:26:42
问题 I try to detect when carriage goes at new line in UITextView. I can detect it by comparison total later width with UITextView width: CGSize size = [textView.text sizeWithAttributes:textView.typingAttributes]; if(size.width > textView.bounds.size.width) NSLog (@"New line"); But it dose not work proper way because -sizeWithAttributes:textView returns only width of letters without indentation width. Help please solve this. 回答1: This is how I would do it: Get the UITextPosition of the last

Measuring the pixel width of a string

梦想与她 提交于 2019-11-27 01:09:18
I need to measure the pixel width of a string in Cocoa Touch. Can anyone point me to a link that explains how to do this? On iPhone OS it is slightly different, instead look at the NSString UIKit Additions Reference . The idea is the same as in Cocoa for Mac OS X, but there are more methods. For single lines of text use: - (CGSize)sizeWithFont:(UIFont *)font forWidth:(CGFloat)width lineBreakMode:(UILineBreakMode)lineBreakMode And for multiline texts use: - (CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size lineBreakMode:(UILineBreakMode)lineBreakMode The use of a

Simple string concatenation in Objective C

霸气de小男生 提交于 2019-11-27 01:01:43
问题 I have an NSString named 'you' with value "This is a you string!". I want to concat "123" in 'you', how can I do it? I am using this code and it is giving an error. you=[you stringByAppendingString:@"123"]; 回答1: This code here is working for me NSString *s = @"avant"; s = [s stringByAppendingString:@" - après"]; NSLog(@"%@", s); 2012-01-13 11:48:59.442 tabbar[604:207] avant - après So my guess is that your you is a bad pointer that is not nil and not the NSString you think it have. Have you

iphone sdk - Remove all characters except for numbers 0-9 from a string [duplicate]

依然范特西╮ 提交于 2019-11-27 00:51:56
This question already has an answer here: Remove all but numbers from NSString 22 answers OK So here's the plan. The XML I'm getting data from allows non-numeric text in phone number fields (for descriptions or contact names, etc). I am trying to extract only the numbers and call the tel: URL with them to initiate a call. Here's whats NOT working: NSCharacterSet *charset = [[NSCharacterSet decimalDigitCharacterSet] invertedSet]; NSString *number = @"(555) 555-5555 Office"; NSString *strippedNumber = [number stringByTrimmingCharactersInSet:charset]; NSString *phoneURL = [NSString

Converting between NSData and base64 strings

 ̄綄美尐妖づ 提交于 2019-11-27 00:41:48
What is the easiest and fastest code to do a conversion between NSData and a base64 string? I've read a bunch of solutions at SO and mostly they involve in adding another class etc. I found a great solution here but it's too complex. Scroll down to the Conclusion section on the page you linked and download the provided NSData+Base64 files . Its the best solution I have seen so far and is incredibly easy to use. If you can learn anything about Cocoa, you can learn to use that project. Example NSString *originalString = [NSString stringWithFormat:@"test"]; NSData *data = [NSData

How can I extract a URL from a sentence that is in a NSString?

老子叫甜甜 提交于 2019-11-27 00:29:32
问题 What I'm trying to accomplish is as follows. I have a NSString with a sentence that has a URL within the sentience. I'm needing to be able to grab the URL that is presented within any sentence that is within a NSString so for example: Let's say I had this NSString NSString *someString = @"This is a sample of a http://abc.com/efg.php?EFAei687e3EsA sentence with a URL within it."; I need to be able to extract http://abc.com/efg.php?EFAei687e3EsA from within that NSString. This NSString isn't

How to convert a unichar value to an NSString in Objective-C?

大憨熊 提交于 2019-11-27 00:27:47
I've got an international character stored in a unichar variable. This character does not come from a file or url. The variable itself only stores an unsigned short(0xce91) which is in UTF-8 format and translates to the greek capital letter 'A'. I'm trying to put that character into an NSString variable but i fail miserably. I've tried 2 different ways both of which unsuccessful: unichar greekAlpha = 0xce91; //could have written greekAlpha = 'Α' instead. NSString *theString = [NSString stringWithFormat:@"Greek Alpha: %C", greekAlpha]; No good. I get some weird chinese characters. As a sidenote

Finding out whether a string is numeric or not

邮差的信 提交于 2019-11-27 00:05:07
问题 How can we check if a string is made up of numbers only. I am taking out a substring from a string and want to check if it is a numeric substring or not. NSString *newString = [myString substringWithRange:NSMakeRange(2,3)]; 回答1: Here's one way that doesn't rely on the limited precision of attempting to parse the string as a number: NSCharacterSet* notDigits = [[NSCharacterSet decimalDigitCharacterSet] invertedSet]; if ([newString rangeOfCharacterFromSet:notDigits].location == NSNotFound) { //

NSString containing hex convert to ascii equivalent

試著忘記壹切 提交于 2019-11-26 23:24:14
问题 I have an NSString that has hex information such as <00000020 66747970 4d344120 00000000 4d344120 6d703432 69736f6d 00000000 00031203 which came from NSData. What I need to do is convert that NSString of Hex data to Ascii which would be: [0][0][0] ftypM4A [0][0][0][0]M4A mp42isom[0][0][0][0][0][3][18][3] As you might be able to tell this is a M4A file. I loaded the first part of the file in to NSData using NSFileHandle. I then stored it into NSString: NSData *data = [filehandle

How to know if NSString fits in UILabel or not and index of the last string which fits?

痴心易碎 提交于 2019-11-26 23:17:30
问题 I have 4 lines UILabel with exact frame and font. I need to know if this string fits the label and what is the index of the last character which fits. 回答1: The kernel of the answer is in Cupcake's referenced posting. Anyway, you can use sizeWithFont:constrainedToSize:lineBreakMode: to figure out what the size of a frame would be with a particular font in a label of a given width given a specific word wrapping, e.g. CGSize size = [string sizeWithFont:font constrainedToSize:sizeConstraint