nsstring

Using a NSString to set a color for a label

旧城冷巷雨未停 提交于 2019-12-21 20:16:31
问题 I want to create a method and use a string value "redColor" to set the UIColor for a label. tableColorName is the NSString "redColor" and I tried to apply a selector to create the UIColor and apply it to my textLabel. Thanks -(void) changeLabelColor { SEL labelColor = NSSelectorFromString([NSString stringWithFormat:[@"%@", tableColorName]]); UIColor *color = [[UIColor class] performSelector:labelColor]; self.textLabel.textColor = color; } 回答1: Use this method -(UIColor *

How to count the number of uppercase characters in a NSString?

狂风中的少年 提交于 2019-12-21 19:27:29
问题 I'm trying to find out the best way to count the number of uppercase characters that are in a NSString. I know how to find out if a certain character is uppercase by using this code: NSString *s = @"This is a string"; BOOL isUppercase = [[NSCharacterSet uppercaseLetterCharacterSet] characterIsMember:[s characterAtIndex:0]]; What would be the best way to count the number of uppercase letters in a NSString? Thanks. 回答1: NSString *s = @"This is a string"; int count=0; for (i = 0; i < [s length];

stringWithFormat vs initWithFormat under ARC

你说的曾经没有我的故事 提交于 2019-12-21 17:51:47
问题 stringWithFormat: is a class method of NSString , and returns an autoreleased string; initWithFormat: is an instance method, and before ARC the programmer had to take care of the returned object's memory management. If we have ARC turned on, what is the difference between the two methods? 回答1: With ARC, these two methods are equivalent. See: WWDC 2011 Session Video - Introducing Automatic Reference Counting WWDC 2011 Session Video - Objective-C Advancements In-Depth (explains how ARC code

(# ゚Д゚) is a 5-letter-word. But in iOS, [@“(# ゚Д゚)” length] is 7. Why?

风格不统一 提交于 2019-12-21 12:33:03
问题 (# ゚Д゚) is a 5-letter-word. But in iOS, [@"(# ゚Д゚)" length] is 7. Why? I'm using <UITextInput> to modify the text in a UITextField or UITextView . When I make a UITextRange of 5 character length, it can just cover the (# ゚Д゚) . So, why this (# ゚Д゚) looks like a 5-character-word in UITextField and UITextView , but looks like a 7-character-word in NSString??? How can I get the correct length of a string in this case? 回答1: 1) As many in the comments have already stated, Your string is made of 5

Word wrap for NSMutableAttributedString

不打扰是莪最后的温柔 提交于 2019-12-21 09:12:16
问题 I have NSMutableAttributedString and the string is pretty long. I would like to do word wrap while displaying it on the UIlabel. If it was NSString, i will go ahead and do something like this, Dynamic UILabel truncating the text But how can i do it with NSAttributedString ? And once it is done, i need to resize the view depending on the label size. 回答1: The lineBreakMode property isn't deprecated in iOS 6. It has simply changed the names of the constants. The old constants are deprecated, but

Fastest way to get array of NSRange objects for all uppercase letters in an NSString?

ε祈祈猫儿з 提交于 2019-12-21 05:46:13
问题 I need NSRange objects for the position of each uppercase letter in a given NSString for input into a method for a custom attributed string class. There are of course quite a few ways to accomplish this such as rangeOfString:options: with NSRegularExpressionSearch or using RegexKitLite to get each match separately while walking the string. What would be the fastest performing approach to accomplish this task? 回答1: The simplest way is probably to use -rangeOfCharacterFromSet:options:range:

UITextField get currently edited word

不羁的心 提交于 2019-12-21 04:59:24
问题 I'm working on an autocompletion component and I have one problem that I would like to solve in some easy way. I want to support edits to autocompleted text, for example: blablabl @usertag blablabl If user goes back and edits @usertag string, I would like to start autocompletion when it's edited. Question is, how to get currently edited word from textfield. I thought about taking cursor position, seperate nsstring to word by " " (space) and count letters from first word to cursor position. Is

Displaying proportionally spaced numbers (instead of monospace / tabular) on iOS

纵然是瞬间 提交于 2019-12-21 04:32:13
问题 I am rendering numbers in iOS (targeting 7 and up) by storing them in an NSAttributedString and rendering with "drawAtPoint:". I am using Helvetica Neue. I have noticed that digits of numbers drawn like this are not proportional – the glyphs all have the same width. Even a skinny "1" takes up the same space as a "0". A test confirms this: for(NSInteger i=0; i<10; ++i) { NSString *iString = [NSString stringWithFormat: @"%d", i]; const CGSize iSize = [iString sizeWithAttributes: [self

Delete all characters after a certain character from a string in Swift [duplicate]

天涯浪子 提交于 2019-12-21 03:34:18
问题 This question already has answers here : What is the more elegant way to remove all characters after specific character in the String object in Swift (4 answers) Closed 3 years ago . I have a textField and I would like to remove all character after a certain character. For instance if what I have in the textField is the word Orange and I want to remove all characters after the n I would like to get Ora after the deletion. How can I delete all characters after a certain character from a string

Remove newline character from first line of NSString

こ雲淡風輕ζ 提交于 2019-12-21 03:27:33
问题 How can I remove the first \n character from an NSString? Edit: Just to clarify, what I would like to do is: If the first line of the string contains a \n character, delete it else do nothing. ie: If the string is like this: @"\nhello, this is the first line\nthis is the second line" and opposed to a string that does not contain a newline in the first line: @"hello, this is the first line\nthis is the second line." I hope that makes it more clear. 回答1: This should do the trick: NSString *